Skip to content

Commit

Permalink
App/Service: Revise the getIpAddress method
Browse files Browse the repository at this point in the history
This patch revises the getIpAddress method to return the proper localhost
address corresponding to the emulator or the real target. Moreover, the
platform type variables are eliminated.

Signed-off-by: Wook Song <[email protected]>
  • Loading branch information
wooksong committed May 27, 2024
1 parent d0c85c0 commit 49dda32
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,28 @@ class MainService : Service() {
return result
}

private fun getIpAddress(): String? {
val connectivityManager = applicationContext.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
// TODO: Add an ApplicationContext Parameter
private fun getIpAddress(): String {
val connectivityManager = App.context().getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork
val linkProperties = connectivityManager.getLinkProperties(network)
val linkAddresses = linkProperties?.linkAddresses
linkAddresses?.forEach {linkAddress ->
val inetAddress = linkAddress.address
if (inetAddress is Inet4Address && !inetAddress.isLoopbackAddress && inetAddress.isSiteLocalAddress) {
return inetAddress.hostAddress
var inetAddress = if (isRunningOnEmulator) "10.0.2.2" else "localhost"
val linkProperties = connectivityManager.getLinkProperties(network) ?: return inetAddress

for (linkAddress in linkProperties.linkAddresses) {
val address = linkAddress.address ?: continue

when {
address !is Inet4Address -> continue
address.isLoopbackAddress -> continue
else -> {
inetAddress = address.hostAddress?:continue

break
}
}
}

return "localhost"
return inetAddress
}

private fun findPort(): Int {
Expand Down

0 comments on commit 49dda32

Please sign in to comment.