Android 7.0 does not scan result

When I start scanning (Bluetooth Le) within a few seconds, stop scanning. Then start then stop ... after about 5-8 cycles, the start action will be invalid, which means the scan record could not be received. 1. This condition only appears on Android 7.0 or higher (7.1.1); 2. I tried two scanning methods: BluetoothAdapter.startLeScan () and Scanner.startScan (), no difference.

private void scanToggle(final boolean enable) {
    mScanHandler.removeCallbacks(scanTask);
    if (enable) {
        TelinkLog.i("ADV#scanner#startScan");
        scanner = mBluetoothAdapter.getBluetoothLeScanner();
        scanner.startScan(null, settings, scanCallback);
        mScanning = true;
        mDeviceList.clear();
        mListAdapter.notifyDataSetChanged();
       //mBluetoothAdapter.startLeScan(leScanCallback);
        mScanHandler.postDelayed(scanTask, SCAN_PERIOD);
    } else {
        TelinkLog.i("ADV#scanToggle#stopScan");
        mScanning = false;
        //mBluetoothAdapter.stopLeScan(leScanCallback);
        scanner.stopScan(scanCallback);
    }
    invalidateOptionsMenu();
}


private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
        TelinkLog.d("scan:" + device.getName());
        for (AdvDevice advDevice : mDeviceList) {
            if (device.getAddress().equals(advDevice.device.getAddress())) return;
        }
        mDeviceList.add(new AdvDevice(device, rssi, scanRecord));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mListAdapter.notifyDataSetChanged();
            }
        });
    }
};

private ScanCallback scanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);
        for (AdvDevice advDevice : mDeviceList) {
            if (result.getDevice().getAddress().equals(advDevice.device.getAddress())) return;
        }
        mDeviceList.add(new AdvDevice(result.getDevice(), result.getRssi(), result.getScanRecord().getBytes()));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mListAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
    }

    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
    }
};

      

+3


source to share


3 answers


You've likely encountered new and undocumented behavior changes on Android 7 that often prevent apps from scanning.



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

+4


source


I had this problem too. It is resolved by including location. In android, we need to enable the scan location near devices. So please check if the location is enabled or not. Below is some useful code. I hope this works, in my case it works well.

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 starting scanning, scan and get the scan result.

+1


source


You need to make sure that you stop and restart the scan only after 6 seconds for all devices with Android N and above.

This is in line with the imposed restriction that applications are allowed to scan a maximum of 5 times in 30 seconds.

Weve changed the behavior of BLE scanning starting with DP4. It is good to prevent apps from starting and stopping scanning more than 5 times every 30 seconds. For long scans, convert them well to opportunistic scans.

Refer to this discussion here

+1


source







All Articles