1

I have problem to start BLE scan on Huawei P30 running on Android 10. It works on Huawei P20 running Android 9 and many other devices, but Android 10 is weird. In application I have Start Scan button, so this is scan when application is running(no background scan). When I press this button I see that state is RxBleClient.State.READY and code inside this block is executed. Unfortunatelly the code inside subscribe is not called. Weird thing is when the mobile screen is locked and then unlocked the Scan will start. The same is when the BT is turned off and on again

stateDisposable = rxBleClient.observeStateChanges().startWith(rxBleClient.state)
            // switchMap makes sure that if the state will change
            // the rxBleClient.scanBleDevices() will dispose and thus end the scan
            .switchMap<Any> { state ->
                stateSubject.onNext(convertState(state))
                when (state) {
                    RxBleClient.State.READY -> {
                        return@switchMap rxBleClient.scanBleDevices(
                            ScanSettings.Builder()
                                .setScanMode(ScanSettings.SCAN_MODE_BALANCED)
                                .build(),
                            ScanFilter.Builder()
                                .setServiceUuid(ParcelUuid(UUID.fromString(Constants.UUID_SERVICE_BATTERY)))
                                .build()
                        )
                    }
                    RxBleClient.State.BLUETOOTH_NOT_AVAILABLE,
                        // basically no functionality will work here
                    RxBleClient.State.LOCATION_PERMISSION_NOT_GRANTED,
                        // scanning and connecting will not work
                    RxBleClient.State.BLUETOOTH_NOT_ENABLED,
                        // scanning and connecting will not work
                    RxBleClient.State.LOCATION_SERVICES_NOT_ENABLED -> {
                        // scanning will not work
                        return@switchMap Observable.empty()
                    }
                }
            }
            .subscribe(
                { rxBleScanResult ->
                    println("XXX scan received")
                    if (rxBleScanResult is ScanResult) {
                        println("XXX scan received address: ${rxBleScanResult.bleDevice.macAddress}")                         
                        scanResultSubject.onNext(scanResults)
                    }
                },
                { throwable ->
                    println("Error when scanning BLE devices: $throwable")
                }
            )

Does anybody knows how to fix this?

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
Martin K
  • 61
  • 1
  • 10
  • `Weird thing is when the mobile screen is locked and then unlocked the Scan will start. The same is when the BT is turned off and on again` — do you mean that after these situations the scans are received? – Dariusz Seweryn May 06 '20 at 19:35
  • Yes, when mobile phone is locked and unlocked the scans will be received by mobile application(the same for BT Off and On) – Martin K May 11 '20 at 09:57
  • Have you put a log in `switchMap` to log the states you receive? – Dariusz Seweryn May 12 '20 at 10:07
  • 1
    yes, the scanBleDevices() method is called but no scanResult are received – Martin K May 13 '20 at 08:41
  • You may be facing this issue: https://github.com/Polidea/RxAndroidBle/issues/688 . If so you can filter in your own code and do not use offloaded filtering. – Dariusz Seweryn May 14 '20 at 19:09

0 Answers0