Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 8.0 Oreo wifi list not getting

On Android 8.0, not getting wifi list using wifiManager, below API level 26 I am getting the list.

This function returns the WifiManager Object

public static WifiManager getWifiManager(Context context) {
        WifiManager wifiManager = null;
        try {

            wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        return wifiManager;
    }

This function returns the wifi list

public static List<ScanResult> getWifiScanResults(Boolean sorted, Context context) {
    WifiManager wifiManager = NetworkUtil.getWifiManager(context);
    List<ScanResult> wifiList = wifiManager.getScanResults();

    //Remove results with empty ssid
    List<ScanResult> wifiListNew = new ArrayList<>();
    for (ScanResult scanResult : wifiList) {
        if (!scanResult.SSID.equals(""))
            wifiListNew.add(scanResult);
    }
    wifiList.clear();
    wifiList.addAll(wifiListNew);

         return wifiList;
}

I had registered the BroadcastReceiver

WiFiMainActivity.this.registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

and here is the startScan() Method

   public static void startScan(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        wifiManager.startScan();
    }

User having following permission

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

Not able to track the bug on android 8.0 Oreo, is there any other permission that I am missing.

like image 808
Kumar Jadhav Avatar asked Mar 08 '18 10:03

Kumar Jadhav


People also ask

Why is my Android phone not picking up Wi-Fi?

Restart your device. Open your Settings app and tap Network & internet or Connections. Depending on your device, these options may be different. Turn Wi-Fi off and mobile data on, and check if there's a difference. If not, turn mobile data off and Wi-Fi on and check again.

Why is my phone not picking up available Wi-Fi?

If your Android phone won't connect to Wi-Fi, you should first make sure that your phone isn't on Airplane Mode, and that Wi-Fi is enabled on your phone. If your Android phone claims it's connected to Wi-Fi but nothing will load, you can try forgetting the Wi-Fi network and then connecting to it again.

What do I do when my Wi-Fi is not showing in range?

Return to your Android device's Settings > Wireless & Networks > Wi-Fi panel and tap Wi-Fi Settings. Find your network's name (SSID) on the list of nearby Wi-Fi networks. If your network's name is not on the list, the AP or router may be hiding its SSID. Click Add Network to configure your network name manually.


1 Answers

Since in Android 8 was introduced the background execution limitation most of BroadcastReceiver actions registered in Manifest will be no longer sent nor received except these ones.

Yes, you are registering it through your Context but maybe your code is wrong.

You should register it through your Application Context instead of your Activity Context to avoid memory leaks. Also, try creating a void IntentFilter and adding your action afterwards. This code is working fine in my application with targetSdkVersion 26+ in Android 8 Oreo (Nexus 5X).

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.wifi.SCAN_RESULTS");
context.registerReceiver(new YourBroadcastReceiver(), intentFilter);

Hope this help.

like image 122
Marc Estrada Avatar answered Oct 10 '22 03:10

Marc Estrada