1

I am trying to connect to a device via wifi

final WifiNetworkSpecifier wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
                .setSsidPattern(new PatternMatcher(networkSSIDPattern, PatternMatcher.PATTERN_PREFIX))
                .setWpa2Passphrase(passWord)
                .build();


final NetworkRequest networkRequest = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                .setNetworkSpecifier(wifiNetworkSpecifier)
                .build();


connectivityManager = (ConnectivityManager) RainBird.getContext().getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);

connectivityManager.requestNetwork(networkRequest, networkCallback);

I am getting a dialog with the good device network, after connecting, the same network appears two times in the dialog.

how can I fix it?

First connection view: enter image description here

after one connection, the same network appears 2 times: enter image description here

The problem only occurs on android12

haythem souissi
  • 3,263
  • 7
  • 50
  • 77

3 Answers3

1

I believe that Mike's answer might help you:

This is the proper way to register for connectivity changes on API 21 and higher. The following code can be placed in a base activity and that way you can expect every screen in your app (that inherits from this activity) to get these callbacks.

First, create a network callback which will monitor connectivity changes.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private val networkCallback: ConnectivityManager.NetworkCallback = object : ConnectivityManager.NetworkCallback() {

    override fun onAvailable(network: Network?) {
        super.onAvailable(network)
    }

    override fun onLost(network: Network?) {
        super.onLost(network)
    }
}

Then, register and unregister this callback in the relevant spots.

override fun onResume() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
        cm?.registerNetworkCallback(NetworkRequest.Builder().build(), networkCallback)
    }
}
override fun onPause() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
        cm?.unregisterNetworkCallback(networkCallback)
    }
}
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
1

Finally, I found the solution I just changed compileSdkVersion from 30 to 31

ConnectivityManager in android 12 (version 31) contain the fix

thank you every one

Actually this did fix the problem for my pixel 6 I still can reproduce the problem on samsung S22...

haythem souissi
  • 3,263
  • 7
  • 50
  • 77
1

On Android 12 WifiNetworkSpecifier API is only for peer-to-peer local-only network which means it must NOT have NetworkCapabilities.NET_CAPABILITY_INTERNET. For more information please check this link. If you are looking for an internet connection, please use WifiNetworkSuggestion API.

Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25