Calling startLeScan several times does not scan BLE devices properly

I try to call the startLeScan method several times in my Android application, it checks BLE devices for the first time, and the second time it cannot scan devices. I have a loop scan in my application and I stop scanning every 10 seconds and start again. Here are some magazines that show how my application works.

05-20 00:45:31.146  15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.147  15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.160  15841-15852/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=133 clientIf=0
05-20 00:45:31.161  15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.165  15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.169  15841-15909/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=7
05-20 00:45:31.171  15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.175  15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.179  15841-15853/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=8
05-20 00:45:31.181  15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.185  15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.189  15841-18615/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=6
05-20 00:45:31.191  15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.192  15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.202  15841-15852/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=133 clientIf=0
05-20 00:45:31.203  15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.207  15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.211  15841-15909/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=10

I have these function calls in my BluetoothLEScannerForMR2.java, and when I call the startScan method several times, I got the above logs.

protected void startScan() {
    if (getBluetoothAdapter() != null) {
        if (getBluetoothAdapter().isEnabled()) {
            getBluetoothAdapter().startLeScan(mLeScanCallback);
            mScanStopTime = new Date().getTime() + mScanPeriod;

            mScanning = true;

            handleIntervalScanning();
        }
    }
}

protected void stopScan() {
    if (getBluetoothAdapter() != null) {
        if (getBluetoothAdapter().isEnabled()) {
            getBluetoothAdapter().stopLeScan(mLeScanCallback);
            mScanning = false;
        }
    }
}

private void handleIntervalScanning() {
    long msUntilNextStop = mScanStopTime - (new Date().getTime());

    if (msUntilNextStop > mScanPeriod)
        msUntilNextStop =  mScanPeriod;

    if (msUntilNextStop > 0) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                handleIntervalScanning();
            }
        }, msUntilNextStop);
    } else {
        if (getBluetoothAdapter() != null) {
            if (getBluetoothAdapter().isEnabled()) {
                if (mScanning) {
                    getBluetoothAdapter().stopLeScan(mLeScanCallback);

                    startScan();
                }
            }
        }
    }
}
+4
source share

Source: https://habr.com/ru/post/1588869/


All Articles