且构网

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

Android:如何监控 WiFi 信号强度

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

首先,确保您的清单中有 用于 ACCESS_WIFI_STATE.

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

接下来,当我收到成功的结果时,我在 IntentFilter 中使用了 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.

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

然后,在接收器中,我使用 WifiManager 对象中的 getScanResults(),该对象还包含信号强度.

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

为了以这种方式停止它,您只需调用 unregisterRecever()(因此您需要保留它以供参考).我还没有测试过自己是否可以修改我的扫描代码以仅检查当前连接,但我知道我得到了很多结果——Wi-Fi 信号频繁且快速地变化.我想为了监视单个连接,您还可以过滤扫描结果并查找设备当前连接到的那个.

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.

我希望这会有所帮助.