Android:如何监控WiFi信号强度

发布于 2024-07-29 21:07:09 字数 891 浏览 3 评论 0原文

当信号强度发生变化时我会收到通知。 我尝试创建以下方法并在 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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百变从容 2024-08-05 21:07:09

只是放弃我的解决方案:

在我的应用程序中

@Override
protected void onResume() {
    super.onResume();
    networkStateReceiver = new NetworkStateReceiver(this);
    this.registerReceiver(networkStateReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
    this.registerReceiver(networkStateReceiver, new IntentFilter("android.net.wifi.SCAN_RESULTS"));
    networkStateReceiver.registerListener(this);
}

,我的接收器中的 onReceive 看起来像:

@Override
public void onReceive(Context context, Intent intent) {
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
        switch (networkInfo.getType()) {
            case ConnectivityManager.TYPE_ETHERNET:
                this.networkStateMessage = new NetworkStateMessage(networkInfo);
                break;
            case ConnectivityManager.TYPE_WIFI:
                this.networkStateMessage = new NetworkStateMessage(networkInfo, wifiManager.getConnectionInfo());
                worker.schedule(new Runnable() {
                    @Override
                    public void run() { wifiManager.startScan(); }
                }, 5, TimeUnit.SECONDS);
                break;
            default:
                this.networkStateMessage = null;
                break;
        }
    }
    notifyStateToAll(this.networkStateMessage);
}

这个想法是,如果 wifi 是活动网络,则每 5 秒启动一次。

Just to drop my solution:

in my App I have

@Override
protected void onResume() {
    super.onResume();
    networkStateReceiver = new NetworkStateReceiver(this);
    this.registerReceiver(networkStateReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
    this.registerReceiver(networkStateReceiver, new IntentFilter("android.net.wifi.SCAN_RESULTS"));
    networkStateReceiver.registerListener(this);
}

And the onReceive in my Receiver looks like:

@Override
public void onReceive(Context context, Intent intent) {
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
        switch (networkInfo.getType()) {
            case ConnectivityManager.TYPE_ETHERNET:
                this.networkStateMessage = new NetworkStateMessage(networkInfo);
                break;
            case ConnectivityManager.TYPE_WIFI:
                this.networkStateMessage = new NetworkStateMessage(networkInfo, wifiManager.getConnectionInfo());
                worker.schedule(new Runnable() {
                    @Override
                    public void run() { wifiManager.startScan(); }
                }, 5, TimeUnit.SECONDS);
                break;
            default:
                this.networkStateMessage = null;
                break;
        }
    }
    notifyStateToAll(this.networkStateMessage);
}

The idea is the to startScan() only every 5 seconds if the wifi is the active network.

静赏你的温柔 2024-08-05 21:07:09

首先,确保您的清单中具有 ACCESS_WIFI_STATE 权限。

其次,我不确定单个连接的通知,但要获取无线电所看到的所有内容的通知,您可以开始扫描:

wifi.startScan();

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

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

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

希望这会有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文