SCAN_RESULTS_AVAILABLE_ACTION在Android 6.0中返回空列表

昨天,我的Nexus 5收到了更新Android MNC到的版本6.0 - Marshmallow。从那时起,扫描设备中可用网络的操作将停止接收列表,在这种情况下,即使Wifi系统设置中列出了10个以上的Wifi网络,结果列表的大小也为0。


这是通常的代码:SCAN_RESULTS_AVAILABLE_ACTION在Receiver中注册并等待事件,如下所示:


// Register the Receiver in some part os fragment...

getActivity().registerReceiver(wifiListener, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

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

wifiManager.startScan();


// Inside the receiver:

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

List<ScanResult> results = wifiManager.getScanResults();

// the result.size() is 0 after update to Android v6.0, same code working in older devices.

我搜索了有关此主题的API主题的更改,但没有看到此功能的任何重大更改。


有人注意到吗?API中有新内容还是仅是个别情况?


温温酱
浏览 1904回答 3
3回答

心有法竹

从Android 6.0开始,权限行为已更改为运行时。要使用需要许可的功能,应首先检查该许可是否先前已授予。使用checkSelfPermission(permissionString) 方法返回结果,而权限为PERMISSION_GRANTED或PERMISSION_DENIED。如果未授予许可或这是第一次,则应提出许可请求。向用户提供授予或拒绝的选项。if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){&nbsp; &nbsp;requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);&nbsp; &nbsp; //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method}else{&nbsp; &nbsp; getScanningResults();&nbsp; &nbsp;//do something, permission was previously granted; or legacy device}如果您的代码在M之前的设备上运行,则继续执行代码,使用旧方法授予权限。一旦请求许可,对话框将显示给用户。他/她的答复将作为:@Override&nbsp;public void onRequestPermissionsResult(int requestCode, String[] permissions,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int[] grantResults) {&nbsp; &nbsp; &nbsp;if (requestCode == PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Do something with granted permission&nbsp; &nbsp; &nbsp; &nbsp; mWifiListener.getScanningResults();&nbsp; &nbsp; &nbsp;}&nbsp;}之后,您可以检查定位服务是否打开,使用LocationServices.SettingsApi并请求用户启用(如果此选项被禁用)。Play服务LocationSettingsStatusCodes.RESOLUTION_REQUIRED回调可以做到这一点。

拉风的咖菲猫

我在AOSP问题跟踪器问题185370中发现了相关问题,如果GPS关闭,则WifiManager#getScanResults()返回一个空数组列表。问题从#1提到,手机必须打开定位服务才能获取手机的wifi列表。从#18开始,Android项目成员声称开发团队已修复您报告的问题,并将在以后的版本中提供。APP位于targetSdkVersion 23中,只需按照上述解决方案检查运行时权限即可。强制启用位置服务问题将在Android未来版本中修复。

胡子哥哥

因此,问题似乎出在新的权限处理上。您必须先获得许可,然后才能进入wifi代码。这是一个例子:// call this method only if you are on 6.0 and up, otherwise call doGetWifi()private void getWifi() {&nbsp; &nbsp; if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 0x12345);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; doGetWifi(); // the actual wifi scanning&nbsp; &nbsp; }}@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {&nbsp; &nbsp; if (requestCode == 0x12345) {&nbsp; &nbsp; &nbsp; &nbsp; for (int grantResult : grantResults) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (grantResult != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; getWifi();&nbsp; &nbsp; }}此检查必须在活动中完成。原始示例代码在此处可用,并根据本主题中讨论的问题进行了修改。原版的根据API的链接更改,您的应用程序必须具有位置权限之一。引用:WifiManager.getScanResults():您的应用程序必须具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限。另外请注意,您的BroadcastReceiver在执行操作时收到了一个新的布尔键SCAN_RESULTS_AVAILABLE_ACTION:EXTRA_RESULTS_UPDATED。这显示扫描是否完成,您可以通过调用来访问结果wifiManager.getScanResults()。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android