15

以下のコードを使用して、暗号化されたネットワークに接続します。ただし、ネットワークが保護されておらず、キーを空( "")のままにすると、失敗します。誰かがこれを解決する方法を考えていますか?さらに、ssid / bssidを使用してネットワークが開いているかどうかを検出することは可能ですか?または、フィルターでスキャンする必要がありますか?

public void connectToSSID(final String ssid, final String key) {
    Log.i("wifimaster", "connection to "+ssid);

    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\""+ssid+"\""; //IMPORTANT! This should be in Quotes!!  
    wc.priority = 40;
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);

    wc.preSharedKey = "\""+key+"\"";
    wc.wepKeys[0] = "\""+key+"\""; //This is the WEP Password
    wc.wepTxKeyIndex = 0;

    wc.preSharedKey = "\""+key+"\"";

    int res = wifiManager.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res );
    boolean es = wifiManager.saveConfiguration();
    Log.d("WifiPreference", "saveConfiguration returned " + es );
    boolean b = wifiManager.enableNetwork(res, true);   
    Log.d("WifiPreference", "enableNetwork returned " + b );

    if(b)
        Toast.makeText(c, c.getString(R.string.connected), Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(c, c.getString(R.string.unconnected), Toast.LENGTH_SHORT).show();
}
4

1 に答える 1

14

使用する

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

のみ、それは動作します。

への他のすべての割り当てを削除しwcます。ネットワークに接続するという単純なタスクにそれらを使用する必要はありません。あなたはcreatingネットワークではなく、ネットワークに接続していますexisting

完全な解決策については、次のリンクを確認してください:Androidの特定のWi-Fiネットワークにプログラムで接続するにはどうすればよいですか? それはあなたが知る必要があるすべてを持っています。

于 2012-10-19T06:36:37.300 に答える