Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
840 views
in Technique[技术] by (71.8m points)

bluetooth lowenergy - Android 7.0 ble scan no result

When I start ble(Bluetooth Le) scan for seconds, then stop scan. Then start, then stop... after about 5-8 Loops, the start action will be No effect ,this means no scan record can be received. 1.This condition only appears on Android 7.0 or above(7.1.1); 2.I have tried two scan method: 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);
    }
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...