且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的:如何监视WiFi信号强度

更新时间:2023-02-26 15:15:18

首先,确保你有<使用-许可> 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.