Im working with Galaxy S10, android 10. From here, BLE scan with Samsung devices need non-empty scan filter to scan in screen-off state.
Below is my AdvertiseData and ScanFilter code.
ParcelUuid aaUuid = ParcelUuid.fromString("0000aa11-0000-1000-8000-00805F9B34FB");
AdvertiseData advertiseData = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.addServiceUuid(aaUuid)
.build();
ScanFilter scanFilter = new ScanFilter.Builder()
.setServiceUuid(aaUuid)
.build();
With screen on, it works well. However, with screen off, it doesn't work too.
I set 10 times scanning to check this. At first, screen on, it works well. When it became screen-off, the first scan works good. But next scan result does not appear and it just start next scanning process (without result).
'WakeLock' for each scanning repetition makes same result. How to solve this problem?
Add 1. I used WorkManager to handle background process. Then I was looking for another method.
Add 2. Below is my scancallback code.
private ScanCallback mScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
if (result.getDevice().getName() != null)
setBleScanResult(result);
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Log.d("LOG_BLESCANNING_onScanFailed", "Error Code : " + errorCode);
}
};
Add 3. Below is my log when screen off
...
D/Worker: ============ Bluetooth Scanning start ============
D/LOG_BluetoothScan: Bluetooth scanning started successfully
D/BluetoothAdapter: isLeEnabled(): ON
D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=4 mScannerId=0
D/BluetoothAdapter: isLeEnabled(): ON
D/LOG_BluetoothScan: Bluetooth scanning stopped
...
and screen on
D/Worker: ============ Bluetooth Scanning start ============
D/LOG_BluetoothScan: Bluetooth scanning started successfully
D/BluetoothAdapter: isLeEnabled(): ON
D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=4 mScannerId=0
D/LOG_BluetoothScan: W S10 | [00001111-0000-1000-8000-00805f9b34fb] | 55:40:DE:86:D0:03
D/LOG_BluetoothScan: W S10 | [00001111-0000-1000-8000-00805f9b34fb] | 55:40:DE:86:D0:03
D/LOG_BluetoothScan: W S10 | [00001111-0000-1000-8000-00805f9b34fb] | 55:40:DE:86:D0:03
D/LOG_BluetoothScan: W S10 | [00001111-0000-1000-8000-00805f9b34fb] | 55:40:DE:86:D0:03
D/BluetoothAdapter: isLeEnabled(): ON
D/LOG_BluetoothScan: Bluetooth scanning stopped
Add 4. I'm curious about 'Why is this problem only in the ble scanning?' I check ble advertising in screen off state does not have any problem.
Add 5. Advertising also has problem... but it is not essential. I think MainActivity - Worker (from WorkManager) connection is bad to control this situation.