rxble setupIndication - PullRequest
       38

rxble setupIndication

0 голосов
/ 21 марта 2020

Мне нужно подключиться к устройству BLE, прочитать некоторые характеристики, настроить индикацию по двум характеристикам и дождаться индикации.

Я использую библиотеку android RxBle

Я хотел бы setupIndication с использованием RxBle

connectionDisposable.set(
            establishConnection(false)
                .flatMapSingle { cxn ->
                    connection = cxn
                    println("XXX DiscoverServices")
                    cxn.discoverServices()
                }.flatMapSingle { services ->
                    service = services
                    println("XXX RequestMtu")
                    connection!!.requestMtu(247)
                        .flatMap { request ->
                            connectionMtu = request
                            Single.just(connection)
                        }
                }.flatMap {
                    println("XXX Indication")
                    readInitialValues(connection!!)
                        .startWith(setupIndications(connection!!))
                }.subscribe({ dataPair ->
                    println("Reading char values")
                    parseCharacteristicData(dataPair)
                }, {
                    connectionSubject.onNext(ConnectionState.ERROR)
                })
        )

, где метод setupIndication выглядит следующим образом

private fun setupIndications(cxn: RxBleConnection): Observable<Pair<UUID, ByteArray>> {
    val characteristics = arrayListOf(
        //UUID.fromString(Constants.UUID_CHARACTERISTIC_CCP)
        UUID.fromString(Constants.UUID_CHARACTERISTIC_COMMAND_DATA),
        UUID.fromString(Constants.UUID_CHARACTERISTIC_BATTERY)
    )
    return Observable.fromIterable(characteristics)
        .flatMapSingle { uuid ->
            service!!.getCharacteristic(uuid)
        }.flatMap {
            if (it.uuid.toString() == Constants.UUID_CHARACTERISTIC_BATTERY) {
                cxn.setupNotification(it)
                    .flatMap { observable -> observable }
                    .map { data -> Pair(it.uuid, data) }
            } else {
                cxn.setupIndication(it)
                    .flatMap { observable -> observable }
                    .map { data -> Pair(it.uuid, data) }
            }
        }
}

Настройка уведомлений работает нормально, но при вызове метода устройство setupIndication будет отключено, и в журнале

произойдет ошибка
Tag: RxBle#ConnectionOperationQueue, Level:  3, Message:  FINISHED 
MtuRequestOperation(93930390) in 103 ms
XXX Tag: RxBle#ConnectionOperationQueue, Level:  3, Message:  Connection operations queue to 
be terminated (MAC='XX:XX:XX:XX:XX:XX')
com.polidea.rxandroidble2.exceptions.BleDisconnectedException: Disconnected from 
MAC='XX:XX:XX:XX:XX:XX' with status -1 (UNKNOWN)
     at com.polidea.rxandroidble2.internal.serialization.ConnectionOperationQueueImpl.onConnectionUnsubs 
 cribed(ConnectionOperationQueueImpl.java:162)
     at 
com.polidea.rxandroidble2.internal.connection.ConnectorImpl$1$1.run(ConnectorImpl.java:65)
        at io.reactivex.internal.operators.observable.ObservableDoFinally$DoFinallyObserver.runFinally(Obse 
 rvableDoFinally.java:142)
     at io.reactivex.internal.operators.observable.ObservableDoFinally$DoFinallyObserver.dispose(ObservableDoFinally.java:98)
 at io.reactivex.internal.disposables.DisposableHelper.dispose(DisposableHelper.java:124)
 at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.dispose(ObservableSubscribeOn.java:73)
 at io.reactivex.internal.operators.observable.ObservableUnsubscribeOn$UnsubscribeObserver$DisposeTask.run(ObservableUnsubscribeOn.java:95)
 at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:38)
 at io.reactivex.internal.schedulers.ScheduledDirectTask.call(ScheduledDirectTask.java:26)
 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
 at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
 at java.util.concurrent.ThreadPoolExecutor.processTask(ThreadPoolExecutor.java:1187)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
 at java.lang.Thread.run(Thread.java:784)
 XXX Tag: RxBle#ClientOperationQueue, Level:  3, Message:  QUEUED   
DisconnectOperation(17945456)
 XXX Tag: RxBle#ClientOperationQueue, Level:  3, Message:  STARTED  
DisconnectOperation(17945456)
 XXX Tag: RxBle#ClientOperationQueue, Level:  4, Message:  RUNNING  

DisconnectOperation{MAC='XX:XX:XX:XX:XX:XX'}
/BluetoothManager: getConnectionState()
/BluetoothManager: getConnectedDevices
/BluetoothGatt: cancelOpen() - device: C0:63:39:49:4D:95
/BluetoothGatt: onClientConnectionState() - status=0 clientIf=10 device=C0:63:39:49:4D:95
/System.out: XXX Tag: RxBle#GattCallback, Level:  4, Message:  MAC='XX:XX:XX:XX:XX:XX'  
onConnectionStateChange(), status=0, value=0
/BluetoothGatt: close()

Итак, как настроить индикацию ???

...