特定の WPA アクセス ポイントに接続する Android アプリを作成しています。接続すると、http 呼び出しが発行されます。ネットワーク構成は保存されません。wifiネットワークへの接続時のスタックオーバーフローに関するほぼすべての投稿を読みましたが、私に合った答えが見つかりません。ここに私が接続するために使用しているコードがあります..
WifiConfiguration wc = new WifiConfiguration();
wc.allowedAuthAlgorithms.clear();
wc.allowedGroupCiphers.clear();
wc.allowedPairwiseCiphers.clear();
wc.allowedProtocols.clear();
wc.allowedKeyManagement.clear();
wc.SSID = "\"".concat("<ssid>").concat("\"");
wc.preSharedKey = "\"".concat("<password>").concat("\"");
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
wc.priority = 0;
//wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
// connect to and enable the connection
WifiManager wifiManager = (WifiManager) getSystemService(this.WIFI_SERVICE);
int netId = wifiManager.addNetwork(wc);
boolean wifiEnabled = wifiManager.enableNetwork(netId, true);
wifiManager.setWifiEnabled(true);
Log.d("opener", "addNetwork returned " + netId);
if (netId > 0) {
wifiId = netId;
}
ただし、netId は常に -1 です。2 つの異なる電話 (ICS:HTC Rezound と GingerBread:Motorola DroidX) で試しました。どちらもまったく同じ結果を示しています。私は何を間違っていますか?
編集: WPA2 アクセス ポイントで同じコードを試したところ、非常に奇妙な結果が得られました。このコードを実行すると、最初は -1 が返されますが、1 秒の遅延で同じメソッドを 2 回目に呼び出すと、有効な netId が返されます。だから質問は
- 上記のコードが wpa に接続しないのはなぜですか?
- wpa2 では、接続するために上記のメソッドを 2 回呼び出す必要があるのはなぜですか? 編集:接続するには複数回接続する必要があることがわかりました。接続に 3 ~ 4 回かかる場合があります。したがって、今のところ、構成を追加すると >0 id が返されるまでループしています。