• WifiManager

    Get the WifiManager instance

    private WifiManager mWifiManager = (WifiManager)c.getSystemService(Context.WIFI_SERVICE);Copy the code
  • Check the status of Wiif

    Wifi status constant

    WIFI_STATE_DISABLED = 1; //Wi-Fi is disabled.
    WIFI_STATE_DISABLING = 0; //Wi-Fi is currently being disabled
    WIFI_STATE_ENABLED = 3; //Wi-Fi is enabled.
    WIFI_STATE_ENABLING = 2; //Wi-Fi is currently being enabled.
    WIFI_STATE_UNKNOWN = 4; //Wi-Fi is in an unknown stateCopy the code

    Wifi status monitoring

    int state = mWifiManager.isWifiEnabled();Copy the code

    The value of state corresponds to the wifi state constant above

  • Connect the wifi

    Open the wifi

    mWifiManager.setWifiEnabled(true);Copy the code

    Close the wifi

    mWifiManager.setWifiEnabled(false);Copy the code
  • Monitor the change of Wifi status

    Changes in Wifi status can be monitored by broadcast

    private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){ int s = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN); // Wifi status changes will trigger this broadcast //s value corresponds to the above wifi status constant, can be based on different status values to update interface}}};Copy the code

    Link to the wifi

    Int netId = mwifimanager.addNetwork (config); boolean b = mWifiManager.enableNetwork(netId, true);Copy the code

    Configuration // SSID: name of wifi, PWD: password of wifi, type: encryption mode 0: no encryption 1: WEP 2: WAP

    public WifiConfiguration createWifiInfo(String ssid,String pwd,int type){ log.e("create wifif ssid: "+ssid+" pwd: "+pwd+" type "+type); WifiConfiguration cfg = new WifiConfiguration(); WifiConfiguration tempConfig = this.isExist(ssid); if(tempConfig ! = null) { mWifiManger.removeNetwork(tempConfig.networkId); } cfg.SSID = "\"" + ssid + "\""; if( ! TextUtils.isEmpty(pwd)) { if(type == 0){ cfg.wepKeys[0] = ""; cfg.allowedKeyManagement.set(0); cfg.wepTxKeyIndex = 0; }else if( type == 1 ) { cfg.wepKeys[0] = "\"" + pwd + "\""; cfg.wepTxKeyIndex = 0; }else { cfg.preSharedKey = "\"" + pwd + "\""; } } return cfg; }Copy the code

    Wifi disconnection

    mWifiManager.disableNetwork(netId);Copy the code

    Delete the saved wifi

    boolean r = mWifiManager.removeNetwork(netId);Copy the code
  • Scan for available Wifi

    After the Wifi connection, you can scan the currently available Wifi

    boolean scan = mWifiManager.startScan(); // If true is returned, the scan is successfulCopy the code

    Get the list of wifi scanned

    List scanResults = mWifiManager.getScanResults();Copy the code

    You can use ScanResult to obtain wifi information

    for(ScanResult sr : scanResults){ String SSID = sr.SSID; // Wifi name, the name displayed in the Settings String BSSID = sr.bssid; String cap = sr.capabilities; // Wifi encryption mode int freq = sr.frequency; Int level = sr.level; // Wifi intensity}Copy the code

    Calculate wifi strength

    int level = WifiManager.calculateSignalLevel(sr.level, 5);Copy the code
  • Get information about the wifi that is already connected

    WifiInfo wi = mWifiManger.getConnectionInfo();Copy the code

    The information for WifiInfo is shown below




    Write the picture description here

  • Get information about the configured wifi

    List configs = mWifiManager.getConfiguredNetworks();Copy the code

    The message for the WifiConfiguration is shown below




    Write the picture description here




    Write the picture description here