7

I am building an app where the connection to a 2nd device is the essence. Therefore, I used the WifiNetworkSpecifier API. However, the application must be able to automatically reconnect to the target network once the users leave and return to the Wi-Fi perimeter. Thus, I used the WifiNetworkSuggestion API. However, I am experiencing several issues there:

  1. Once I get connected to the SSID using the specifier API and I confirm the push notification generated by the suggestion API, the suggestion API does not seem to work until I manually disconnect from the SSID (unregister network callback previously assigned to the specifier request) or kill the application.
  2. If there is another network present in the perimeter which the user previously connected to by using the OS Wi-Fi manager (a hotspot, for instance), Android will prioritize this network, hence the suggestion API for my application would never auto-reconnect to the wanted and accessible SSID.

From my experience and understanding (which might be wrong) so far, it seems like we have to manually unregister the network callback previously assigned to the specifier request, or kill the application, and let the suggestion API to do its thing until it can work properly. This might be problematic if there are other networks (which the user previously connected to by using the OS Wi-Fi manager) present in the perimeter. In this case, we'd never auto-reconnect to the SSID defined by the application and the suggestion API would never work.

The question is: how to combine those two APIs to be able to connect to an SSID, yet auto-reconnect, without doing such ugly hacks as manually disconnecting the user, or killing the application, which also doesn't give us any guarantees?

In my opinion, this whole new implementation with the new network APIs is not done well, it's creating a lot of issues and restrictions for developers, or at least it's poorly documented.

Here's the code used for making the requests. Note that the device I'm connecting to does not have actual internet access, it's just used as a p2p network.

@RequiresApi(api = Build.VERSION_CODES.Q)
private fun connectToWiFiOnQ(wifiCredentials: WifiCredentials, onUnavailable: () -> Unit) {

    val request = NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
        .setNetworkSpecifier(createWifiNetworkSpecifier(wifiCredentials))
        .build()

    networkCallback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            connectivityManager.bindProcessToNetwork(network)
        }

        override fun onUnavailable() {
            super.onUnavailable()
            onUnavailable.invoke()
        }
    }

    networkCallback?.let {
        addNetworkSuggestion(wifiCredentials)
        connectivityManager.requestNetwork(request, it)
    }
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private fun addNetworkSuggestion(wifiCredentials: WifiCredentials) {
    wifiManager.addNetworkSuggestions(listOf(createWifiNetworkSuggestion(wifiCredentials))).apply {
        if (this != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
            if (this == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_EXCEEDS_MAX_PER_APP) {
                wifiManager.removeNetworkSuggestions(emptyList())
                addNetworkSuggestion(wifiCredentials)
            }
        }
    }

    suggestionBroadcastReceiver?.let { context.unregisterReceiver(it) }
    suggestionBroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent?.action != WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)
                return

            // Post connection processing..
        }
    }

    context.registerReceiver(
        suggestionBroadcastReceiver, IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)
    )
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private fun createWifiNetworkSpecifier(wifiCredentials: WifiCredentials): WifiNetworkSpecifier {
    return when (wifiCredentials.authenticationType.toLowerCase()) {
        WifiCipherType.NOPASS.name.toLowerCase() -> WifiNetworkSpecifier.Builder()
            .setSsid(wifiCredentials.networkSSID)
            .setIsHiddenSsid(wifiCredentials.isSSIDHidden)
            .build()
        WifiCipherType.WPA.name.toLowerCase() -> WifiNetworkSpecifier.Builder()
            .setSsid(wifiCredentials.networkSSID)
            .setWpa2Passphrase(wifiCredentials.password)
            .setIsHiddenSsid(wifiCredentials.isSSIDHidden)
            .build()
        else -> WifiNetworkSpecifier.Builder()
            .setSsid(wifiCredentials.networkSSID)
            .setIsHiddenSsid(wifiCredentials.isSSIDHidden)
            .build()
    }
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private fun createWifiNetworkSuggestion(wifiCredentials: WifiCredentials): WifiNetworkSuggestion {
    return when (wifiCredentials.authenticationType.toLowerCase()) {
        WifiCipherType.NOPASS.name.toLowerCase() -> WifiNetworkSuggestion.Builder()
            .setSsid(wifiCredentials.networkSSID)
            .setIsHiddenSsid(wifiCredentials.isSSIDHidden)
            .build()
        WifiCipherType.WPA.name.toLowerCase() -> WifiNetworkSuggestion.Builder()
            .setSsid(wifiCredentials.networkSSID)
            .setWpa2Passphrase(wifiCredentials.password)
            .setIsHiddenSsid(wifiCredentials.isSSIDHidden)
            .build()
        else -> WifiNetworkSuggestion.Builder()
            .setSsid(wifiCredentials.networkSSID)
            .setIsHiddenSsid(wifiCredentials.isSSIDHidden)
            .build()
    }
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Luja93
  • 451
  • 3
  • 8

1 Answers1

0

Calling the suggestion API in onAvailable works for me. That way the user doesn't see two popups at the same time either.

val networkCallback = object : ConnectivityManager.NetworkCallback() {
    override fun onAvailable(network: Network) {
        connectivityManager.bindProcessToNetwork(network)
        addNetworkSuggestion(wifiCredentials)
    }
}
Donny Rozendal
  • 852
  • 9
  • 12