android中 获取WiFi信息并计算wifi的信号强度

一、首先获得WifiManager

  WifiManager wifiManager=(WifiManager) getSystemService(WIFI_SERVICE);

添加权限:

 二、  WifiInfo wifiInfo=wifiManager.getConnectionInfo();//当前wifi连接信息

         List scanResults=wifiManager.getScanResults();//搜索到的设备列表

    for (ScanResult scanResult : scanResults) {
            tv.append("\n设备名:"+scanResult.SSID
                       +" 信号强度:"+scanResult.level+"/n :"+wifiManager.calculateSignalLevel(scanResult.level,4));

        }

附WifiManager中计算级别的代码:

  /**
     * Calculates the level of the signal. This should be used any time a signal
     * is being shown.
     *
     * @param rssi The power of the signal measured in RSSI.
     * @param numLevels The number of levels to consider in the calculated
     *            level.
     * @return A level of the signal, given in the range of 0 to numLevels-1
     *         (both inclusive).
     */
    public static int calculateSignalLevel(int rssi, int numLevels) {
        if (rssi <= MIN_RSSI) {
            return 0;
        } else if (rssi >= MAX_RSSI) {
            return numLevels - 1;
        } else {
            int partitionSize = (MAX_RSSI - MIN_RSSI) / (numLevels - 1);
            return (rssi - MIN_RSSI) / partitionSize;
        }
    }

你可能感兴趣的:(android中 获取WiFi信息并计算wifi的信号强度)