0

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

代码:

  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;
            }
        }
    }
4

1 回答 1

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();

  }};

}
于 2012-12-20T13:16:02.287 回答