Reputation: 21
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">
Upvotes: 1
Views: 435