I have an app that needs to connect to a hardware via Wi-Fi. For this purpose, the hardware offers an offline Wi-Fi Hotspot. The hotspot's DHCP doesn't give us a gateway or DNS so we know right away, that it's for offline use only.
The end user will be at work and have to stay connected to the Wi-Fi the whole time in order to receive sensor data and alerts if something is wrong with the data. But at the same time, we don't want to force the end user to be offline with his mobile device. He should be able to use his mobile data for our and all other apps he has.
I though I can control this by calling android.net.ConnectivityManager.requestNetwork. But it doesn't seem to work reliably. Will calling requestNetwork
in my app influence if all the other apps have access to mobile data?
Is this even something that we can control? I tested this on multiple devices with different manufacturers and android versions and they all behave differently.
E.g. Pixel 3a works fine. After some time I get a notification regarding the offline Wi-Fi. If I ignore it, it keeps working. If I press "yes" on it, I am offline and connected to the Wi-Fi.
Huawei P20 Lite seems to fire a OnLost
event for my mobile data NetworkCallback
every time I request it. Like its trying to cancel the mobile connection every time I reconnect because I'm in the wifi. Some devices offer a "Smart Switch" (Samsung) or "Wi-Fi+" (Huawei) mode which isn't on per default. And it seems like a hassle to ask the user to turn it on, since they might not want to use it in different scenarios.
After trying this out on various devices, I can say, that it is not possible to achive the desired behaviour consinstently on all devices. Pretty much all the devices, behave differently. The OS-version is not really a factor. It depends more on how the vendors handle the case, that a user is connected to an offline wifi.
Based on the device, it might be possible to stay connected to offline wifi and use mobile data at the same time for all apps. But the OS can automatically disconnect you or lock you into the offline-wifi and disable mobile data. Maybe it does it because the user selected an option in the system dialogs as shown above.
In the latter case, it is possible to force a mobile data connection with the ConnectivityManager
for the own app. But it is not possible to make all the other apps use this mobile connection as well.