No response received in startscan method at WifiManager in Android 8 Oreo

旧巷老猫 提交于 2019-12-03 15:35:56

In Android 8 or higher the implicit BroadcastReceivers declared via Manifest are no longer send nor received due to performance reasons (this is an optimization introduced in Android 8 which limits background execution). There are some exceptions which are listed here, but android.net.wifi.SCAN_RESULTS action isn't an exception, so in Android 8+ you can't register a android.net.wifi.SCAN_RESULTS action to wait for ScanResults in the Manifest (actually you can, but you'll receive nothing).

This happens if your targetSdkVersion is 26 (Android 8 Oreo) or higher, but if you declare in your Gradle file a targetSdkVersion 25 or lower this optimization will not run for your app and your implicit Intents registered through Manifest will work as expected.

To get it work in Android 8 Oreo with a targetSdkVersion 26+ you have to register it through your Application Context.

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

But be aware, this kind of registration requires the app being running, so when your app is stopped you will not receive this BroadcastReceiver.

P.S: Your code is correct, but you didn't keep in mind this Android 8 limitation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!