Network Interface has no address when tethering in android

465 views Asked by At

I am attempting to setup a broadcast or multicast server on Android while it is acting as a hotspot. After some initial troubles with broadcasting (receiving network unreachable errors) I saw that this is normal for Android so I opted to implement it via Multicast since we were considering that as preferable anyway. When I did so, I receive the following error:

W/System.err(13578): java.net.SocketException: No address associated with interface: [wlan0][21]

I understand what this means, but when I check the interfaces while my hotspot is active using the netcfg command in ADB Shell, I see the following:

wlan0  UP  192.168.43.1/24  0x00001043 02:1a:11:f0:3d:80

So I don't understand why in the runtime I get a no address associated exception but the shell command shows one, unless it is because Android is trying to stop me somehow. Any help understanding what is going on and how I might successfully open a Multicast Socket on this interface would be great. I attempt to open it as below:

// Create the multicast socket
final MulticastSocket socket = new MulticastSocket(0);
Log.d(TAG, "Joining multicast group: 224.0.0.30");
final SocketAddress address = new InetSocketAddress(InetAddress.getByName("224.0.0.30"), socket.getLocalPort());
socket.joinGroup(address, NetworkInterface.getByName("wlan0"));
Log.d(TAG, "Multicast socket using interface: " + socket.getNetworkInterface());
1

There are 1 answers

0
Jared On BEST ANSWER

Turns out this is caused by a race condition, which makes perfect sense after I thought about it. It takes some time before Android is able to update the file in sys/class/net. My solution was to simply poll repeatedly until it returns with an address, and then continue.