2
votes

I have some local network services that do not need internet access. Since Lollipop android can automatically switch its active network to mobile data if no internet connectivity is detected on wifi network (exlamation mark icon). I receive ConnectivityManager.CONNECTIVITY_ACTION broadcast that says mobile is taking over.

When that happens it seems I cannot establish TCP connections and multicast UDP packets are ignored even though I have a MulticastLock aquired. I tried to reaquire it, used requestNetworkFeature(), setNetworkPreference() and pretty much every other method ConnectivityManager has to offer but nothing helps.

However turning mobile data off makes android switch back to wifi, I receive the broadcast that its switching and everything goes back to normal. It seems the wifi network must be the active network in order to have full functionality.

Is there a way to force the system to use wifi even if there is not internet?

1

1 Answers

1
votes

I ran into a similar issue. By using the new android.net.Network class and its method bindSocket, I was at least able to establish a socket connection via WiFi.

Just pick the WiFi network from ConnectivityManager.getAllNetworks() and either bind your socket to this network or use the socket factory from the network.

The Network class and the according methods in the ConnectivityManager require SDK 21, but you can use reflection if you do not want to switch the SDK.