Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Q, programmatically connect to different WiFi AP for internet

As in Android Q, several WiFi APIs are restricted. I am trying to use alternate APIs to connect to different Wifi AP for internet.

Below is my code :

    WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
    builder.setSsid("wifi-ap-ssid");
    builder.setWpa2Passphrase("wifi-ap-password");

    WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();

    NetworkRequest.Builder networkRequestBuilder1 = new NetworkRequest.Builder();
    networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier);

    NetworkRequest nr = networkRequestBuilder1.build();
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    cm.requestNetwork(nr, callback);

This allows me to connect but Internet is disabled. This is working as defined in Android docs.

Alternate way i tried is below :

    WifiNetworkSuggestion.Builder wifiNetworkSuggestionBuilder1 = new WifiNetworkSuggestion.Builder();
    wifiNetworkSuggestionBuilder1.setSsid("wifi-ap-ssid");
    wifiNetworkSuggestionBuilder1.setWpa2Passphrase("wifi-ap-password");
    WifiNetworkSuggestion wifiNetworkSuggestion = wifiNetworkSuggestionBuilder1.build();
    List<WifiNetworkSuggestion> list = new ArrayList<>();
    list.add(wifiNetworkSuggestion);
    wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiManager.removeNetworkSuggestions(new ArrayList<WifiNetworkSuggestion>());
    wifiManager.addNetworkSuggestions(list);

declared permission in Manifest :

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

Using this didn't change anything in behavior.

Please let know sequence of APIs to connect successfully to different Wifi AP with internet capability.

like image 502
Vinodh Avatar asked Jul 01 '19 06:07

Vinodh


People also ask

How do I connect to a specific WiFi network in Android programmatically?

Required permissions in the manifest file. Programmatically add a new wifi network to the WifiManager. With this new wifi network added to the WifiManager, when the device is located within this wifi network, the WifiManager will automatically connect to this new wifi network.

Is there a way to connect Android 10 and 11 through Wi-Fi programmatically?

No, This is not possible to enable or disable Wi-Fi programmatically from Android-10 API level 29 [Until google provides an alternative solution].

Can I connect to multiple Wi-Fi networks Android?

You can only be connected to one network at the time. However, there is a standard called WiFi direct that will allow you to do what you are asking for, but it isn't yet implemented in Android.


1 Answers

Try calling bindProcessToNetwork() in onAvailable() callback to regain network connectivity, it works fine for me.

Connect to network:

    WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
    builder.setSsid("wifi-ap-ssid");
    builder.setWpa2Passphrase("wifi-ap-password");

    WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();

    NetworkRequest.Builder networkRequestBuilder1 = new NetworkRequest.Builder();
    networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier);

    NetworkRequest nr = networkRequestBuilder1.build();
    ConnectivityManager cm = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    ConnectivityManager.NetworkCallback networkCallback = new 
        ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            super.onAvailable(network);
            Log.d(TAG, "onAvailable:" + network);
            cm.bindProcessToNetwork(network);
        }
    });
    cm.requestNetwork(nr, networkCallback);

Disconnect from the bound network:

cm.unregisterNetworkCallback(networkCallback);
like image 96
Neil.Ling Avatar answered Sep 28 '22 06:09

Neil.Ling