2016-07-24 154 views
0

我打電話功能WifiManager.addNetwork(WifiConfiguration)試圖添加一個adhoc wifi網絡到設備的wifi配置。但是在Android M上,這個函數返回-1,我假設它是因爲它沒有互聯網訪問。它在大多數其他設備上工作正常。下面是我使用的代碼片段。Android,自動連接到沒有互聯網接入的WiFi網絡

WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = '\"' + ssid + '\"'; wifiConfiguration.hiddenSSID = false; wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wifiManager.addNetwork(wifiConfiguration);

任何辦法來解決互聯網連通性檢查和強制網絡添加?

回答

0

您需要啓用後,網絡:

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks(); 
for(WifiConfiguration i : list) { 
    if(i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) { 
     wifiManager.disconnect(); 
     wifiManager.enableNetwork(i.networkId, true); 
     wifiManager.reconnect();    
    enter code here 
     break; 
    }   
}