使用 RxBLe 订阅通知更改 Android

Subscribing to notification changes with RxBLe Android

我正在尝试从我构建的 BLE 设备订阅通知更改,并打印通知的值。我知道我想从中读取通知的特征 UUID:

scanSubscription = rxBleClient.scanBleDevices(
        ScanSettings.Builder().build(),
        ScanFilter.Builder().setDeviceName("MyDevice").build() // Filter for devices named MyDevice
    )
    .take(1) // stop the scan when a matching device will be scanned for the first time
    .flatMap {
        val device = it.bleDevice
        device.establishConnection(false)
            .flatMap < Any > {
                rxBleConnection: RxBleConnection - > rxBleConnection.setupNotification(charUUID)
            }
            .doOnNext {
                notificationObservable: Any ? - >
            }
            .flatMap < Any > {
                notificationObservable: Any ? - > notificationObservable
            } // <-- Notification has been set up, now observe value changes.

    }
    .subscribe({ /* written */ }, {
        throwable - >
        // Handle an error here.
        // println("Scan Error: $throwable")
    })

为了证明设备按预期工作,我使用 Android 的 BLE 扫描仪应用观察了通知变化。

当我在传感器上挥手时,值会发生变化。在我的例子中,它增加了。

我的问题是当通知值改变时如何打印该值?调试时,无法打印 Count。它甚至没有出现在我的控制台中。

通常在 Android 上使用 Log class 将数据记录到 logcat。你可以这样使用它:

scanSubscription = rxBleClient.scanBleDevices(
        ScanSettings.Builder().build(),
        ScanFilter.Builder().setDeviceName("MyDevice").build() // Filter for devices named MyDevice
    )
    .take(1) // stop the scan when a matching device will be scanned for the first time
    .flatMap {
        val device = it.bleDevice
        device.establishConnection(false)
            .flatMap < Any > {
                rxBleConnection: RxBleConnection - > rxBleConnection.setupNotification(charUUID)
            }
            .doOnNext {
                notificationObservable: Any ? - >
            }
            .flatMap < Any > {
                notificationObservable: Any ? - > notificationObservable
            } // <-- Notification has been set up, now observe value changes.

    }
    .subscribe(
        { notification -> Log.i("Notification!", notification.contentToString()) }, 
        { throwable -> Log.e("Whoops!", "Scan Error", throwable) }
    )