android获取连接wifi的信号强度,如何获得连接WiFi android的信号强度?

我想获得与我的设备连接的这个wifi的信号强度.这可能很容易.但我是初学者.这是家庭作业.怎么弄?

我试过这段代码.但它不起作用.

registerReceiver(new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

int state = wifi.getWifiState();

if(state == WifiManager.WIFI_STATE_ENABLED) {

List results = wifi.getScanResults();

for (ScanResult result : results) {

if(result.BSSID.equals(wifi.getConnectionInfo().getBSSID())) {

int level = WifiManager.calculateSignalLevel(wifi.getConnectionInfo().getRssi(),

result.level);

int difference = level * 100 / result.level;

int signalStrangth= 0;

if(difference >= 100)

signalStrangth = 4;

else if(difference >= 75)

signalStrangth = 3;

else if(difference >= 50)

signalStrangth = 2;

else if(difference >= 25)

signalStrangth = 1;

tv.setText(tv.getText() + "\nDifference :" + difference + " signal state:" + signalStrangth);

}

}

}

}

}, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));

我也试过这段代码.但这也行不通.

int numberOfLevels = 5;

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值