Skip to content

Commit

Permalink
App/Service: Get IP address form device
Browse files Browse the repository at this point in the history
This patch removes hardcoded IP address and uses
Connectivity manager to get IP address.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed May 24, 2024
1 parent da5fb23 commit ebff36b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions ml_inference_offloading/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.app.Service
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.net.ConnectivityManager
import android.os.Binder
import android.os.Handler
import android.os.HandlerThread
Expand All @@ -25,6 +26,8 @@ import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream
import java.net.Inet4Address
import java.net.InetAddress
import java.net.ServerSocket
import kotlin.concurrent.thread

Expand All @@ -48,8 +51,6 @@ class MainService : Service() {
}

private val TAG = "MainService"
// todo: Use internal network only now
private val HOST_ADDR = "192.168.50.191"
private val binder = LocalBinder()
private lateinit var serviceHandler : MainHandler
private lateinit var serviceLooper : Looper
Expand Down Expand Up @@ -208,6 +209,21 @@ class MainService : Service() {
return result
}

private fun getIpAddress(): String? {
val connectivityManager = applicationContext.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
}
}

return "localhost"
}

private fun findPort(): Int {
var port = -1
val portFinder = thread() {
Expand All @@ -225,11 +241,12 @@ class MainService : Service() {
}

fun startServer() {
val hostAddress = getIpAddress()
if (!isPortAvailable(port)) {
port = findPort()
}

val desc = "tensor_query_serversrc host=" + HOST_ADDR + " port=" + port.toString() + " ! " +
val desc = "tensor_query_serversrc host=" + hostAddress + " port=" + port.toString() + " ! " +
"other/tensor,format=static,dimension=(string)3:224:224:1,type=uint8,framerate=0/1 ! " +
"tensor_filter framework=tensorflow-lite model=" + model.getAbsolutePath() + " ! " +
"other/tensor,format=static,dimension=(string)1001:1,type=uint8,framerate=0/1 ! tensor_query_serversink async=false"
Expand Down

0 comments on commit ebff36b

Please sign in to comment.