Android: How to monitor WiFi signal strength

前端 未结 2 896
粉色の甜心
粉色の甜心 2020-11-29 03:42

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

private void initializeWiFi         


        
相关标签:
2条回答
  • 2020-11-29 04:25

    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.

    0 讨论(0)
  • 2020-11-29 04:32

    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.

    0 讨论(0)
提交回复
热议问题