Android WIFI模块解析

Android中的AP扫描结果,通过WifiNative的JNI调用,实际调用wpa_supplicant的相应命令,并返回字符串给JNI方法,下面是对该字符串的解析函数:


private static final Pattern scanResultPattern = Pattern.compile("\t+");
/**
* Parse the scan result line passed to us by wpa_supplicant (helper).
* @param line the line to parse
* @return the {@link ScanResult} object
*/
private ScanResult parseScanResult(String line) {
ScanResult scanResult = null;
if (line != null) {
/*
* Cache implementation (LinkedHashMap) is not synchronized, thus,
* must synchronized here!
*/
synchronized (mScanResultCache) {
String[] result = scanResultPattern.split(line);
if (3 <= result.length && result.length <= 5) {
String bssid = result[0];
// bssid | frequency | level | flags | ssid
int frequency;
int level;
try {
frequency = Integer.parseInt(result[1]);
level = Integer.parseInt(result[2]);
/* some implementations avoid negative values by adding 256
* so we need to adjust for that here.
*/
if (level > 0) level -= 256;
} catch (NumberFormatException e) {
frequency = 0;
level = 0;
}

/*
* The formatting of the results returned by
* wpa_supplicant is intended to make the fields
* line up nicely when printed,
* not to make them easy to parse. So we have to
* apply some heuristics to figure out which field
* is the SSID and which field is the flags.
*/
String ssid;
String flags;
if (result.length == 4) {
if (result[3].charAt(0) == '[') {
flags = result[3];
ssid = "";
} else {
flags = "";
ssid = result[3];
}
} else if (result.length == 5) {
flags = result[3];
ssid = result[4];
} else {
// Here, we must have 3 fields: no flags and ssid
// set
flags = "";
ssid = "";
}

// bssid + ssid is the hash key
String key = bssid + ssid;
scanResult = mScanResultCache.get(key);
if (scanResult != null) {
scanResult.level = level;
scanResult.SSID = ssid;
scanResult.capabilities = flags;
scanResult.frequency = frequency;
} else {
// Do not add scan results that have no SSID set
if (0 < ssid.trim().length()) {
scanResult =
new ScanResult(
ssid, bssid, flags, level, frequency);
mScanResultCache.put(key, scanResult);
}
}
} else {
Slog.w(TAG, "Misformatted scan result text with " +
result.length + " fields: " + line);
}
}
}

return scanResult;
}


WIFI添加一个网络配置,返回生成的网络ID

/**
* see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)}
* @return the supplicant-assigned identifier for the new or updated
* network if the operation succeeds, or {@code -1} if it fails
*/
public int addOrUpdateNetwork(WifiConfiguration config) {
enforceChangePermission();

/*
* If the supplied networkId is -1, we create a new empty
* network configuration. Otherwise, the networkId should
* refer to an existing configuration.
*/
int netId = config.networkId;
boolean newNetwork = netId == -1;
boolean doReconfig = false;
// networkId of -1 means we want to create a new network
synchronized (mWifiStateTracker) {
if (newNetwork) {
netId = mWifiStateTracker.addNetwork();
if (netId < 0) {
if (DBG) {
Slog.d(TAG, "Failed to add a network!");
}
return -1;
}
doReconfig = true;
}
mNeedReconfig = mNeedReconfig || doReconfig;
}

setVariables: {
/*
* Note that if a networkId for a non-existent network
* was supplied, then the first setNetworkVariable()
* will fail, so we don't bother to make a separate check
* for the validity of the ID up front.
*/
if (config.SSID != null &&
!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.ssidVarName,
config.SSID)) {
if (DBG) {
Slog.d(TAG, "failed to set SSID: "+config.SSID);
}
break setVariables;
}

if (config.BSSID != null &&
!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.bssidVarName,
config.BSSID)) {
if (DBG) {
Slog.d(TAG, "failed to set BSSID: "+config.BSSID);
}
break setVariables;
}

String allowedKeyManagementString =
makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings);
if (config.allowedKeyManagement.cardinality() != 0 &&
!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.KeyMgmt.varName,
allowedKeyManagementString)) {
if (DBG) {
Slog.d(TAG, "failed to set key_mgmt: "+
allowedKeyManagementString);
}
break setVariables;
}

