Android的:如何监视WiFi信号强度 [英] Android: How to monitor WiFi signal strength

查看:534
本文介绍了Android的:如何监视WiFi信号强度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会收到通知,当信号强度的变化。我试图创建下面的方法,并调用它在OnCreate():

 私人无效initializeWiFiListener(){
Log.i(TAG,执行initializeWiFiListener);

字符串connectivity_context = Context.WIFI_SERVICE;
最后WifiManager无线=(WifiManager)getSystemService(connectivity_context);

如果(!wifi.isWifiEnabled()){
如果(wifi.getWifiState()!= WifiManager.WIFI_STATE_ENABLING){
wifi.setWifiEnabled(真正的);
}
}

registerReceiver(新BroadcastReceiver的(){

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
WifiInfo信息= wifi.getConnectionInfo();
// TODO:实施操作处理方法
}

},新的IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
}
 

我会AP preciate如果任何人都可以检查的方法是正确写入。我试图运行的应用程序,但还没有收到任何通知,我不知道是否是因为信号强度可能在我运行调试,或者是因为缺少一些东西的地方不变。

谢谢!

解决方案

首先,确保你有<使用-许可> ACCESS_WIFI_STATE 在你的清单。

二,我不知道有关单个连接的通知,但得到的一切无线电是看到了通知,就可以开始扫描:

  wifi.startScan();
 

接下来,当我收到了成功的结果,我在的IntentFilter WifiManager.SCAN_RESULTS_AVAILABLE_ACTION P>

然后,在接收器,我使用 getScanResults() WifiManager 对象,其中还包含了信号实力。

有关停止这种方式,您只需致电 unregisterRecever()(所以你要保持它周围的引用)。我没有测试过自己,看看我的扫描code可以进行修改,以只检查当前的连接,但我知道我有足够的结果 - Wi-Fi信号频繁和迅速改变。我想监视单个连接,您也可以只过滤扫描结果,寻找一个设备当前连接。

我希望这有助于一点。

I would receive notifications when signal strength changes. I tried to create the following method and call it in the onCreate():

private void initializeWiFiListener(){
	Log.i(TAG, "executing initializeWiFiListener");

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

	if(!wifi.isWifiEnabled()){
		if(wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING){
			wifi.setWifiEnabled(true);
		}
	}

	registerReceiver(new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			WifiInfo info = wifi.getConnectionInfo();
			//TODO: implement methods for action handling
		}

	}, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
}

I would appreciate if anyone could check if the method is written correctly. I tried to run the app, but haven't received any notifications and am not sure whether it is because the signal strength might be constant in the place I run debug or it is because something is missing.

Thanks!

解决方案

First, make sure you have <uses-permission> for ACCESS_WIFI_STATE in your manifest.

Second, I'm not sure about notifications for a single connection, but to get notifications of everything the radio is seeing, you can start a scan:

wifi.startScan();

Next, when I've received successful results, I used WifiManager.SCAN_RESULTS_AVAILABLE_ACTION in the IntentFilter.

Then, in the receiver, I use getScanResults() from the WifiManager object, which also contains the signal strength.

For stopping it this way, you simply call to unregisterRecever() (so you'll want to keep it around for referencing). I haven't tested myself to see if my scanning code can be modified to just check the current connection, but I do know I got plenty of results -- Wi-Fi signals change frequently and quickly. I suppose for monitoring a single connection, you can also just filter the scan results and look for the one the device is currently connected to.

I hope this helps a little.

这篇关于Android的:如何监视WiFi信号强度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