0

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.

TTKK
  • 15
  • 4

2 Answers2

1

This is how I solve the problem

  1. Use 'ForegroundService'
  • I didnt check whether workmanager is available or not, but ForegroundService works well. I use timer and timertask to repeat the process in foreground.
  1. Grant permission "Allowed all the time"
  • It was the main issue. Doze mode do not allow my app to use GPS. So I open the GPS setting and set my app's permission from allow when using app to allow all the time.
TTKK
  • 15
  • 4
0

this is my working code - even device screen is locked.

UPDATE:

thank u for mentioned that you have used "WorkerManager". After that only striked in my mind. Since android 8, google introduced battery optimizations such as Doze mode. So, it doesn't allow the service to be run in background during Sleep(Doze mode). So, you can do the BLE Scanning **

using "Foreground Service"

** instead of Worker or Jobscheduler, etc.

please view this link: https://developer.android.com/training/monitoring-device-state/doze-standby

I also, struggled at this stage 2years ago. After switched to "Forgreound Service" it is working

OLD ( Initialization Regarding - BLE Scan) kindly check with your code. This is simple. Awaiting for your response.

1. Initialize scanfilter

    btLeScanFilters = new ArrayList<ScanFilter>();
    ScanFilter scanFilter = new ScanFilter.Builder()
            .setServiceUuid(new ParcelUuid("........ uuid reference ......"))
            .build();
    btLeScanFilters.add(scanFilter);

2. Initialize scan settings

    btLeScanSettings = new ScanSettings.Builder().

setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();

3. Initialize scan callback

   private ScanCallback leScanCallback = new ScanCallback() {

    @Override
    public void onScanResult(int callbackType,
                             final ScanResult result){

        new TPMSAsyncTask().execute(result);
    }
   }

4. start scanning

    btLeScanner.startScan(btLeScanFilters, 
               btLeScanSettings,
               leScanCallback);
        
   
  • only different part is filter initialization. I initialize filter in list and try same process but the result is not changed. Thank you for help. – TTKK Aug 07 '20 at 01:45
  • please share your log when device screen locks. kindly track your scancallback. it is present in activity or service ?? and if it is in activity, also look at onpause method of activity. – PushpaSakthi Aug 07 '20 at 09:28
  • I add my code and log and I didnt set any code in onPause() method. – TTKK Aug 07 '20 at 11:20
  • I tried but it is same :( I guess when screen goes off, GPS turns off too. – TTKK Aug 17 '20 at 08:13
  • Thank you so much, I solved the problem by ForegroundService and just changing permission from 'Allowed only while in use' to 'Allow all the time'.. It was very simple problem – TTKK Aug 19 '20 at 01:20