Android WIFI模块解析

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

Java代码   收藏代码
  1. private static final Pattern scanResultPattern = Pattern.compile("\t+");  
  2. /** 
  3.      * Parse the scan result line passed to us by wpa_supplicant (helper). 
  4.      * @param line the line to parse 
  5.      * @return the {@link ScanResult} object 
  6.      */  
  7.     private ScanResult parseScanResult(String line) {  
  8.         ScanResult scanResult = null;  
  9.         if (line != null) {  
  10.             /* 
  11.              * Cache implementation (LinkedHashMap) is not synchronized, thus, 
  12.              * must synchronized here! 
  13.              */  
  14.             synchronized (mScanResultCache) {  
  15.                 String[] result = scanResultPattern.split(line);  
  16.                 if (3 <= result.length && result.length <= 5) {  
  17.                     String bssid = result[0];  
  18.                     // bssid | frequency | level | flags | ssid  
  19.                     int frequency;  
  20.                     int level;  
  21.                     try {  
  22.                         frequency = Integer.parseInt(result[1]);  
  23.                         level = Integer.parseInt(result[2]);  
  24.                         /* some implementations avoid negative values by adding 256 
  25.                          * so we need to adjust for that here. 
  26.                          */  
  27.                         if (level > 0) level -= 256;  
  28.                     } catch (NumberFormatException e) {  
  29.                         frequency = 0;  
  30.                         level = 0;  
  31.                     }  
  32.   
  33.                     /* 
  34.                      * The formatting of the results returned by 
  35.                      * wpa_supplicant is intended to make the fields 
  36.                      * line up nicely when printed, 
  37.                      * not to make them easy to parse. So we have to 
  38.                      * apply some heuristics to figure out which field 
  39.                      * is the SSID and which field is the flags. 
  40.                      */  
  41.                     String ssid;  
  42.                     String flags;  
  43.                     if (result.length == 4) {  
  44.                         if (result[3].charAt(0) == '[') {  
  45.                             flags = result[3];  
  46.                             ssid = "";  
  47.                         } else {  
  48.                             flags = "";  
  49.                             ssid = result[3];  
  50.                         }  
  51.                     } else if (result.length == 5) {  
  52.                         flags = result[3];  
  53.                         ssid = result[4];  
  54.                     } else {  
  55.                         // Here, we must have 3 fields: no flags and ssid  
  56.                         // set  
  57.                         flags = "";  
  58.                         ssid = "";  
  59.                     }  
  60.   
  61.                     // bssid + ssid is the hash key  
  62.                     String key = bssid + ssid;  
  63.                     scanResult = mScanResultCache.get(key);  
  64.                     if (scanResult != null) {  
  65.                         scanResult.level = level;  
  66.                         scanResult.SSID = ssid;  
  67.                         scanResult.capabilities = flags;  
  68.                         scanResult.frequency = frequency;  
  69.                     } else {  
  70.                         // Do not add scan results that have no SSID set  
  71.                         if (0 < ssid.trim().length()) {  
  72.                             scanResult =  
  73.                                 new ScanResult(  
  74.                                     ssid, bssid, flags, level, frequency);  
  75.                             mScanResultCache.put(key, scanResult);  
  76.                         }  
  77.                     }  
  78.                 } else {  
  79.                     Slog.w(TAG, "Misformatted scan result text with " +  
  80.                           result.length + " fields: " + line);  
  81.                 }  
  82.             }  
  83.         }  
  84.   
  85.         return scanResult;  
  86.     }  


