2015-08-20 200 views
2

我正在創建一個Android應用程序,該應用程序允許您通過特定的BSSID連接到WiFi。我已經實現了掃描所有wifis的部分,並在WiFi網絡安全類型的功能上做出WiFi配置。我還通過特定的BSSID實現了與WiFi網絡的連接。如何使用特定的bssid android連接到wifi?

但我有一個問題:與特定BSSID的連接適用於安全網絡,但對於開放網絡並不起作用,我不知道爲什麼。事實上,這種與具有特定BSSID的開放網絡的連接在三星Galaxy S4或更新的版本上工作,但不適用於Galaxy S2和S3 ......這真的很奇怪。在S2和S3上,連接期間不考慮BSSID。

這是我用它來創建一個開放的網絡配置,並嘗試在它與特定的BSSID連接樣本代碼:

WifiConfiguration conf = new WifiConfiguration(); 
    conf.SSID = "\"" + wifiItem.getSSID() + "\""; 
    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
    WifiManager wifiManager = (WifiManager) parentActivity.getSystemService(Context.WIFI_SERVICE); 

    wifiManager.addNetwork(conf); 
    conf.BSSID = wifiItem.getBSSID(); 
    wifiManager.updateNetwork(conf); 
    wifiManager.saveConfiguration(); 

    for (WifiConfiguration wifiConfiguration : wifiManager.getConfiguredNetworks()) { 

     if (wifiConfiguration.SSID.equals("\"" + wifiItem.getSSID() + "\"")) { 

      wifiManager.disconnect(); 
      wifiConfiguration.BSSID = wifiItem.getBSSID(); 
      wifiManager.updateNetwork(wifiConfiguration); 
      wifiManager.enableNetwork(wifiConfiguration.networkId, true); 
      wifiManager.reconnect(); 

     } 

如果有人可以幫助我,這將是太酷了。我花了很多時間,真的不明白這個問題。

+0

做你解決這個問題?分享你的結論嗎? – atx

回答

1

你可以試着找過這段代碼,這就是我們用來連接到網絡:

private void connectToNetwork(String password, ScanResult result, String capabilities) { 
    WifiConfiguration wc = new WifiConfiguration(); 
    wc.SSID = "\"" + result.SSID + "\""; 

    wc.hiddenSSID = true; 
    wc.status = WifiConfiguration.Status.ENABLED; 

    if (capabilities.contains("WPA2")) { 
     wc.preSharedKey = "\"" + password + "\""; 
     wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 
     wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    } 

    if (capabilities.contains("WPA")) { 
     wc.preSharedKey = "\"" + password + "\""; 
     wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 
     wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 
    } 

    if (capabilities.contains("WEP")) { 
     wc.wepKeys[0] = password; 
     wc.wepTxKeyIndex = 0; 
     wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
     wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
    } 

    if (capabilities.contains("TKIP")) { 
     wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 
     wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 
    } 

    if (capabilities.contains("CCMP")) { 
     wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
     wc.allowedKeyManagement.set(WifiConfiguration.PairwiseCipher.CCMP); 
    } 

    if (!hasWifiSecurity(capabilities)) { 
     wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE); 
     wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 
    } 

    // wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); 

    // // wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 
    // wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

    int res = wifi.addNetwork(wc); 
    Log.d("WifiPreference", "add Network returned " + res); 
    boolean b = wifi.enableNetwork(res, true); 
    Log.d("WifiPreference", "enableNetwork returned " + b); 
    Log.d("", "Reassociate: " + wifi.reassociate()); 
    Log.d("", "ReConnect: " + wifi.reconnect()); 
    wifi.saveConfiguration(); 

    WifiInfo connection = wifi.getConnectionInfo(); 
    if (connection != null) { 
     if (connection.getSupplicantState().equals(SupplicantState.INACTIVE)) { 
      wifi.removeNetwork(res); 
      wifi.saveConfiguration(); 
      scanForWiFiNetworks(); 

      wifi.enableNetwork(wifi.getConnectionInfo().getNetworkId(), true); 

     } 
    } 
    // scanForWiFiNetworks(); 

    // if (password.equalsIgnoreCase("")) { 
    // setScanningEnabled(true); 
    // } 
    // Toast.makeText(con, "Connecting to network: " + connectionInfo, Toast.LENGTH_SHORT).show(getMessageComments(dialog.getTextID())); 
} 
+0

謝謝,我已經看到了,但這段代碼並沒有解決我的問題... – Boublond

相關問題