RXBle Start scan does not work on Android 10 (Huawei P30)

614 Views Asked by At

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?

0

There are 0 best solutions below