I want to use 2 android devices, 1 as a server and 2 as a client. And this process should be through the local network, so the 1st device will have to open a hotspot and the 2nd device will have to connect to it. I did the same process but connect from 2 devices to the server It doesn't work. I don't understand what I should do in this case. I mean, which thread should I connect to?
val client = HttpClient(Android) {
install(Logging)
}
object KtorClient {
var ipAddress: String = ""
var ipv4Address = ""
fun getData() {
GlobalScope.launch {
val data = client.request("http://$ipAddress:8080/api/data")
println("DATA:${data.call.body<String>()}")
}
}
fun otherGetData() {
GlobalScope.launch {
val data = client.request("https://$ipv4Address:8080/api/data")
println("DATA:${data.call.body<String>()}")
}
}
}
fun getLocalIpAddress(): String {
try {
val interfaces = NetworkInterface.getNetworkInterfaces()
while (interfaces.hasMoreElements()) {
val networkInterface = interfaces.nextElement()
val addresses = networkInterface.inetAddresses
while (addresses.hasMoreElements()) {
val address = addresses.nextElement()
if (!address.isLoopbackAddress && address.hostAddress.contains(":").not()) {
return address.hostAddress ?: ""
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return "N/A"
}