RxAndroidBle - 写入特征并接收通知 - 未收到通知
RxAndroidBle - Write characteristic and receive notifications - No notifications received
我在我的应用程序中使用 RxAndroidBle 库来实现一些 write/read 特性。
一切正常,除了一个我无法解决的用例。
问题:我需要为特定特征写入时间戳(INTERVAL_START_TIMESTAMP),所有比该时间戳更新的记录(存储在BLE设备上)将被推送到另一个特征(INTERVAL_RECORD).但是我根本没有收到任何通知...
这是我的代码:
var connection: RxBleConnection? = null
MyApplication.getRxBleClient(context).getBleDevice(mac)
.establishConnection(false)
.flatMap {
connection = it
connection?.setupIndication(UUID.fromString(Characteristic.INTERVAL_RECORD.uuid))
}
.doOnNext {
connection?.writeCharacteristic(UUID.fromString(Characteristic.INTERVAL_START_TIMESTAMP.uuid), ByteUtils.toByteArray(timestamp)
?.subscribe({
Log.e("Interval_start_timestamp", "write " + ByteUtils.toInt(it))
}, {
Log.e("Interval_start_timestamp", "write " + it.message)
})
}
.flatMap { it }
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
Log.e("Notification received", "" + ByteUtils.toInt(it))
}, {
Log.e("Notification error", "" + it.message)
})
在我的日志中,我可以看到通知设置正确,并且 writeCharacteristic 也成功。我只是没有收到任何通知。
BLE 设备没有问题,因为在 iOS.
上一切正常
有什么帮助或建议吗?
编辑:这是我的日志:
D/BluetoothGatt: connect() - device: DE:E4:DD:A1:D9:AC, auto: false
D/BluetoothAdapter: isSecureModeEnabled
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=5e7c5a4a-2fda-438c-b83d-4f944b5b6e79
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=8 device=DE:E4:DD:A1:D9:AC
D/RxBle#BluetoothGatt: onConnectionStateChange newState=2 status=0
D/RxBle#ClientOperationQueue: FINISHED ConnectOperation(184940035) in 4124 ms
D/RxBle#ConnectionOperationQueue: QUEUED ServiceDiscoveryOperation(53595065)
D/RxBle#ConnectionOperationQueue: STARTED ServiceDiscoveryOperation(53595065)
D/BluetoothGatt: discoverServices() - device: DE:E4:DD:A1:D9:AC
D/BluetoothGatt: onConnectionUpdated() - Device=DE:E4:DD:A1:D9:AC interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=DE:E4:DD:A1:D9:AC Status=0
D/RxBle#BluetoothGatt: onServicesDiscovered status=0
V/RxBle#ServiceDiscoveryOperation: Preparing services description
D/BluetoothGatt: onConnectionUpdated() - Device=DE:E4:DD:A1:D9:AC interval=39 latency=0 timeout=500 status=0
V/RxBle#ServiceDiscoveryOperation: --------------- ====== Printing peripheral content ====== ---------------
PERIPHERAL ADDRESS: DE:E4:DD:A1:D9:AC
PERIPHERAL NAME: null
-------------------------------------------------------------------------
Primary Service - Generic Access (00001800-0000-1000-8000-00805f9b34fb)
Instance ID: 1
-> Characteristics:
* Device Name (00002a00-0000-1000-8000-00805f9b34fb)
Properties: [ READ WRITE ]
* Appearance (00002a01-0000-1000-8000-00805f9b34fb)
Properties: [ READ ]
* Peripheral Preferred Connection Parameters (00002a04-0000-1000-8000-00805f9b34fb)
Properties: [ READ ]
Primary Service - Generic Attribute (00001801-0000-1000-8000-00805f9b34fb)
Instance ID: 8
-> Characteristics:
* Service Changed (00002a05-0000-1000-8000-00805f9b34fb)
Properties: [ INDICATE ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
Primary Service - Unknown service (e3ba1523-2785-005b-6676-d476eabca48e)
Instance ID: 12
-> Characteristics:
* Unknown characteristic (e3ba1524-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1526-2785-005b-6676-d476eabca48e)
Properties: [ READ INDICATE ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1527-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba1528-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba1529-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba152a-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba152c-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba152d-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba152e-2785-005b-6676-d476eabca48e)
Properties: [ READ ]
* Unknown characteristic (e3ba152f-2785-005b-6676-d476eabca48e)
Properties: [ READ ]
* Unknown characteristic (e3ba1530-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1531-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1532-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1538-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1539-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba153a-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
Primary Service - Unknown service (00001530-1212-efde-1523-785feabcd123)
Instance ID: 53
-> Characteristics:
* Unknown characteristic (00001532-1212-efde-1523-785feabcd123)
Properties: [ WRITE_NO_RESPONSE ]
* Unknown characteristic (00001531-1212-efde-1523-785feabcd123)
Properties: [ WRITE NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (00001534-1212-efde-1523-785feabcd123)
Properties: [ READ ]
--------------- ====== Finished peripheral content ====== ---------------
D/BluetoothGatt: setCharacteristicNotification() - uuid: e3ba1526-2785-005b-6676-d476eabca48e enable: true
D/RxBle#ConnectionOperationQueue: QUEUED DescriptorWriteOperation(82308959)
D/RxBle#ConnectionOperationQueue: FINISHED ServiceDiscoveryOperation(53595065) in 1235 ms
D/RxBle#ConnectionOperationQueue: STARTED DescriptorWriteOperation(82308959)
D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
D/RxBle#ConnectionOperationQueue: FINISHED DescriptorWriteOperation(82308959) in 91 ms
D/RxBle#ConnectionOperationQueue: QUEUED CharacteristicWriteOperation(85617269)
D/RxBle#ConnectionOperationQueue: STARTED CharacteristicWriteOperation(85617269)
D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=e3ba1527-2785-005b-6676-d476eabca48e status=0
E/Interval_start_timestamp: write 1548979200
D/RxBle#ConnectionOperationQueue: FINISHED CharacteristicWriteOperation(85617269) in 92 ms
假设 notifications/indications 是由写入适当特征触发的,看起来信息流是正确的。即
D/BluetoothGatt: setCharacteristicNotification() - uuid: e3ba1526-2785-005b-6676-d476eabca48e enable: true
D/RxBle#ConnectionOperationQueue: STARTED DescriptorWriteOperation(82308959)
D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
D/RxBle#ConnectionOperationQueue: FINISHED DescriptorWriteOperation(82308959) in 91 ms
D/RxBle#ConnectionOperationQueue: STARTED CharacteristicWriteOperation(85617269)
D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=e3ba1527-2785-005b-6676-d476eabca48e status=0
E/Interval_start_timestamp: write 1548979200
D/RxBle#ConnectionOperationQueue: FINISHED CharacteristicWriteOperation(85617269) in 92 ms
先设置通知,然后写入特征,没有关于任何错误的信息。在这种情况下,我认为问题出在假设上,并且会仔细检查是否:
- notification/indication 特征使用了正确的 UUID
- 正确的 UUID 用于应触发通知的特征
- 写入应触发通知的特性的值是正确的(例如,如果
ByteUtils.toByteArray(timestamp)
转换中没有错误 - 即字节顺序)
我在我的应用程序中使用 RxAndroidBle 库来实现一些 write/read 特性。 一切正常,除了一个我无法解决的用例。
问题:我需要为特定特征写入时间戳(INTERVAL_START_TIMESTAMP),所有比该时间戳更新的记录(存储在BLE设备上)将被推送到另一个特征(INTERVAL_RECORD).但是我根本没有收到任何通知...
这是我的代码:
var connection: RxBleConnection? = null
MyApplication.getRxBleClient(context).getBleDevice(mac)
.establishConnection(false)
.flatMap {
connection = it
connection?.setupIndication(UUID.fromString(Characteristic.INTERVAL_RECORD.uuid))
}
.doOnNext {
connection?.writeCharacteristic(UUID.fromString(Characteristic.INTERVAL_START_TIMESTAMP.uuid), ByteUtils.toByteArray(timestamp)
?.subscribe({
Log.e("Interval_start_timestamp", "write " + ByteUtils.toInt(it))
}, {
Log.e("Interval_start_timestamp", "write " + it.message)
})
}
.flatMap { it }
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
Log.e("Notification received", "" + ByteUtils.toInt(it))
}, {
Log.e("Notification error", "" + it.message)
})
在我的日志中,我可以看到通知设置正确,并且 writeCharacteristic 也成功。我只是没有收到任何通知。 BLE 设备没有问题,因为在 iOS.
上一切正常有什么帮助或建议吗?
编辑:这是我的日志:
D/BluetoothGatt: connect() - device: DE:E4:DD:A1:D9:AC, auto: false
D/BluetoothAdapter: isSecureModeEnabled
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=5e7c5a4a-2fda-438c-b83d-4f944b5b6e79
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=8 device=DE:E4:DD:A1:D9:AC
D/RxBle#BluetoothGatt: onConnectionStateChange newState=2 status=0
D/RxBle#ClientOperationQueue: FINISHED ConnectOperation(184940035) in 4124 ms
D/RxBle#ConnectionOperationQueue: QUEUED ServiceDiscoveryOperation(53595065)
D/RxBle#ConnectionOperationQueue: STARTED ServiceDiscoveryOperation(53595065)
D/BluetoothGatt: discoverServices() - device: DE:E4:DD:A1:D9:AC
D/BluetoothGatt: onConnectionUpdated() - Device=DE:E4:DD:A1:D9:AC interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=DE:E4:DD:A1:D9:AC Status=0
D/RxBle#BluetoothGatt: onServicesDiscovered status=0
V/RxBle#ServiceDiscoveryOperation: Preparing services description
D/BluetoothGatt: onConnectionUpdated() - Device=DE:E4:DD:A1:D9:AC interval=39 latency=0 timeout=500 status=0
V/RxBle#ServiceDiscoveryOperation: --------------- ====== Printing peripheral content ====== ---------------
PERIPHERAL ADDRESS: DE:E4:DD:A1:D9:AC
PERIPHERAL NAME: null
-------------------------------------------------------------------------
Primary Service - Generic Access (00001800-0000-1000-8000-00805f9b34fb)
Instance ID: 1
-> Characteristics:
* Device Name (00002a00-0000-1000-8000-00805f9b34fb)
Properties: [ READ WRITE ]
* Appearance (00002a01-0000-1000-8000-00805f9b34fb)
Properties: [ READ ]
* Peripheral Preferred Connection Parameters (00002a04-0000-1000-8000-00805f9b34fb)
Properties: [ READ ]
Primary Service - Generic Attribute (00001801-0000-1000-8000-00805f9b34fb)
Instance ID: 8
-> Characteristics:
* Service Changed (00002a05-0000-1000-8000-00805f9b34fb)
Properties: [ INDICATE ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
Primary Service - Unknown service (e3ba1523-2785-005b-6676-d476eabca48e)
Instance ID: 12
-> Characteristics:
* Unknown characteristic (e3ba1524-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1526-2785-005b-6676-d476eabca48e)
Properties: [ READ INDICATE ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1527-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba1528-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba1529-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba152a-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba152c-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba152d-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
* Unknown characteristic (e3ba152e-2785-005b-6676-d476eabca48e)
Properties: [ READ ]
* Unknown characteristic (e3ba152f-2785-005b-6676-d476eabca48e)
Properties: [ READ ]
* Unknown characteristic (e3ba1530-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1531-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1532-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1538-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba1539-2785-005b-6676-d476eabca48e)
Properties: [ READ NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (e3ba153a-2785-005b-6676-d476eabca48e)
Properties: [ READ WRITE ]
Primary Service - Unknown service (00001530-1212-efde-1523-785feabcd123)
Instance ID: 53
-> Characteristics:
* Unknown characteristic (00001532-1212-efde-1523-785feabcd123)
Properties: [ WRITE_NO_RESPONSE ]
* Unknown characteristic (00001531-1212-efde-1523-785feabcd123)
Properties: [ WRITE NOTIFY ]
-> Descriptors:
* Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
* Unknown characteristic (00001534-1212-efde-1523-785feabcd123)
Properties: [ READ ]
--------------- ====== Finished peripheral content ====== ---------------
D/BluetoothGatt: setCharacteristicNotification() - uuid: e3ba1526-2785-005b-6676-d476eabca48e enable: true
D/RxBle#ConnectionOperationQueue: QUEUED DescriptorWriteOperation(82308959)
D/RxBle#ConnectionOperationQueue: FINISHED ServiceDiscoveryOperation(53595065) in 1235 ms
D/RxBle#ConnectionOperationQueue: STARTED DescriptorWriteOperation(82308959)
D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
D/RxBle#ConnectionOperationQueue: FINISHED DescriptorWriteOperation(82308959) in 91 ms
D/RxBle#ConnectionOperationQueue: QUEUED CharacteristicWriteOperation(85617269)
D/RxBle#ConnectionOperationQueue: STARTED CharacteristicWriteOperation(85617269)
D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=e3ba1527-2785-005b-6676-d476eabca48e status=0
E/Interval_start_timestamp: write 1548979200
D/RxBle#ConnectionOperationQueue: FINISHED CharacteristicWriteOperation(85617269) in 92 ms
假设 notifications/indications 是由写入适当特征触发的,看起来信息流是正确的。即
D/BluetoothGatt: setCharacteristicNotification() - uuid: e3ba1526-2785-005b-6676-d476eabca48e enable: true
D/RxBle#ConnectionOperationQueue: STARTED DescriptorWriteOperation(82308959)
D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
D/RxBle#ConnectionOperationQueue: FINISHED DescriptorWriteOperation(82308959) in 91 ms
D/RxBle#ConnectionOperationQueue: STARTED CharacteristicWriteOperation(85617269)
D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=e3ba1527-2785-005b-6676-d476eabca48e status=0
E/Interval_start_timestamp: write 1548979200
D/RxBle#ConnectionOperationQueue: FINISHED CharacteristicWriteOperation(85617269) in 92 ms
先设置通知,然后写入特征,没有关于任何错误的信息。在这种情况下,我认为问题出在假设上,并且会仔细检查是否:
- notification/indication 特征使用了正确的 UUID
- 正确的 UUID 用于应触发通知的特征
- 写入应触发通知的特性的值是正确的(例如,如果
ByteUtils.toByteArray(timestamp)
转换中没有错误 - 即字节顺序)