我在 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;
}
}
}