I'm developing a Raspberry Pi-based IoT device which hosts its own WiFi AP (no internet) and have successfully followed this Android Developers blog post from 2016 to connect to it and route traffic to it from my Android app, even when mobile data is enabled (i.e. via Network#getSocketFactory
). (i.e. I'm NOT having the issue reported in this question: Send request over WiFi (without connection) even if Mobile data is ON (with connection) on Android M)
The problem now is that my Android 10 device (a Google Pixel) automatically disconnects from the network and switches to my home WiFi network (with internet) after a few minutes. This is with the app in the foreground, with an active web socket connection to a server-side app running on the Pi.
I could work around the problem by listening for network state changes in the app and forcibly reconnecting to my IoT network via WifiManager#enableNetwork
, but this seems like a hacky solution and the connection would still be interrupted, leading to a poor user experience.
Another thought I had was to use WifiManager#disableNetwork
to disable all other configured WiFi networks, thus preventing the phone from connecting to them. However the doc states that disabling networks created by other apps is not allowed.
Maintaining a connection to an IoT WiFi network with no net access seems to be a reasonable use case for an Android app that Google are (or were) aware of, but I'm struggling to piece together the current best practices for how to do this in 2020. I'm wondering if it's achievable via the newer WiFi suggestion APIs. However these sound like are even more restrictive to app developers and don't offer any guarantees about the WiFi network that will actually be joined.