服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android WiFi热点开发的示例代码

Android WiFi热点开发的示例代码

2022-10-31 14:02大头呆 Android

这篇文章主要介绍了Android WiFi热点开发的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

上次写了Android连接匿名WiFi的内容。WiFI开发对于应用层开发是比较小众的知识点,不过既然用到了就在此记录下。

创建热点

1、根据加密类型、密码、是否隐藏等参数来创建热点

?
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
53
static WifiConfiguration createWifiConfig(String SSID, @WifiSecurityType int wifiCipherType, String password, boolean hiddenSSID) {
 
   WifiConfiguration wifiConfiguration = new WifiConfiguration();
   wifiConfiguration.SSID = convertToQuotedString(SSID);
   wifiConfiguration.hiddenSSID=hiddenSSID;//是否隐藏热点true=隐藏,如果隐藏需要其他设备手动添加网络
   switch (wifiCipherType) {
     case WifiSecurityType.SECURITY_NONE:
       wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
       break;
     case WifiSecurityType.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 WifiSecurityType.SECURITY_WPA_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 WifiSecurityType.SECURITY_WPA_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;
 }

然后调用WifiManager的setWifiApEnabled方法来设置wifiConfiguration,因为是隐藏的,需要通过反射:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
try {
     Method method = mWifManager.getClass().getMethod(
         "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
     boolean enable = (Boolean) method.invoke(mWifManager, config, true);
 
     if (enable) {
       Log.d("WiFi", "热点已开启");
     } else {
       Log.d("WiFi", "创建热点失败");
     }
   } catch (Exception e) {
     e.printStackTrace();
   }

关闭热点

关闭热点比较简单,也是用上面的方法,第二个参数传false就行了:

?
1
2
3
4
5
6
7
8
public void closeWifiAp() {
    try {
      Method method = mWifiManager.getClass().getMethod("setWifiApEnabled",   WifiConfiguration.class, boolean.class);
      method.invoke(mWifiManager, null, false);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

监听热点状态

热点的状态可以通过广播的方式来监听:

?
1
2
public static final String WIFI_AP_STATE_CHANGED_ACTION =
   "android.net.wifi.WIFI_AP_STATE_CHANGED";

不过这个变量是隐藏的,只能直接通过值来注册广播:

?
1
2
IntentFilter filter = new IntentFilter();
  filter.addAction("android.net.wifi.WIFI_AP_STATE_CHANGED");

然后在广播中获取state:

?
1
int state = intent.getIntExtra("wifi_state", 0);

wifi热点有如下几种状态:

?
1
2
3
4
5
#WIFI_AP_STATE_DISABLED 
#WIFI_AP_STATE_DISABLING
#WIFI_AP_STATE_ENABLED
#WIFI_AP_STATE_ENABLING
#WIFI_AP_STATE_FAILED

其他API:

获取WiFI热点当前状态,返回值就是上面五种状态:

?
1
public int getWifiApState()

判断WiFi热点是否打开:

?
1
public boolean isWifiApEnabled()

获取当前wifi热点的WifiConfiguration:

?
1
public WifiConfiguration getWifiApConfiguration()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://juejin.im/post/5d72469b51882515fa48ae5a

延伸 · 阅读

精彩推荐