Reputation: 1533
In my app, i need to connect to a WiFi after scanning a QR code. How should i go about achieving this?
I tried reading the qr code and was successful in that using https://github.com/ZBar/ZBar/tree/master/android . How should i use the data i get from the QR code to connect to a WiFi? The data i get is in the format: "Id:23,wifiName:wert,wifiPass:12345678"
I found out this library https://github.com/zxing/zxing but i am not able to formulate a method of how to use it?
I don't need the exact code. Just mention the correct method.
Upvotes: 1
Views: 3079
Reputation: 809
You have to tokenize the data which you received from qr code scan. you have data in form like "Id:23,wifiName:wert,wifiPass:12345678"
so in order to tokinze this data , following method will give you the ssid and password in order to connect to the 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();
}
To connect 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;
}
When you receive the data after scanning , pass that data to a string variable and call the method contentParsing(data)
Upvotes: 2