尽管提供了我认为正确的UUID: D/ERROR:: com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException: Characteristic not found with UUID 00001101-0000-1000-8000-00805f9b34fb
,但是我仍无法从设备中检索数据。我该怎么办?
// Get device from scan
rxBleDevice = scanResult.bleDevice
val deviceUUID = UUID.fromString(scanResult.bleDevice.bluetoothDevice.uuids[0].toString())
// Check device info
println("Name: " + scanResult.bleDevice.name) // Prints to "TestDevice"
println("MAC Address: " + scanResult.bleDevice.macAddress) // Prints to "34:81:F4:3C:2D:7B"
println("UUID = $deviceUUID") // Prints to "00001101-0000-1000-8000-00805f9b34fb"
println("Service UUIDs = " + scanResult.scanRecord.serviceUuids) // Prints to "null"
/* Establish connection to device */
rxBleDevice!!.establishConnection(false) ?
.doOnNext {
_ ->
Log.d("Device: ", "Connection Established")
} ?
.flatMapSingle {
rxBleConnection ->
deviceUUID? .let {
rxBleConnection.readCharacteristic(it)
}
} ? .subscribe({
count ->
// count should be in bytes
println("SUCCESS: $count")
}, {
throwable ->
Log.d("ERROR: ", "$throwable")
})
我的预期输出是一个字节数组,例如,读取“ datc00099”。
答案 0 :(得分:1)
您尝试使用的UUID
来自BluetoothDevice.getUuids()
,它似乎来自“经典的”蓝牙世界。
scanResult.scanRecord.serviceUuids
也可能是null
,因为不能保证UUID
被广告。请记住,服务UUID
和特征UUID
,虽然格式相同,但要标识不同属性。
获取所需的UUID
的最简单方法是询问外围设备的工程师。
或者,如果您知道要搜索的内容,则可以连接到外围设备,开始服务发现并搜索结果。在1.9.0
版本中使用此lib看起来像这样:
rxBleClient.scanBleDevices(
ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build(),
ScanFilter.Builder()
.setDeviceName("TestDevice")
.build()
)
.take(1)
.flatMap { it.bleDevice.establishConnection(false) }
.flatMapSingle { it.discoverServices() }
.take(1)
.subscribe(
{ t -> t.bluetoothGattServices }, // Print services
{ Log.e("ERROR", "WHOOPS!", it) }
)
日志选项应为:
RxBleClient.updateLogOptions(LogOptions.Builder()
.setLogLevel(LogConstants.VERBOSE)
.setUuidsLogSetting(LogConstants.UUIDS_FULL)
.build()
在您的日志中,您应该看到类似以下内容的
:V/RxBle#ServiceDiscoveryOperation: Preparing services description
V/RxBle#ServiceDiscoveryOperation: --------------- ====== Printing peripheral content ====== ---------------
V/RxBle#ServiceDiscoveryOperation: MAC='XX:XX:XX:XX:XX:XX'
V/RxBle#ServiceDiscoveryOperation: PERIPHERAL NAME: SensorTag
V/RxBle#ServiceDiscoveryOperation: -------------------------------------------------------------------------
V/RxBle#ServiceDiscoveryOperation: Primary Service - Generic Access (00001800-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 1
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Device Name (00002a00-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * Appearance (00002a01-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * Peripheral Privacy Flag (00002a02-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: * Reconnection Address (00002a03-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ WRITE ]
V/RxBle#ServiceDiscoveryOperation: * Peripheral Preferred Connection Parameters (00002a04-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: Primary Service - Generic Attribute (00001801-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 12
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Service Changed (00002a05-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ INDICATE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Device Information (0000180a-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 16
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * System ID (00002a23-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * Model Number String (00002a24-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * Serial Number String (00002a25-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * Firmware Revision String (00002a26-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * Hardware Revision String (00002a27-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * Software Revision String (00002a28-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * Manufacturer Name String (00002a29-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * IEEE 11073-20601 Regulatory Certification Data List (00002a2a-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: * PnP ID (00002a50-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000aa00-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 35
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa01-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa02-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000aa10-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 43
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa11-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa12-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa13-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000aa20-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 54
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa21-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa22-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000aa30-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 62
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa31-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa32-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa33-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000aa40-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 73
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa41-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa42-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa43-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000aa50-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 85
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa51-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa52-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (0000ffe0-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 93
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (0000ffe1-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Properties: [ NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000aa60-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 98
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa61-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000aa62-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000ccc0-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 105
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000ccc1-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ READ NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000ccc2-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000ccc3-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ WRITE ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: Primary Service - Unknown service (f000ffc0-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Instance ID: 116
V/RxBle#ServiceDiscoveryOperation: -> Characteristics:
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000ffc1-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ WRITE_NO_RESPONSE WRITE NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Unknown characteristic (f000ffc2-0451-4000-b000-000000000000)
V/RxBle#ServiceDiscoveryOperation: Properties: [ WRITE_NO_RESPONSE WRITE NOTIFY ]
V/RxBle#ServiceDiscoveryOperation: -> Descriptors:
V/RxBle#ServiceDiscoveryOperation: * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: * Characteristic User Description (00002901-0000-1000-8000-00805f9b34fb)
V/RxBle#ServiceDiscoveryOperation: --------------- ====== Finished peripheral content ====== ---------------
括号中提供了特征UUID
。您只需要找到所需的那个。