I’m trying to figure out how to use Android’s new and “improved” NetworkSpecifier
APIs to properly connect to a hidden network programmatically, instead of using the old and deprecated WifiConfiguration
method.
Currently, my application can already connect to a visible network using the following code:
networkRequestCallback = BlockingNetworkCallback(connectivityManager, future)
connectivityManager.requestNetwork(
NetworkRequest.Builder().apply {
addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
setNetworkSpecifier(
WifiNetworkSpecifier.Builder().apply {
setSsid(ssid)
}.build()
)
}.build(),
networkRequestCallback
)
Where the BlockingNetworkCallback
simply implements the NetworkCallback
interface, and it calls connectivityManager.bindProcessToNetwork(network)
inside its onAvailable()
once it finds the desired network. I can post this code if necessary, though I find it somewhat trivial.
Once the requirements of our app changed, and we were now required to find a connect to a hidden network instead of a visible one, my hope was that adding this line setIsHiddenSsid(true)
into the NetworkSpecifier
would do the trick. But as it is very usual with Android, that is not the case all the time.
Pixel phones work like a charm to no surprise, but it seems that no other phone wants to make that happen — I’ve tried a few devices from the Samsung-family, Motorolas, LGs, even a random Sony device we’ve got.
Long story short, I’d like to know if there’s a better way of connecting to a hidden network in Android 10 and above and, ideally, using some kind of NetworkRequest
, unless of course there’s a better solution.