实现wifi条形码扫描仪

时间:2016-02-14 10:43:15

标签: android qr-code

在我的应用中,我需要在扫描QR码后连接到WiFi。我应该如何实现这一目标?

我尝试阅读qr代码并使用https://github.com/ZBar/ZBar/tree/master/android取得了成功。我应该如何使用从QR码获得的数据连接到WiFi?我得到的数据格式为:“Id:23,wifiName:wert,wifiPass:12345678”

我发现了这个库https://github.com/zxing/zxing,但我无法制定如何使用它的方法?

我不需要确切的代码。只需提及正确的方法。

1 个答案:

答案 0 :(得分:2)

您必须标记从二维码扫描中收到的数据。您拥有"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)