Oreo Wifi Connectivity
I had same issue. This is how I fix this issue in Android 8+ devices. 1. Since Android 8+ devices auto switch between WiFI and Cellular devices. I removed below code. i.e. Code to force move to wifi.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { NetworkRequest.Builder request = new NetworkRequest.Builder(); Log.i("forceCellularConnection","request WIFI enable"); request.addCapability(NetworkCapabilities.TRANSPORT_WIFI); connectivityManager.requestNetwork(request.build(), new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { connectivityManager.setProcessDefaultNetwork(network); connectivityManager.bindProcessToNetwork(network); } } });}
Also added below code to forget wifi connection before we make new connection. It takes some time for device to disconnect WiFI. So I added delay before connecting to new WIFI SSID.
this.wifiManager.disableNetwork(configuration.networkId);this.wifiManager.removeNetwork(configuration.networkId);this.wifiManager.saveConfiguration(); // Not needed in API level 26 and above
Workaround which I tried and helped me making connection work are below: In my case also I have to connect WiFi which doesn't have internet on it. Its a peer to peer connection.
I Made 2-3 attempts for connection
new CountDownTimer(16000, 2000) {
Also I have written below broadcast receiver to check the state of WiFI. And then made the connection.
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); Log.d("WifiReceiver", ">>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<"); SupplicantState supl_state = ((SupplicantState) intent .getParcelableExtra(WifiManager.EXTRA_NEW_STATE)); switch (supl_state) { case ASSOCIATED: Log.i("SupplicantState", "ASSOCIATED"); // authMsg = "ASSOCIATED"; break; case ASSOCIATING: // authMsg = "ASSOCIATING"; Log.i("SupplicantState", "ASSOCIATING"); break; case AUTHENTICATING: // authMsg = "AUTHENTICATING"; Log.i("SupplicantState", "Authenticating..."); break; case COMPLETED: authMsg = "CONNECTED"; Log.i("SupplicantState", "Connected"); final ConnectivityManager connection_manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkRequest.Builder request = new NetworkRequest.Builder(); request.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); connection_manager.registerNetworkCallback(request.build(), new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { ConnectivityManager.setProcessDefaultNetwork(network); } }); break; case DISCONNECTED:
I had the same issue. What I did is adding a step to the boarding process where I invite the user to go to there settings, add manually this network with no data and accept the popup shown by the system.
I have not found a real solution yet.However, I've seen an app called eWeLink where they manage to do it, but it is not an open source project.
I am currently facing the same issue right now for the newer phone with Android 8.+. I am using WifiManager call bindProcessToNetwork
here this will allows data traffic go through the wifi with no internet access,
here how I connect my phone to the access point
//Method to connect to WIFI Networkpublic boolean connectTo(String networkSSID, String key) { WifiConfiguration config = new WifiConfiguration(); WifiInfo info = wifi.getConnectionInfo(); //get WifiInfo int id = info.getNetworkId(); //get id of currently connected network config.SSID = "\"" + networkSSID + "\""; if (key.isEmpty()) { config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); } int netID = wifi.addNetwork(config); int tempConfigId = getExistingNetworkId(config.SSID); if (tempConfigId != -1) { netID = tempConfigId; } boolean disconnect = wifi.disconnect(); wifi.disableNetwork(id); //disable current network boolean enabled = wifi.enableNetwork(netID, true); boolean connected = wifi.reconnect();if (((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)) || ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) && !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M))) { final ConnectivityManager manager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkRequest.Builder builder; builder = new NetworkRequest.Builder(); //set the transport type do WIFI builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); manager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { manager.bindProcessToNetwork(network); } else { ConnectivityManager.setProcessDefaultNetwork(network); } try { } catch (Exception e) { e.printStackTrace(); } manager.unregisterNetworkCallback(this); } }); }
after connecting to the access point created by the device, it will constantly drop off from it.