Android WIFI模块解析
16lz
2021-01-23
Android中的AP扫描结果,通过WifiNative的JNI调用,实际调用wpa_supplicant的相应命令,并返回字符串给JNI方法,下面是对该字符串的解析函数:
WIFI添加一个网络配置,返回生成的网络ID
认证类型管理类:
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" }; }
更多相关文章
- 使用 Android快速开发框架 Afinal 0.3 快速开发网络应用相关APK
- Android最基本的异步网络请求框架
- Android 判断网络状态,并且在没有网络的时候,打开网络设置对话框
- Android的http网络请求和发送
- 网络对讲机C#服务器 Android客户端(三) android客户端代码分析 附
- android中做网络请求的几种方式
- Android 网络框架 Retrofit 源码解析
- Android 判断网络状态,并且在没有网络的时候,提示网络未开启
- Android 网络请求简单使用方式