android wifi信号强度不正常

时间:2012-12-20 12:35:21

标签: android android-wifi

我在wpa_supplicant中使用以下代码来获取Android中wifi的信号强度。虽然路由器距离1米,但我能够获得链路速度,但信号强度始终显示-110dB。

代码:

  if ((os_strcasecmp(cmd, "rssi") == 0) || (os_strcasecmp(cmd, "rssi-approx") == 0)) {
        struct iwreq wrq;
        struct iw_statistics stats;
        signed int rssi;
        wpa_printf(MSG_DEBUG, ">>>. DRIVER AWEXT RSSI ");
        wrq.u.data.pointer = (caddr_t) &stats;
        wrq.u.data.length = sizeof(stats);
        wrq.u.data.flags = 1; /* Clear updated flag */
        os_strncpy(wrq.ifr_name, drv->wext->ifname, IFNAMSIZ);

        if (ioctl(drv->wext->ioctl_sock, SIOCGIWSTATS, &wrq) < 0) {
            perror("ioctl[SIOCGIWSTATS]");
            ret = -1;
        } else {
            if (stats.qual.updated & IW_QUAL_DBM) {
                /* Values in dBm, stored in u8 with range 63 : -192 */
                rssi = ( wrq.u.qual.level > 63 ) ?
                    wrq.u.qual.level - 0x100 :
                    wrq.u.qual.level;
            } else {
                rssi = wrq.u.qual.level;
            }

            if (drv->ssid_len != 0 && drv->ssid_len < buf_len) {
                os_memcpy((void *) buf, (void *) (drv->ssid),
                        drv->ssid_len );
                ret = drv->ssid_len;
                ret += snprintf(&buf[ret], buf_len-ret,
                        " rssi %d\n", rssi);
                if (ret < (int)buf_len) {
                    return( ret );
                }
                ret = -1;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

以下代码将提供wifi强度的RSSI值...

希望这会对你有帮助.....

public class Wifi2 extends Activity{

    Context context; 

  @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


    context=this;
}   

  @Override
  public void onResume() {
      super.onResume();
      //Note: Not using RSSI_CHANGED_ACTION because it never calls me back.
      IntentFilter rssiFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
      this.registerReceiver(myRssiChangeReceiver, rssiFilter);

      WifiManager wifiMan=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
      wifiMan.startScan();
  }


      @Override
  public void onPause() {
      super.onPause();
      this.unregisterReceiver(myRssiChangeReceiver);

  }
  /**
   * Broadcast receiver to update 
   * s
   */
  private BroadcastReceiver myRssiChangeReceiver
          = new BroadcastReceiver(){
      @Override
      public void onReceive(Context arg0, Intent arg1) {
          WifiManager wifiMan=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
          wifiMan.startScan();
          int newRssi = wifiMan.getConnectionInfo().getRssi();
          Toast.makeText(context, ""+newRssi, Toast.LENGTH_SHORT).show();

  }};

}