在我的应用程序中,我需要在扫描二维码后连接到WiFi。我应该如何去实现这个目标呢?
我尝试过读取二维码,并成功地使用了https://github.com/ZBar/ZBar/tree/master/android。如何使用从二维码获取的数据连接到WiFi?我得到的数据格式是:"Id:23,wifiName:wert,wifiPass:12345678“
我发现了这个库https://github.com/zxing/zxing,但我不能制定一个方法,如何使用它?
我不需要确切的代码。只需提到正确的方法即可。
发布于 2018-09-27 14:43:19
你必须标记化你从二维码扫描收到的数据。你有像"Id:23,wifiName:wert,wifiPass:12345678"这样的形式的数据,所以为了触发这些数据,下面的方法将给你ssid和密码,以便连接到wifi。
public String contentParsing(String content) {
StringTokenizer tokens = new StringTokenizer(content, ":,;");
String idLabel = tokens.nextToken();
String idLabelValue = tokens.nextToken();
String ssidLabel = tokens.nextToken();
String ssid = tokens.nextToken();
String passwordLabel = tokens.nextToken();
String password= tokens.nextToken();
}连接wifi
public static WifiConfiguration createWifiCfg(String ssid, String password, int type)
{
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + ssid + "\"";
if(type == WIFICIPHER_NOPASS){
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
else if(type == WIFICIPHER_WEP){
config.hiddenSSID = true;
config.wepKeys[0]= "\""+password+"\"";
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}else if(type == WIFICIPHER_WPA){
config.preSharedKey = "\""+password+"\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}当您在扫描后收到数据时,将该数据传递给字符串变量并调用contentParsing(data)方法
https://stackoverflow.com/questions/35390829
复制相似问题