Android 连接匿名WiFi的示例代码

前言

因为开发需要在应用内部实现wifi连接,结合网上的资料,实现连接wifi的还是比较简单,但是对于连接匿名wifi,却鲜有提及,所以在此分享下。

基本使用

首先介绍下wifi开发相关的一些基础概念和工具类等,如果对wifi已经有过接触的同学可以直接跳过看下一节。

1.权限

Android中要使用系统功能一般都要申请权限,这里wifi需要的权限有

1
2
3
4
5
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> // 需要系统权限 [定位权限]

因为可以利用wifi进行定位,所以这里需要申请定位权限,在6.0以上设备,定位权限需要主动申请。

2.API

 

类名 功能
WifiManager wifi统一管理类,进行各种wifi操作
WifiInfo 描述当前连接的wifi热点信息
WifiConfiguration wifi网络配置信息
ScanResult 描述扫描出的wifi热点的信息

 

WifiManager类是framework层暴露的api,用来管理wifi。

1
val mWifiManager = mContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

通过他可以得到:1.已经配置的网络列表。2.当前连接的wifi。3.扫描到的wifi。4.以及一些常量表示广播的意图等

ScanResult类用于存放wifi扫描结果信息,主要有以下内容: 

 

属性 描述
SSID 描述wifi热点的名称,就是大家搜索到的直接名称,如ChinaNet
BSSID 姑且理解成热点的mac地址,但实际有所不同
networkID 数字型的id
level 描述wifi信号强弱的值,值是负数,绝对值越小,信号越强
capabilities 如加密方式,如WEP

 

3.监听设备wifi状态的改变

wifi状态的改变是会导致广播事件的发生。

1
2
3
val filter = IntentFilter()
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION) //监听wifi状态改变
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) //监听扫描到wifi列表改变
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private val mReceiver = object : BroadcastReceiver() {
 
    override fun onReceive(context: Context, intent: Intent) {
      val action = intent.action
      if (TextUtils.isEmpty(action)) return
      when (action) {
        WifiManager.WIFI_STATE_CHANGED_ACTION -> {
          
        }
        WifiManager.SCAN_RESULTS_AVAILABLE_ACTION -> {
           
        }
      }
    }
  }

WifiManager之中有当前状态的enum类型,可以看下表:

 

名称 描述
WIFI_STATE_DISABLING wifi正在关闭
WIFI_STATE_DISABLED wifi关闭
WIFI_STATE_ENABLING wifi正在开启
WIFI_STATE_ENABLED wifi开启
WIFI_STATE_UNKNOWN wifi未知

 

连接普通wifi

连接wifi我大致分为以下几步:

  • 获取想要连接WiFi热点的SSID、加密方式信息,和用户输入的密码
  • 根据上述信息来创建wifiConfigruation对象
  • 调用WifiManager的方法,传入wifiConfigruation,完成wifi连接
1
2
3
4
5
6
7
public static void connectNewWifi(Context mContext, WifiConfiguration wifiConfiguration) {
     
    WifiManager wifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    int networkId = wifiManager.addNetwork(wifiConfiguration);
    wifiManager.enableNetwork(networkId, true);
 
  }

所以重点是怎样创建wifiConfigruation,不同的加密方式的wifi,创建过程也不太一样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public WifiConfiguration createWifiConfig(String SSID, @WifiCipherType int wifiCipherType, String password) {
   WifiConfiguration wifiConfiguration = new WifiConfiguration();
   wifiConfiguration.SSID = convertToQuotedString(SSID);
 
   switch (wifiCipherType) {
     case WifiCipherType.SECURITY_NONE:
       wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
       break;
     case WifiCipherType.SECURITY_WEP:
       wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
       wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
       wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
       if (!TextUtils.isEmpty(password)) {
         int length = password.length();
         // WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
         if ((length == 10 || length == 26 || length == 58)
             && password.matches("[0-9A-Fa-f]*")) {
           wifiConfiguration.wepKeys[0] = password;
         } else {
           wifiConfiguration.wepKeys[0] = ‘"‘ + password + ‘"‘;
         }
       }
       break;
 
     case WifiCipherType.SECURITY_PSK:
       wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
       if (!TextUtils.isEmpty(password)) {
         if (password.matches("[0-9A-Fa-f]{64}")) {
           wifiConfiguration.preSharedKey = password;
         } else {
           wifiConfiguration.preSharedKey = ‘"‘ + password + ‘"‘;
         }
       }
       break;
 
     case WifiCipherType.SECURITY_EAP:
       wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
       wifiConfiguration.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
       wifiConfiguration.enterpriseConfig = new WifiEnterpriseConfig();
       int eapMethod = 0;
       int phase2Method = 0;
       wifiConfiguration.enterpriseConfig.setEapMethod(eapMethod);
       wifiConfiguration.enterpriseConfig.setPhase2Method(phase2Method);
       if (!TextUtils.isEmpty(password)) {
         wifiConfiguration.enterpriseConfig.setPassword(password);
       }
       break;
     default:
       break;
   }
   return wifiConfiguration;
 }

至此wifi就连接完成了,然后可以在广播中获取连接结果。相应的wifi配置信息会被保存在/data/misc/wifi/wpa_supplicant.conf中:

1
2
3
4
5
6
7
network={
    ssid="test"
    psk="88888888"
    key_mgmt=WPA-PSK
    disabled=1
    id_str="%7B%22creatorUid%22%3A%221000%22%2C%22configKey%22%3A%22%5C%22test%5C%22WPA_PSK%22%7D"
}

连接匿名wifi

匿名wifi相较于普通wifi,不同之处在于不会广播其SSID,所以就不能被直接扫描到,需要我们输入wifi的SSID来主动进行扫描,先来看下匿名wifi的配置信息:

1
2
3
4
5
6
7
8
9
network={                                               
    ssid="test2"                                         
    scan_ssid=1                                          
    bssid=56:28:f8:fa:f8:a0                                    
    psk="44444444"                                        
    key_mgmt=WPA-PSK                                       
    disabled=1                                          
    id_str="%7B%22creatorUid%22%3A%221000%22%2C%22configKey%22%3A%22%5C%22test2%5C%22WPA_PSK%22%7D"
}

可以看到,多了一个scan_ssid属性,查看WifiConfiguration,确实有一个属性可以设置

1
2
3
4
5
/**
   * This is a network that does not broadcast its SSID, so an
   * SSID-specific probe request must be used for scans.
   */
  public boolean hiddenSSID;

所以在创建WifiConfiguration的时候多设置下这个属性就行了:

1
configuration.hiddenSSID = true

Android 连接匿名WiFi的示例代码

上一篇:iOS Workflow 分享 - Create QR Code


下一篇:ECC椭圆曲线算法-1