Last time I wrote about Android connecting to anonymous WiFi. WiFI development for application layer development is a relatively small knowledge point, but since used in this record.

Create a hot

1. Create a hotspot based on the encryption type, password, and whether to hide the hotspot

 static WifiConfiguration createWifiConfig(String SSID, @WifiSecurityType int wifiCipherType, String password, boolean hiddenSSID) {

        WifiConfiguration wifiConfiguration = new WifiConfiguration();
        wifiConfiguration.SSID = convertToQuotedString(SSID);
        wifiConfiguration.hiddenSSID=hiddenSSID;// Whether to hide the hotspot true= Hide the hotspot. To hide the hotspot, other devices need to manually add the network
        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;
    }
Copy the code

Then call the WifiManager setWifiApEnabled method to set the wifiConfiguration, since it is hidden, by 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"."Hot spot on");
            } else {
                Log.d("WiFi"."Failed to create hotspot"); }}catch (Exception e) {
            e.printStackTrace();
        }
Copy the code

Closed hot

To close a hot spot, use the same method as above, and pass the second parameter false:

public void closeWifiAp(a) {
        try {
            Method method = mWifiManager.getClass().getMethod("setWifiApEnabled",      WifiConfiguration.class, boolean.class);
            method.invoke(mWifiManager, null.false);
        } catch(Exception e) { e.printStackTrace(); }}Copy the code

Monitoring hotspot Status

The hotspot status can be monitored by broadcasting:

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

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

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

Then get state in the broadcast:

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

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
Copy the code

Other API:

Get the current status of WiFI hotspot, the return value is the above five states:

public int getWifiApState(a)
Copy the code

Determine whether the WiFi hotspot is on:

public boolean isWifiApEnabled(a)
Copy the code

Of the current wifi hotspotWifiConfiguration:

public WifiConfiguration getWifiApConfiguration(a)
Copy the code