现在,我正在开发一个必须连接到特定SSID并使用AP创建套接字的应用程序。我想通过接收器建立连接时得到事件。我正在使用以下代码连接到SSID:
public boolean connectToSSID(Context context, final String ssid, final String pass) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\""; // Please note the quotes.
// String should
// contain ssid in quotes
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> networkList = wifiManager.getScanResults();
setText("Check WiFi devices");
if (networkList != null) {
for (ScanResult network : networkList) {
// check if current connected SSID
if (network.SSID.equals(ssid)) {
// get capabilities of current connection
String Capabilities = network.capabilities;
// Log.d (TAG, network.SSID + " capabilities : " +
// Capabilities);
if (Capabilities.contains("WPA2")) {
conf.preSharedKey = "\"" + pass + "\"";
} else if (Capabilities.contains("WPA")) {
conf.preSharedKey = "\"" + pass + "\"";
} else if (Capabilities.contains("WEP")) {
conf.wepKeys[0] = "\"" + pass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers
.set(WifiConfiguration.GroupCipher.WEP40);
}
wifiManager.addNetwork(conf);
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();
/*if(isWifiConnected(context)){
setText("Connected to SSID");
return true;
}else{
setText("Unable to connect");
return false;
}*/
return true;
}
}
break;
}
}
}
return false;
}
我已经阅读了这些问题,但是找不到答案。
How do I connect to a specific Wi-Fi network in Android programmatically?
How do I see if Wi-Fi is connected on Android?
How to detect when WIFI Connection has been established in Android?
How to auto connect a WiFi with specified SSID?
问题是我可以通过接收器获取连接事件吗?在此先感谢