Skip to content

Instantly share code, notes, and snippets.

@MaTriXy
Created November 14, 2017 12:47
Show Gist options
  • Select an option

  • Save MaTriXy/0370e297f4600873795c7edb8e8f18e8 to your computer and use it in GitHub Desktop.

Select an option

Save MaTriXy/0370e297f4600873795c7edb8e8f18e8 to your computer and use it in GitHub Desktop.
Get Local IP Address in kotlin
private fun getLocalIpAddress(): String? {
try {
val wifiManager: WifiManager = context?.getSystemService(WIFI_SERVICE) as WifiManager
return ipToString(wifiManager.connectionInfo.ipAddress)
} catch (ex: Exception) {
Log.e("IP Address", ex.toString())
}
return null
}
private fun ipToString(i: Int): String {
return (i and 0xFF).toString() + "." +
(i shr 8 and 0xFF) + "." +
(i shr 16 and 0xFF) + "." +
(i shr 24 and 0xFF)
}
@mage1k99
Copy link
Copy Markdown

Thank you very much, Was searching for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment