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

转于:http://blog.csdn.net/wang1588143/article/details/6653475

--------------------------------------------------------------------------------------------

一、首先获得WifiManager

  WifiManager wifiManager=(WifiManager) getSystemService(WIFI_SERVICE);

添加权限:<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

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

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

相关推荐