I want my app to create a SoftAP (or have the user start the Hotspot for older Android versions) and broadcast data there.
Say the subnet is defined as 192.168.43.1/24, I want to be able to send to 192.168.43.255.
How can I detect this IP programmatically? I find it very difficult to find information about getting the router IP, the netmask and the client IPs from the official documentation. Still, if feels like discovering peers is the whole point of using a SoftAP, isn't it?
I tried the following:
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connManager.allNetworks.forEach { network ->
val linkProps = connManager.getLinkProperties(network)
linkProps?.linkAddresses?.forEach { linkAddress ->
println("linkAddress: ${linkAddress.address}/${linkAddress.prefixLength}")
}
println("dhcp server: ${linkProps?.dhcpServerAddress?.hostAddress}")
}
But this only seems to show the "active" Network (the one that is connected to the Internet).
This one apparently shows all interfaces (at least it shows the hotspot interface), but I don't have a way to know that it is the hotspot, and I don't know the network prefix:
NetworkInterface.getNetworkInterfaces().asSequence().forEach { net ->
net.inetAddresses.asSequence().forEach { addr ->
println("host addr: ${addr.hostAddress}")
}
}
Also for some reason the docs seem to show promising interfaces like SoftApCallback#onConnectedClientsChanged, but first I don't see IPs there (just the MAC of the client), and second I can't seem to access it from my code (wifiManager.registerSoftApCallback does not seem to exist for me).
from Find subnet IP of Android (soft) hotspot
No comments:
Post a Comment