I wrote an Android app for Android 9 that displays the user a list of available wifi networks and lets the user connect to them. Connecting to networks that offer internet connection works fine. But in most of the cases I have to connect to networks without internet connection. For my project I have to create my own access points/hotspots on a linux machine and therefore want my app to be able to connect to it.
If I pick one of my created networks, I am reconnected to a known and already configured wifi my device knows of that it offers internet connectivity (our office wifi for example).
I've already had a detailed look on stackoverflow but most people with this problem seemed to be working with Android Marshmallow or Lollipop and actually rather had the problem that their device was using mobile data to send/receive data while being connected to the non-internet wifi.
AFAIK Android likely rejects wifi networks that do not offer internet access. How can I tell the system to connect to them anyway? Connecting to these networks through the settings app works fine. It just doesn't work out of my app.
Here's my connect code so far:
private fun connectToSelectedNetwork(networkSSID: String){
try {
val conf = WifiConfiguration()
conf.SSID = "\"" + networkSSID + "\""
conf.preSharedKey = "\"" + PASSPHRASE + "\""
val network = wifiManager?.addNetwork(conf)
wifiManager?.disconnect()
wifiManager?.enableNetwork(network!!, true)
wifiManager?.reconnect()
} catch (ex: Exception) {
println(Arrays.toString(ex.stackTrace))
}
}
Thanks!