Android 13 can't connect to wifi programmatically
2024-04-17 13:45

I'm trying to connect to a wifi (passing the ssid and password). The callback of the function it does return that the wifi is available, put it does not really connect to the wifi. The code does work in Android 12 and below, but in Android 13 and above is not connecting.

This is the method that I'm using for connecting to the wifi:

public void reconnectToWifi(Context context, String password, OnAvailableCallback onAvailableCallback) { // Disconnect from the current Wi-Fi network wifiManager.disconnect(); Handler handler = new Handler(Looper.getMainLooper()); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { // Create a new WifiNetworkSpecifier.Builder for the desired Wi-Fi network WifiNetworkSpecifier specifier = new WifiNetworkSpecifier.Builder() .setSsid(ssid) .setWpa2Passphrase(password) .build(); // Create a new NetworkRequest using the specifier NetworkRequest request = new NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) .setNetworkSpecifier(specifier) .build(); // Register a NetworkCallback to handle the connection ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(@NonNull Network network) { super.onAvailable(network); // Connection successful, perform any necessary actions Log.d(TAG, "Connected to the Wi-Fi network: " + ssid); boolean connected = isConnectedToSpecificWifi(context, ssid); if (connected) { Log.d(TAG, "Really connected to the Wi-Fi network: " + ssid); handler.postDelayed(onAvailableCallback::onAvailableCallback, 5000); } else { Log.d(TAG, "Not connected to the Wi-Fi network: " + ssid); reconnectToWifi(context, "pass", onAvailableCallback); } connectivityManager.unregisterNetworkCallback(this); } @Override public void onUnavailable() { super.onUnavailable(); // Connection unsuccessful, handle accordingly Log.d(TAG, "Failed to connect to the Wi-Fi network: " + ssid); connectivityManager.unregisterNetworkCallback(this); reconnectToWifi(context, "pass", onAvailableCallback); } }; // Request the network using the NetworkRequest and NetworkCallback connectivityManager.requestNetwork(request, networkCallback); Log.d(TAG, "Connecting to the Wi-Fi network: " + ssid); } }

I have the permissions:

< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> < uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> < uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> < uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> < uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> < uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES">


other answer :

It seems like youre using the Network Specifier API to connect to a WiFi network programmatically in Android, and youre encountering issues specifically on Android 13 and above.

There have been some changes and improvements in network connectivity handling in newer versions of Android, which might be affecting your code.

Here are a few suggestions and potential areas to investigate to resolve the issue:

Permissions: Ensure that your app has the necessary permissions declared in the manifest file, especially the ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions, which are required to interact with WiFi networks programmatically.

Check NetworkCapabilities: In the NetworkRequest.Builder, you are removing the NET_CAPABILITY_INTERNET capability. This indicates that the network should not have Internet access. Make sure that this behavior is intentional and doesnt conflict with the network requirements.

Debugging Logs: Add more logging statements to your code to track the flow and identify any potential issues or errors. Check for any relevant logcat messages or exceptions that might provide clues about why the connection is not being established.

Testing Different WiFi Networks: Try connecting to different WiFi networks with varying security configurations (e.g., open, WPA2, WEP) to see if the issue is specific to certain types of networks.

Verify WiFi Configuration: Double-check that the SSID and password you are providing are correct and match the configuration of the WiFi network youre trying to connect to.

Update Code for Compatibility: Review the latest Android documentation and release notes to ensure that your code is up-to-date and compatible with the changes introduced in Android 13 and above. There might be new APIs or recommendations for handling network connectivity.

Consider Alternative Approaches: If you continue to experience difficulties with the Network Specifier API, consider alternative approaches for connecting to WiFi networks programmatically, such as using the WifiConfiguration API or intent-based methods.

By investigating these areas and making necessary adjustments to your code, you should be able to diagnose and resolve the connectivity issue on Android 13 and above.