String allowedProtocolsString =
makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings);
if (config.allowedProtocols.cardinality() != 0 &&
!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.Protocol.varName,
allowedProtocolsString)) {
if (DBG) {
Slog.d(TAG, "failed to set proto: "+
allowedProtocolsString);
}
break setVariables;
}

String allowedAuthAlgorithmsString =
makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings);
if (config.allowedAuthAlgorithms.cardinality() != 0 &&
!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.AuthAlgorithm.varName,
allowedAuthAlgorithmsString)) {
if (DBG) {
Slog.d(TAG, "failed to set auth_alg: "+
allowedAuthAlgorithmsString);
}
break setVariables;
}

String allowedPairwiseCiphersString =
makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings);
if (config.allowedPairwiseCiphers.cardinality() != 0 &&
!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.PairwiseCipher.varName,
allowedPairwiseCiphersString)) {
if (DBG) {
Slog.d(TAG, "failed to set pairwise: "+
allowedPairwiseCiphersString);
}
break setVariables;
}

String allowedGroupCiphersString =
makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings);
if (config.allowedGroupCiphers.cardinality() != 0 &&
!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.GroupCipher.varName,
allowedGroupCiphersString)) {
if (DBG) {
Slog.d(TAG, "failed to set group: "+
allowedGroupCiphersString);
}
break setVariables;
}

// Prevent client screw-up by passing in a WifiConfiguration we gave it
// by preventing "*" as a key.
if (config.preSharedKey != null && !config.preSharedKey.equals("*") &&
!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.pskVarName,
config.preSharedKey)) {
if (DBG) {
Slog.d(TAG, "failed to set psk: "+config.preSharedKey);
}
break setVariables;
}

boolean hasSetKey = false;
if (config.wepKeys != null) {
for (int i = 0; i < config.wepKeys.length; i++) {
// Prevent client screw-up by passing in a WifiConfiguration we gave it
// by preventing "*" as a key.
if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) {
if (!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.wepKeyVarNames[i],
config.wepKeys[i])) {
if (DBG) {
Slog.d(TAG,
"failed to set wep_key"+i+": " +
config.wepKeys[i]);
}
break setVariables;
}
hasSetKey = true;
}
}
}

if (hasSetKey) {
if (!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.wepTxKeyIdxVarName,
Integer.toString(config.wepTxKeyIndex))) {
if (DBG) {
Slog.d(TAG,
"failed to set wep_tx_keyidx: "+
config.wepTxKeyIndex);
}
break setVariables;
}
}

if (!mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.priorityVarName,
Integer.toString(config.priority))) {
if (DBG) {
Slog.d(TAG, config.SSID + ": failed to set priority: "
+config.priority);
}
break setVariables;
}

if (config.hiddenSSID && !mWifiStateTracker.setNetworkVariable(
netId,
WifiConfiguration.hiddenSSIDVarName,
Integer.toString(config.hiddenSSID ? 1 : 0))) {
if (DBG) {
Slog.d(TAG, config.SSID + ": failed to set hiddenSSID: "+
config.hiddenSSID);
}
break setVariables;
}

for (WifiConfiguration.EnterpriseField field
: config.enterpriseFields) {
String varName = field.varName();
String value = field.value();
if (value != null) {
if (field != config.eap) {
value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);
}
if (!mWifiStateTracker.setNetworkVariable(
netId,
varName,
value)) {
if (DBG) {
Slog.d(TAG, config.SSID + ": failed to set " + varName +
": " + value);
}
break setVariables;
}
}
}
return netId;
}


认证类型管理类:


/**
* Recognized key management schemes.
*/
public static class KeyMgmt {
private KeyMgmt() { }

/** WPA is not used; plaintext or static WEP could be used. */
public static final int NONE = 0;
/** WPA pre-shared key (requires {@code preSharedKey} to be specified). */
public static final int WPA_PSK = 1;
/** WPA using EAP authentication. Generally used with an external authentication server. */
public static final int WPA_EAP = 2;
/** IEEE 802.1X using EAP authentication and (optionally) dynamically
* generated WEP keys. */
public static final int IEEE8021X = 3;

public static final String varName = "key_mgmt";

public static final String[] strings = { "NONE", "WPA_PSK", "WPA_EAP", "IEEE8021X" };
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值