Android WIFI模块解析

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介:
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.     }  
目录
打赏
0
0
0
0
66
分享
相关文章
Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势
本文深入探讨了 Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势。这对于实现高效的跨平台移动应用开发具有重要指导意义。
621 4
Android与iOS开发环境搭建全解析####
本文深入探讨了Android与iOS两大移动操作系统的开发环境搭建流程,旨在为初学者及有一定基础的开发者提供详尽指南。我们将从开发工具的选择、环境配置到第一个简单应用的创建,一步步引导读者步入移动应用开发的殿堂。无论你是Android Studio的新手还是Xcode的探索者,本文都将为你扫清开发道路上的障碍,助你快速上手并享受跨平台移动开发的乐趣。 ####
【Android】网络技术知识总结之WebView,HttpURLConnection,OKHttp,XML的pull解析方式
本文总结了Android中几种常用的网络技术,包括WebView、HttpURLConnection、OKHttp和XML的Pull解析方式。每种技术都有其独特的特点和适用场景。理解并熟练运用这些技术,可以帮助开发者构建高效、可靠的网络应用程序。通过示例代码和详细解释,本文为开发者提供了实用的参考和指导。
69 15
Android调试终极指南:ADB安装+多设备连接+ANR日志抓取全流程解析,覆盖环境变量配置/多设备调试/ANR日志分析全流程,附Win/Mac/Linux三平台解决方案
ADB(Android Debug Bridge)是安卓开发中的重要工具,用于连接电脑与安卓设备,实现文件传输、应用管理、日志抓取等功能。本文介绍了 ADB 的基本概念、安装配置及常用命令。包括:1) 基本命令如 `adb version` 和 `adb devices`;2) 权限操作如 `adb root` 和 `adb shell`;3) APK 操作如安装、卸载应用;4) 文件传输如 `adb push` 和 `adb pull`;5) 日志记录如 `adb logcat`;6) 系统信息获取如屏幕截图和录屏。通过这些功能,用户可高效调试和管理安卓设备。
深入探索Android系统架构:从内核到应用层的全面解析
本文旨在为读者提供一份详尽的Android系统架构分析,从底层的Linux内核到顶层的应用程序框架。我们将探讨Android系统的模块化设计、各层之间的交互机制以及它们如何共同协作以支持丰富多样的应用生态。通过本篇文章,开发者和爱好者可以更深入理解Android平台的工作原理,从而优化开发流程和提升应用性能。
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。
安卓与iOS的跨平台开发:Flutter框架深度解析
在移动应用开发的海洋中,Flutter作为一艘灵活的帆船,正引领着开发者们驶向跨平台开发的新纪元。本文将揭开Flutter神秘的面纱,从其架构到核心特性,再到实际应用案例,我们将一同探索这个由谷歌打造的开源UI工具包如何让安卓与iOS应用开发变得更加高效而统一。你将看到,借助Flutter,打造精美、高性能的应用不再是难题,而是变成了一场创造性的旅程。
|
5月前
|
深入解析Android系统架构及其对开发者的意义####
【10月更文挑战第21天】 本文旨在为读者揭开Android操作系统架构的神秘面纱,探讨其如何塑造现代移动应用开发格局。通过剖析Linux内核、硬件抽象层、运行时环境及应用程序框架等关键组件,揭示Android平台的强大功能与灵活性。文章强调了理解Android架构对于开发者优化应用性能、提升用户体验的重要性,并展望了未来技术趋势下Android的发展方向。 ####
131 0
安卓与iOS的较量:技术深度解析
【10月更文挑战第24天】 在移动操作系统领域,安卓和iOS无疑是两大巨头。本文将深入探讨这两个系统的技术特点、优势和不足,以及它们在未来可能的发展方向。我们将通过对比分析,帮助读者更好地理解这两个系统的本质和内涵,从而引发对移动操作系统未来发展的深思。
109 0
前端模块打包器的深度解析
【10月更文挑战第13天】前端模块打包器的深度解析
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等