mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Issue 1218 add possible support for hidden SSIDs
git-svn-id: https://zxing.googlecode.com/svn/trunk@2371 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
1acddf28a7
commit
e4803b8799
|
@ -28,7 +28,8 @@ import com.google.zxing.client.result.WifiParsedResult;
|
|||
/**
|
||||
* Handles address book entries.
|
||||
*
|
||||
* @author viki@google.com (Vikram Aggarwal)
|
||||
* @author Vikram Aggarwal
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class WifiResultHandler extends ResultHandler {
|
||||
|
||||
|
@ -52,15 +53,11 @@ public final class WifiResultHandler extends ResultHandler {
|
|||
|
||||
@Override
|
||||
public void handleButtonPress(int index) {
|
||||
// Get the underlying wifi config
|
||||
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
||||
if (index == 0) {
|
||||
String ssid = wifiResult.getSsid();
|
||||
String password = wifiResult.getPassword();
|
||||
String networkType = wifiResult.getNetworkEncryption();
|
||||
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
|
||||
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
|
||||
Toast.makeText(getActivity(), R.string.wifi_changing_network, Toast.LENGTH_LONG).show();
|
||||
WifiConfigManager.configure(wifiManager, ssid, password, networkType);
|
||||
WifiConfigManager.configure(wifiManager, wifiResult);
|
||||
parent.restartPreviewAfterDelay(0L);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,8 @@ import android.util.Log;
|
|||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.google.zxing.client.result.WifiParsedResult;
|
||||
|
||||
/**
|
||||
* @author Vikram Aggarwal
|
||||
* @author Sean Owen
|
||||
|
@ -36,10 +38,7 @@ public final class WifiConfigManager {
|
|||
private WifiConfigManager() {
|
||||
}
|
||||
|
||||
public static void configure(final WifiManager wifiManager,
|
||||
final String ssid,
|
||||
final String password,
|
||||
final String networkTypeString) {
|
||||
public static void configure(final WifiManager wifiManager, final WifiParsedResult wifiResult) {
|
||||
Runnable configureRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
@ -68,6 +67,7 @@ public final class WifiConfigManager {
|
|||
count++;
|
||||
}
|
||||
}
|
||||
String networkTypeString = wifiResult.getNetworkEncryption();
|
||||
NetworkType networkType;
|
||||
try {
|
||||
networkType = NetworkType.forIntentValue(networkTypeString);
|
||||
|
@ -76,15 +76,16 @@ public final class WifiConfigManager {
|
|||
return;
|
||||
}
|
||||
if (networkType == NetworkType.NO_PASSWORD) {
|
||||
changeNetworkUnEncrypted(wifiManager, ssid);
|
||||
changeNetworkUnEncrypted(wifiManager, wifiResult);
|
||||
} else {
|
||||
String password = wifiResult.getPassword();
|
||||
if (password == null || password.length() == 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (networkType == NetworkType.WEP) {
|
||||
changeNetworkWEP(wifiManager, ssid, password);
|
||||
changeNetworkWEP(wifiManager, wifiResult);
|
||||
} else if (networkType == NetworkType.WPA) {
|
||||
changeNetworkWPA(wifiManager, ssid, password);
|
||||
changeNetworkWPA(wifiManager, wifiResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -118,7 +119,7 @@ public final class WifiConfigManager {
|
|||
}
|
||||
}
|
||||
|
||||
private static WifiConfiguration changeNetworkCommon(String ssid) {
|
||||
private static WifiConfiguration changeNetworkCommon(WifiParsedResult wifiResult) {
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
config.allowedAuthAlgorithms.clear();
|
||||
config.allowedGroupCiphers.clear();
|
||||
|
@ -126,14 +127,15 @@ public final class WifiConfigManager {
|
|||
config.allowedPairwiseCiphers.clear();
|
||||
config.allowedProtocols.clear();
|
||||
// Android API insists that an ascii SSID must be quoted to be correctly handled.
|
||||
config.SSID = quoteNonHex(ssid);
|
||||
config.SSID = quoteNonHex(wifiResult.getSsid());
|
||||
config.hiddenSSID = wifiResult.isHidden();
|
||||
return config;
|
||||
}
|
||||
|
||||
// Adding a WEP network
|
||||
private static void changeNetworkWEP(WifiManager wifiManager, String ssid, String password) {
|
||||
WifiConfiguration config = changeNetworkCommon(ssid);
|
||||
config.wepKeys[0] = quoteNonHex(password, 10, 26, 58);
|
||||
private static void changeNetworkWEP(WifiManager wifiManager, WifiParsedResult wifiResult) {
|
||||
WifiConfiguration config = changeNetworkCommon(wifiResult);
|
||||
config.wepKeys[0] = quoteNonHex(wifiResult.getPassword(), 10, 26, 58);
|
||||
config.wepTxKeyIndex = 0;
|
||||
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
|
||||
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
|
||||
|
@ -145,10 +147,10 @@ public final class WifiConfigManager {
|
|||
}
|
||||
|
||||
// Adding a WPA or WPA2 network
|
||||
private static void changeNetworkWPA(WifiManager wifiManager, String ssid, String password) {
|
||||
WifiConfiguration config = changeNetworkCommon(ssid);
|
||||
private static void changeNetworkWPA(WifiManager wifiManager, WifiParsedResult wifiResult) {
|
||||
WifiConfiguration config = changeNetworkCommon(wifiResult);
|
||||
// Hex passwords that are 64 bits long are not to be quoted.
|
||||
config.preSharedKey = quoteNonHex(password, 64);
|
||||
config.preSharedKey = quoteNonHex(wifiResult.getPassword(), 64);
|
||||
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
|
||||
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
|
||||
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
|
||||
|
@ -162,8 +164,8 @@ public final class WifiConfigManager {
|
|||
}
|
||||
|
||||
// Adding an open, unsecured network
|
||||
private static void changeNetworkUnEncrypted(WifiManager wifiManager, String ssid) {
|
||||
WifiConfiguration config = changeNetworkCommon(ssid);
|
||||
private static void changeNetworkUnEncrypted(WifiManager wifiManager, WifiParsedResult wifiResult) {
|
||||
WifiConfiguration config = changeNetworkCommon(wifiResult);
|
||||
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
|
||||
updateNetwork(wifiManager, config);
|
||||
}
|
||||
|
|
|
@ -24,12 +24,18 @@ public final class WifiParsedResult extends ParsedResult {
|
|||
private final String ssid;
|
||||
private final String networkEncryption;
|
||||
private final String password;
|
||||
private final boolean hidden;
|
||||
|
||||
public WifiParsedResult(String networkEncryption, String ssid, String password) {
|
||||
this(networkEncryption, ssid, password, false);
|
||||
}
|
||||
|
||||
public WifiParsedResult(String networkEncryption, String ssid, String password, boolean hidden) {
|
||||
super(ParsedResultType.WIFI);
|
||||
this.ssid = ssid;
|
||||
this.networkEncryption = networkEncryption;
|
||||
this.password = password;
|
||||
this.hidden = hidden;
|
||||
}
|
||||
|
||||
public String getSsid() {
|
||||
|
@ -44,12 +50,18 @@ public final class WifiParsedResult extends ParsedResult {
|
|||
return password;
|
||||
}
|
||||
|
||||
public boolean isHidden() {
|
||||
return hidden;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayResult() {
|
||||
StringBuilder result = new StringBuilder(80);
|
||||
maybeAppend(ssid, result);
|
||||
maybeAppend(networkEncryption, result);
|
||||
maybeAppend(password, result);
|
||||
maybeAppend(Boolean.toString(hidden), result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -19,13 +19,14 @@ package com.google.zxing.client.result;
|
|||
import com.google.zxing.Result;
|
||||
|
||||
/**
|
||||
* Parses a WIFI configuration string. Strings will be of the form:
|
||||
* WIFI:T:WPA;S:mynetwork;P:mypass;;
|
||||
* <p>Parses a WIFI configuration string. Strings will be of the form:</p>
|
||||
*
|
||||
* The fields can come in any order, and there should be tests to see
|
||||
* if we can parse them all correctly.
|
||||
* <p>{@code WIFI:T:[network type];S:[network SSID];P:[network password];H:[hidden?];;}</p>
|
||||
*
|
||||
* <p>The fields can appear in any order. Only "S:" is required.</p>
|
||||
*
|
||||
* @author Vikram Aggarwal
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class WifiResultParser extends ResultParser {
|
||||
|
||||
|
@ -35,18 +36,16 @@ public final class WifiResultParser extends ResultParser {
|
|||
if (!rawText.startsWith("WIFI:")) {
|
||||
return null;
|
||||
}
|
||||
// Don't remove leading or trailing whitespace
|
||||
boolean trim = false;
|
||||
String ssid = matchSinglePrefixedField("S:", rawText, ';', trim);
|
||||
String ssid = matchSinglePrefixedField("S:", rawText, ';', false);
|
||||
if (ssid == null || ssid.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
String pass = matchSinglePrefixedField("P:", rawText, ';', trim);
|
||||
String type = matchSinglePrefixedField("T:", rawText, ';', trim);
|
||||
String pass = matchSinglePrefixedField("P:", rawText, ';', false);
|
||||
String type = matchSinglePrefixedField("T:", rawText, ';', false);
|
||||
if (type == null) {
|
||||
type = "nopass";
|
||||
}
|
||||
|
||||
return new WifiParsedResult(type, ssid, pass);
|
||||
boolean hidden = Boolean.parseBoolean(matchSinglePrefixedField("B:", rawText, ';', false));
|
||||
return new WifiParsedResult(type, ssid, pass, hidden);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue