android中对wifi信号强度的操作

找到组织 发布于 2013/10/18 10:48
阅读 2K+
收藏 5
public class wifiInfo{


  //监听wifi信号变化
  public BroadcastReceiver broadcastREceiver= new BroadcastREceiver() {


     public void onReceive(Context context, Intent intent) {


        getWifiInfo();
     }
  }


  public void getWifiInfo() {


        WifiManager wifiManager= (WifiManager)getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo= wfiManager.getConnectionInfo();
        if(wifiInfo.getBSSID()!= null) {


          //最强设置为5
          int strength= WifiManger.calculateSingalLevel(wifiInfo.getRssi(), 5);
        }
  }

  //自己实现获取wifi信号强度的方法:android2.2中bug
  /*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;
        }
    }*/
  
  protected void onResume() {

   super.onResume();
    registerReceiver(broadcastReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
  }

  protected void onPause() {

    super.onPause();
    unRegisterReceiver(broadcastReceiver);
  }
}

加载中
返回顶部
顶部