Created
November 14, 2017 12:47
-
-
Save MaTriXy/0370e297f4600873795c7edb8e8f18e8 to your computer and use it in GitHub Desktop.
Get Local IP Address in kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much, Was searching for this!