写一个android App 获取Wi-Fi列表

写一个android App 获取Wi-Fi列表

    • WiFiManager代码
    • 扫描到的wifi热点
    • WiFi操作所用到的类
      • 相关类

WiFiManager代码

WiFiManager类,用来获取周围的热点信息。

class WiFiManagent {
    private WifiManager wifiManager = null;
    private ArrayList<ScanResult> wifiList = new ArrayList<>();
    WiFiManagent(Context context){
        wifiManager = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        assert wifiManager !=null;
    }
    private void checkAndOpenWiFi(){
        if (!wifiManager.isWifiEnabled()){
            wifiManager.setWifiEnabled(true);
        }
    }
    private void startScan(){
        wifiManager.startScan();
        wifiList = (ArrayList<ScanResult>) wifiManager.getScanResults();
    }

    /**
     * 打开并扫描WiFi信号,将结果存储在列表里
     */
    void scanWiFi(){
        checkAndOpenWiFi();
        startScan();
    }

    /**
     * @return 信号列表
     */
    ArrayList<ScanResult>getWifiList(){
        return wifiList;
    }

    /**
     * @return Mac地址+信号强度+名称 的数组列表
     */
    ArrayList<String> getBasicInfo(){
        if (wifiList.isEmpty())
            return null;
        ArrayList<String>wifiInfo = new ArrayList<>();
        for (ScanResult e:wifiList){
            wifiInfo.add(e.BSSID+ " \nSSID编号 "+ e.SSID + " 信号强度:" + e.level );
        }
        return wifiInfo;
    }

}

扫描到的wifi热点

写一个android App 获取Wi-Fi列表_第1张图片

WiFi操作所用到的类

方法 方法描述
ScanResult Describes information about a detected access point.
Wificonfiguration A class representing a configured Wi-Fi network,including the security configuration.
Wificonfiguration.AuthAlgorithm Recognized IEEE 802.11 authentication algorithms.
Wificonfiguration.GroupCipher Recognized group ciphers.
Wificonfiguration.KeyMgmt Recognized key management schemes.
Wificonfiguration.PairwiseCipher Recognized pairwise ciphers for WPA.
Wificonfiguration.Protocol Recognized security protocols.
Wificonfiguration.Status Possible status of a network configuration.
WifiInfo Describes the state of any Wifi Connection that ia active or is in the process of being set up.
WifiManager This class provides the primary API for managing all aspects of Wi-Fi connectivicty.
WifiManager.MulticastLock Allows an application to receive Wifi Multicast packets.
WifiManager.WifiLock Allows an application to keep the Wi_Fi radio awake.

相关类

ScanResult类
这个类主要是通过Wifi硬件的扫描来获取一些周边的wifi热点(access point)的信息。

返回类型 域名 解释
public String BSSID AP点的mac地址
public String SSID wifi名称
public String capabilities 网络性能,包括接入点支持的认知、秘钥管理、加密机制等
public int frequency 以MHZ为单位的接入频率
public int level 以dbm为单位的信号强度

你可能感兴趣的:(写一个android App 获取Wi-Fi列表)