Android 7.0 ble scan no result

寵の児 提交于 2019-12-01 08:54:48

You are probably running into the new and undocumented behavior changes in Android 7 that prevent apps from scanning too often.

I wrote a blog post about it: https://blog.classycode.com/undocumented-android-7-ble-behavior-changes-d1a9bd87d983

Joshi Tushar

I had also this kind of problem. It is solved by enabling Location. In android, we need to enable Location for scan near by devices. So please check location is enable or not. Below code is useful. I hope it works, in my case it works good.

LocationManager manager = (LocationManager) mainActivity.getSystemService(Context.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    buildAlertMessageNoGps(mainActivity);
}else{ //scanning part}


public void  buildAlertMessageNoGps(MainActivity mainActivity){
    final AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it? It is required for this application.")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                    mainActivity.startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), MainActivity.LOCATION_ENABLE);
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                    dialog.cancel();
                    mainActivity.finishAffinity();
                }
            });
    final AlertDialog alert = builder.create();
    alert.show();
}

After on location start scanning and get scanning result.

You need to make sure that you stop and re-start your scan only after 6 seconds for all devices with Android N and above.

This is as per the introduced restriction where apps are allowed to scan a maximum of 5 times in 30 seconds.

We’ve changed the BLE Scanning behavior starting in DP4. We’ll prevent applications from starting and stopping scans more than 5 times in 30 seconds. For long running scans, we’ll convert them into opportunistic scans.

Refer to this discussion here

You will also have to include location permissions in your Manifest or else your search will not return anything. This is mandatory i would say for Bluetooth and Wifi searches.

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

As the in the develoepr section for BLE in Android.

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