Android WifiManager.addNetwork() returns -1

后端 未结 5 490
深忆病人
深忆病人 2020-12-10 10:47

I am writing an android app which will connect to a specific WPA access point, when connected, it will issue a http call. It will not save the network config. I have read al

5条回答
  •  借酒劲吻你
    2020-12-10 11:35

    I just had this very same problem. Seemingly everything was okay, but then - it wasn't.

    What I found is this:

    • Android WifiStateMachine will not allow you to add or modify networks unless supplicant is running and connected-to. This affects services running on start-up and services running even if WIFI is off.

    • Android WifiConfigStore tracks owners of the wifi network by UID. It means that you may not be able to modify network created by another process.

    The proper way to add a network is:

    1. Check if WIFI network is enabled. If not, call WifiManager.setWifiEnabled(true).
    2. Wait until WifiManager.pingSupplicant() returns true.
    3. Create and fill a new WifiConfiguration, then pass it to WifiManager.addNetwork(). Make sure the value returned is not (-1).
    4. (optional) use value returned by addNetwork() as an argument to WifiConfiguration.enableNetwork() call (unless it's -1). Note, that the boolean parameter means disableOthers and should be false, unless you have right to modify all other networks: it may fail internally, if you set it to true.

    This should allow you to add (and connect) programmatically to a new Wifi network.

提交回复
热议问题