Android 8.0 Oreo wifi list not getting
Asked Answered
U

3

7

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.

Undersized answered 8/3, 2018 at 10:44 Comment(3)
I have the same problem. I'm working on an app which uses WiFi scan results to work. Since today I was using a targetSdkVersion 25 and all was working perfectly on Android 8. Today I've just updated the targetSdkVersion to 26 and just noticed that on devices running Android 8 the scan results (via BroadcastReceiver declared in the Manifest) are no longer received. Only downgrading the targetSdkVersion to 25 works great, but I need a targetSdkVersion 26 or higher, so I'm frustrated right now. There is no info about this problem on the web.Algicide
Could you test your code with a targetSdkVersion 25 instead of 26 and tell me if you receive the wifi list now?Algicide
@MarcEstrada Were you able to fix this? Thank youMeristic
A
7

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.

Algicide answered 3/6, 2018 at 21:22 Comment(0)
B
3

Your 8.0 android device has to have Location enabled in order for wifimanager to work.

Burny answered 26/3, 2018 at 3:20 Comment(2)
Yes, I am trying... using location-enabled steel wifimanager not working.Undersized
Even with location enabled, targeting to 26+ Wifi ScanResults stops workingAlgicide
V
2

You must ask for the ACCESS_COARSE_LOCATION permission at runtime

 @Override
    public void onResume(){
        super.onResume();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
            }
        }
    }
Vitriolic answered 17/12, 2019 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.