4

I worked on an application which use a BLE scan into a foreground service. I used the library RxBle Android to perform the scan.



private fun scanBleDevices(): Observable<ScanResult>? {
    val rxBleClient = RxBleClient.create(this)
    val scanSettings = ScanSettings.Builder()
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
        .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
        .build()

    val scanFilter = ScanFilter.Builder()
        //.setServiceData(ParcelUuid.fromString("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"), ByteArray(1))
        //.setDeviceAddress("XX:XX:XX:XX:XX:XX")
        .build()

    return rxBleClient.scanBleDevices(scanSettings, scanFilter)
}

private fun startScan(){
    Timber.e("Start SCAN")
    scanBleDevices()?.let {
        it.observeOn(AndroidSchedulers.mainThread())
            .subscribe({ scanResult ->
                //Handle scan result
            }, {
                Timber.e("Scan failed")
            })
    }
}

I used ScanFilter in order to get scan results when the screen of my phone is locked.





When I used ScanFilter with mac address it worked great but it’s not a real solution because I don’t want to specify all the mac adress for all of my beacons.


So I try to use ScanFilter with service data id, it worked except when the screen of my phone is locked.

 The problem is only when I use my app with a Samsung device because I have a Pixel 3 and I don’t have the problem.

Do you have an explanation or a solution ?



Thank you !

yozzy
  • 344
  • 2
  • 15

0 Answers0