Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to monitor WiFi signal strength

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!

like image 921
Niko Gamulin Avatar asked Jul 30 '09 14:07

Niko Gamulin


People also ask

How do I check WiFi signal strength on my phone?

In Windows, go to Network and Internet, and then Network and Sharing Center. Select the blue WiFi link to see the signal strength. On an Android phone or tablet. Look under Settings, WiFi, or Network, and search for a gear or WiFi icon next to the network you're connected to.

How can I measure my WiFi signal strength?

Use a Smartphone or TabletLook under a Settings, Wi-Fi, or Network menu. For example, in the settings on a Google Pixel with Android 10, select Network & internet, select the Wi-Fi you're using, and then select the gear icon next to the network you're connected to. There you can see the signal strength.


1 Answers

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.

like image 150
lilbyrdie Avatar answered Sep 21 '22 19:09

lilbyrdie