Connecting to WiFi network automatically in Android 5 and 6

前端 未结 1 736
小蘑菇
小蘑菇 2021-01-15 07:50

Been struggling with connecting to WiFi network in Android 5 and 6 for a while and other similar questions don\'t seem to work for me. I could get the same code working in A

相关标签:
1条回答
  • 2021-01-15 08:27

    I believe if you add a disconnect() it will do what you want. I've added a check here because addNetwork() can fail to add your network. For Android 6.0+, you can't add a network if it exists already but if this fails you can try getting the network id then connecting. (For instance, it will be there if added (and saved) if you re-install) I also do not add quotes to SSID in Lollipop (ver 21)+.

    int value = mWifiManager.addNetwork(conf);
    // need to check the result if successful
    if (value == -1) {
        return; // network not added successfully
    }
    Log.i(TAG_CHECK, "Connecting to : " + value);
    mWifiManager.disconnect();  // add a disconnect
    boolean enabled = mWifiManager.enableNetwork(value, true);
    Log.i(TAG_CHECK, "enabled to : " + enabled);
    mWifiManager.reconnect();
    
    0 讨论(0)
提交回复
热议问题