Oreo Wifi Connectivity

前端 未结 4 966
無奈伤痛
無奈伤痛 2021-02-05 20:29

I am working on an IoT app in which there is an on boarding process where the user connects to an access point, which has not internet connectivity, configure the device and the

4条回答
  •  情话喂你
    2021-02-05 21:04

    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);
                    }
               }
         });
    
    }
    
    1. 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.

    1. I Made 2-3 attempts for connection

      new CountDownTimer(16000, 2000) {
      
    2. 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:
      

提交回复
热议问题