Android WiFi Hotspot Development example code

I wrote the content of Android connecting to anonymous WiFi last time. WiFi development is a relatively small knowledge point for application layer development, but since it is used, it is recorded here.

Create hotspot

1. Create hotspots according to encryption type, password, whether to hide and other parameters

 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;
  }

Then call WifiManager's setWifiApEnabled method to set wifiConfiguration, because it is hidden and needs reflection:

 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();
    }

Turn off hotspot

It's easy to turn off the hotspot. The above method is also used. Just pass the second parameter false:

public void closeWifiAp() {
    try {
      Method method = mWifiManager.getClass().getmethod("setWifiApEnabled",boolean.class);
      method.invoke(mWifiManager,null,false);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

Monitor hotspot status

The status of hotspots can be monitored by broadcasting:

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

However, this variable is hidden and can only be registered directly through the value:

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

Then get the state in the broadcast:

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

WiFi hotspots have the following states:

#WIFI_AP_STATE_DISABLED
#WIFI_AP_STATE_DISABLING
#WIFI_AP_STATE_ENABLED
#WIFI_AP_STATE_ENABLING
#WIFI_AP_STATE_Failed

Other APIs:

Get the current status of WiFi hotspot, and the return value is the above five statuses:

public int getWifiApState()

Judge whether WiFi hotspot is on:

public boolean isWifiApEnabled()

Get the WiFi configuration of the current WiFi hotspot:

public WifiConfiguration getWifiApConfiguration()

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>