WIFI添加一个网络配置,返回生成的网络ID 
Java代码   收藏代码
  1. /** 
  2.      * see {@link android.net.wifi.WifiManager#addOrUpdateNetwork(WifiConfiguration)} 
  3.      * @return the supplicant-assigned identifier for the new or updated 
  4.      * network if the operation succeeds, or {@code -1} if it fails 
  5.      */  
  6.     public int addOrUpdateNetwork(WifiConfiguration config) {  
  7.         enforceChangePermission();  
  8.   
  9.         /* 
  10.          * If the supplied networkId is -1, we create a new empty 
  11.          * network configuration. Otherwise, the networkId should 
  12.          * refer to an existing configuration. 
  13.          */  
  14.         int netId = config.networkId;  
  15.         boolean newNetwork = netId == -1;  
  16.         boolean doReconfig = false;  
  17.         // networkId of -1 means we want to create a new network  
  18.         synchronized (mWifiStateTracker) {  
  19.             if (newNetwork) {  
  20.                 netId = mWifiStateTracker.addNetwork();  
  21.                 if (netId < 0) {  
  22.                     if (DBG) {  
  23.                         Slog.d(TAG, "Failed to add a network!");  
  24.                     }  
  25.                     return -1;  
  26.                 }  
  27.                 doReconfig = true;  
  28.             }  
  29.             mNeedReconfig = mNeedReconfig || doReconfig;  
  30.         }  
  31.   
  32.         setVariables: {  
  33.             /* 
  34.              * Note that if a networkId for a non-existent network 
  35.              * was supplied, then the first setNetworkVariable() 
  36.              * will fail, so we don't bother to make a separate check 
  37.              * for the validity of the ID up front. 
  38.              */  
  39.             if (config.SSID != null &&  
  40.                     !mWifiStateTracker.setNetworkVariable(  
  41.                         netId,  
  42.                         WifiConfiguration.ssidVarName,  
  43.                         config.SSID)) {  
  44.                 if (DBG) {  
  45.                     Slog.d(TAG, "failed to set SSID: "+config.SSID);  
  46.                 }  
  47.                 break setVariables;  
  48.             }  
  49.   
  50.             if (config.BSSID != null &&  
  51.                     !mWifiStateTracker.setNetworkVariable(  
  52.                         netId,  
  53.                         WifiConfiguration.bssidVarName,  
  54.                         config.BSSID)) {  
  55.                 if (DBG) {  
  56.                     Slog.d(TAG, "failed to set BSSID: "+config.BSSID);  
  57.                 }  
  58.                 break setVariables;  
  59.             }  
  60.   
  61.             String allowedKeyManagementString =  
  62.                 makeString(config.allowedKeyManagement, WifiConfiguration.KeyMgmt.strings);  
  63.             if (config.allowedKeyManagement.cardinality() != 0 &&  
  64.                     !mWifiStateTracker.setNetworkVariable(  
  65.                         netId,  
  66.                         WifiConfiguration.KeyMgmt.varName,  
  67.                         allowedKeyManagementString)) {  
  68.                 if (DBG) {  
  69.                     Slog.d(TAG, "failed to set key_mgmt: "+  
  70.                             allowedKeyManagementString);  
  71.                 }  
  72.                 break setVariables;  
  73.             }  
  74.   
  75.             String allowedProtocolsString =  
  76.                 makeString(config.allowedProtocols, WifiConfiguration.Protocol.strings);  
  77.             if (config.allowedProtocols.cardinality() != 0 &&  
  78.                     !mWifiStateTracker.setNetworkVariable(  
  79.                         netId,  
  80.                         WifiConfiguration.Protocol.varName,  
  81.                         allowedProtocolsString)) {  
  82.                 if (DBG) {  
  83.                     Slog.d(TAG, "failed to set proto: "+  
  84.                             allowedProtocolsString);  
  85.                 }  
  86.                 break setVariables;  
  87.             }  
  88.   
  89.             String allowedAuthAlgorithmsString =  
  90.                 makeString(config.allowedAuthAlgorithms, WifiConfiguration.AuthAlgorithm.strings);  
  91.             if (config.allowedAuthAlgorithms.cardinality() != 0 &&  
  92.                     !mWifiStateTracker.setNetworkVariable(  
  93.                         netId,  
  94.                         WifiConfiguration.AuthAlgorithm.varName,  
  95.                         allowedAuthAlgorithmsString)) {  
  96.                 if (DBG) {  
  97.                     Slog.d(TAG, "failed to set auth_alg: "+  
  98.                             allowedAuthAlgorithmsString);  
  99.                 }  
  100.                 break setVariables;  
  101.             }  
  102.   
  103.             String allowedPairwiseCiphersString =  
  104.                 makeString(config.allowedPairwiseCiphers, WifiConfiguration.PairwiseCipher.strings);  
  105.             if (config.allowedPairwiseCiphers.cardinality() != 0 &&  
  106.                     !mWifiStateTracker.setNetworkVariable(  
  107.                         netId,  
  108.                         WifiConfiguration.PairwiseCipher.varName,  
  109.                         allowedPairwiseCiphersString)) {  
  110.                 if (DBG) {  
  111.                     Slog.d(TAG, "failed to set pairwise: "+  
  112.                             allowedPairwiseCiphersString);  
  113.                 }  
  114.                 break setVariables;  
  115.             }  
  116.   
  117.             String allowedGroupCiphersString =  
  118.                 makeString(config.allowedGroupCiphers, WifiConfiguration.GroupCipher.strings);  
  119.             if (config.allowedGroupCiphers.cardinality() != 0 &&  
  120.                     !mWifiStateTracker.setNetworkVariable(  
  121.                         netId,  
  122.                         WifiConfiguration.GroupCipher.varName,  
  123.                         allowedGroupCiphersString)) {  
  124.                 if (DBG) {  
  125.                     Slog.d(TAG, "failed to set group: "+  
  126.                             allowedGroupCiphersString);  
  127.                 }  
  128.                 break setVariables;  
  129.             }  
  130.   
  131.             // Prevent client screw-up by passing in a WifiConfiguration we gave it  
  132.             // by preventing "*" as a key.  
  133.             if (config.preSharedKey != null && !config.preSharedKey.equals("*") &&  
  134.                     !mWifiStateTracker.setNetworkVariable(  
  135.                         netId,  
  136.                         WifiConfiguration.pskVarName,  
  137.                         config.preSharedKey)) {  
  138.                 if (DBG) {  
  139.                     Slog.d(TAG, "failed to set psk: "+config.preSharedKey);  
  140.                 }  
  141.                 break setVariables;  
  142.             }  
  143.   
  144.             boolean hasSetKey = false;  
  145.             if (config.wepKeys != null) {  
  146.                 for (int i = 0; i < config.wepKeys.length; i++) {  
  147.                     // Prevent client screw-up by passing in a WifiConfiguration we gave it  
  148.                     // by preventing "*" as a key.  
  149.                     if (config.wepKeys[i] != null && !config.wepKeys[i].equals("*")) {  
  150.                         if (!mWifiStateTracker.setNetworkVariable(  
  151.                                     netId,  
  152.                                     WifiConfiguration.wepKeyVarNames[i],  
  153.                                     config.wepKeys[i])) {  
  154.                             if (DBG) {  
  155.                                 Slog.d(TAG,  
  156.                                         "failed to set wep_key"+i+": " +  
  157.                                         config.wepKeys[i]);  
  158.                             }  
  159.                             break setVariables;  
  160.                         }  
  161.                         hasSetKey = true;  
  162.                     }  
  163.                 }  
  164.             }  
  165.   
  166.             if (hasSetKey) {  
  167.                 if (!mWifiStateTracker.setNetworkVariable(  
  168.                             netId,  
  169.                             WifiConfiguration.wepTxKeyIdxVarName,  
  170.                             Integer.toString(config.wepTxKeyIndex))) {  
  171.                     if (DBG) {  
  172.                         Slog.d(TAG,  
  173.                                 "failed to set wep_tx_keyidx: "+  
  174.                                 config.wepTxKeyIndex);  
  175.                     }  
  176.                     break setVariables;  
  177.                 }  
  178.             }  
  179.   
  180.             if (!mWifiStateTracker.setNetworkVariable(  
  181.                         netId,  
  182.                         WifiConfiguration.priorityVarName,  
  183.                         Integer.toString(config.priority))) {  
  184.                 if (DBG) {  
  185.                     Slog.d(TAG, config.SSID + ": failed to set priority: "  
  186.                             +config.priority);  
  187.                 }  
  188.                 break setVariables;  
  189.             }  
  190.   
  191.             if (config.hiddenSSID && !mWifiStateTracker.setNetworkVariable(  
  192.                         netId,  
  193.                         WifiConfiguration.hiddenSSIDVarName,  
  194.                         Integer.toString(config.hiddenSSID ? 1 : 0))) {  
  195.                 if (DBG) {  
  196.                     Slog.d(TAG, config.SSID + ": failed to set hiddenSSID: "+  
  197.                             config.hiddenSSID);  
  198.                 }  
  199.                 break setVariables;  
  200.             }  
  201.   
  202.             for (WifiConfiguration.EnterpriseField field  
  203.                     : config.enterpriseFields) {  
  204.                 String varName = field.varName();  
  205.                 String value = field.value();  
  206.                 if (value != null) {  
  207.                     if (field != config.eap) {  
  208.                         value = (value.length() == 0) ? "NULL" : convertToQuotedString(value);  
  209.                     }  
  210.                     if (!mWifiStateTracker.setNetworkVariable(  
  211.                                 netId,  
  212.                                 varName,  
  213.                                 value)) {  
  214.                         if (DBG) {  
  215.                             Slog.d(TAG, config.SSID + ": failed to set " + varName +  
  216.                                     ": " + value);  
  217.                         }  
  218.                         break setVariables;  
  219.                     }  
  220.                 }  
  221.             }  
  222.             return netId;  
  223.         }  


认证类型管理类: 

Java代码   收藏代码
  1. /** 
  2.      * Recognized key management schemes. 
  3.      */  
  4.     public static class KeyMgmt {  
  5.         private KeyMgmt() { }  
  6.   
  7.         /** WPA is not used; plaintext or static WEP could be used. */  
  8.         public static final int NONE = 0;  
  9.         /** WPA pre-shared key (requires {@code preSharedKey} to be specified). */  
  10.         public static final int WPA_PSK = 1;  
  11.         /** WPA using EAP authentication. Generally used with an external authentication server. */  
  12.         public static final int WPA_EAP = 2;  
  13.         /** IEEE 802.1X using EAP authentication and (optionally) dynamically 
  14.          * generated WEP keys. */  
  15.         public static final int IEEE8021X = 3;  
  16.   
  17.         public static final String varName = "key_mgmt";  
  18.   
  19.         public static final String[] strings = { "NONE""WPA_PSK""WPA_EAP""IEEE8021X" };  
  20.     }  
相关文章
|
5天前
|
Android开发
Android 状态栏WiFi图标的显示逻辑
Android 状态栏WiFi图标的显示逻辑
20 0
|
5天前
|
Java Android开发
Android12 双击power键启动相机源码解析
Android12 双击power键启动相机源码解析
15 0
|
1天前
|
网络协议 物联网
|
6天前
|
Android开发
android连接指定wifi
android连接指定wifi
14 0
|
6天前
|
Android开发
Android 集成vendor下的模块
Android 集成vendor下的模块
11 0
|
6天前
|
Java Android开发
Android 9在连接以太网情况下 还能连接WiFi
Android 9在连接以太网情况下 还能连接WiFi
9 0
|
6天前
|
Android开发
Android12 ethernet和wifi共存
Android12 ethernet和wifi共存
15 0
|
6天前
|
Java Shell Android开发
Android11 有线网和wifi优先级设置
Android11 有线网和wifi优先级设置
11 0
|
6天前
|
Java Android开发 开发者
rk3399 android以太网和wifi共存
rk3399 android以太网和wifi共存
12 0
|
2天前
PandasTA 源码解析(二十三)
PandasTA 源码解析(二十三)
7 0

推荐镜像

更多