How to auto connect a WiFi with specified SSID?

前端 未结 2 1670
野趣味
野趣味 2021-02-09 13:35

Can some body help me to solve this issue?

Here is my code, and at mWifi.enableNetwork(netID, true) it\'s cannot enable network and cannot auto connect to

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-09 13:56

    I think you need to add a WifiConfiguration.KeyMgmt to your WifiConfiguration object. Assuming it's an open network:

    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    

    Also, be cautious in assuming that scan results are available immediately upon exit of your call to startScan(). The best bet in this case is to add a BroadcastReceiver on WifiManager.SCAN_RESULTS_AVAILABLE_ACTION and add to it all of your code from mWifi.getScanResults() forward. You will need to add a call to mWifi.reconnect() once you get enableNetwork() to succeed.

    As for initializing your WifiConfiguration wc, I'd love it if you'd consider my post here. Finally, another good answer is here.

提交回复
热议问题