如何获取特征Uuid?
How to get characteristicUuid?
我使用 RxAndroidBle https://github.com/Polidea/RxAndroidBle 库来处理 ble 设备。但是我在查找 characteristicUuid.
时遇到问题
我从 README 做了所有的事情,但是找不到 characteristicUuid。在read()中需要执行.flatMapSingle { rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid) }
,但是没有characteristicUuid。我在哪里可以买到它?
private lateinit var rxBleClient: RxBleClient
private lateinit var bleDevice: RxBleDevice
private lateinit var characteristicUuid: UUID
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getLocationPermission()
rxBleClient = RxBleClient.create(this)
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
button.setOnClickListener { discover() }
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_OK) {
}
}
private lateinit var scanSubscription: Disposable
private fun discover() {
Log.i("BLE", "start scan")
scanSubscription = rxBleClient.scanBleDevices(
ScanSettings.Builder()
// .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
// .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
.build()
// add filters if needed
)
.subscribe(
{ scanResult ->
bleDevice = scanResult.bleDevice
Log.i("BLE", "SCAN ${bleDevice.name} ${bleDevice.macAddress}")
connect()
scanSubscription.dispose()
// Process scan result here.
},
{ throwable ->
Log.i("BLE", "ERROR SCAN ${throwable.localizedMessage}")
// Handle an error here.
}
)
}
private lateinit var connectDisposable: Disposable
private fun connect() {
val device = bleDevice
connectDisposable = device.establishConnection(false) // <-- autoConnect flag
.subscribe(
{ rxBleConnection ->
Log.i("BLE", "CONNECT ${rxBleConnection.mtu}")
read(device)
connectDisposable.dispose()
// All GATT operations are done through the rxBleConnection.
},
{ throwable ->
Log.i("BLE", "ERROR CONNECT ${throwable.localizedMessage}")
// Handle an error here.
}
)
}
private fun read(device: RxBleDevice) {
val dis = device.establishConnection(false)
.flatMapSingle { rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid) }
.subscribe(
{ characteristicValue ->
Log.i("BLE", "READ $characteristicValue")
// Read characteristic value.
},
{ throwable ->
Log.i("BLE", "ERROR READ ${throwable.localizedMessage}")
// Handle an error here.
}
)
}
制造商关于您正在使用的蓝牙设备的 manual/info 可能应该提及特性的 UUID。
根据我的经验,我们的团队有一位工程师,他为我们提供了有关设备的 ble 服务和特性及其 UUID 的文档。
您可以使用以下代码获取您设备中的所有 ble 服务、特征及其 UUID
rxBleConnection.discoverServices()
.map(rxBleDeviceServices -> {
List<BluetoothGattService> services = rxBleDeviceServices.getBluetoothGattServices();
for (BluetoothGattService service : services) {
// here you can work with service's uuid
String serviceUuid = service.getUuid().toString();
// or with all characteristics in service
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
// here you have your characteristic's UUID
String characteristicUuid = characteristic.getUuid().toString();
}
});
然后保存找到的 BluetoothGattCharacteristic 或 UUID 之一并将其传递给 rxBleConnection.readCharacteristic(bluetoothGattCharacteristic)
我使用 RxAndroidBle https://github.com/Polidea/RxAndroidBle 库来处理 ble 设备。但是我在查找 characteristicUuid.
时遇到问题我从 README 做了所有的事情,但是找不到 characteristicUuid。在read()中需要执行.flatMapSingle { rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid) }
,但是没有characteristicUuid。我在哪里可以买到它?
private lateinit var rxBleClient: RxBleClient
private lateinit var bleDevice: RxBleDevice
private lateinit var characteristicUuid: UUID
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getLocationPermission()
rxBleClient = RxBleClient.create(this)
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
button.setOnClickListener { discover() }
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_OK) {
}
}
private lateinit var scanSubscription: Disposable
private fun discover() {
Log.i("BLE", "start scan")
scanSubscription = rxBleClient.scanBleDevices(
ScanSettings.Builder()
// .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
// .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
.build()
// add filters if needed
)
.subscribe(
{ scanResult ->
bleDevice = scanResult.bleDevice
Log.i("BLE", "SCAN ${bleDevice.name} ${bleDevice.macAddress}")
connect()
scanSubscription.dispose()
// Process scan result here.
},
{ throwable ->
Log.i("BLE", "ERROR SCAN ${throwable.localizedMessage}")
// Handle an error here.
}
)
}
private lateinit var connectDisposable: Disposable
private fun connect() {
val device = bleDevice
connectDisposable = device.establishConnection(false) // <-- autoConnect flag
.subscribe(
{ rxBleConnection ->
Log.i("BLE", "CONNECT ${rxBleConnection.mtu}")
read(device)
connectDisposable.dispose()
// All GATT operations are done through the rxBleConnection.
},
{ throwable ->
Log.i("BLE", "ERROR CONNECT ${throwable.localizedMessage}")
// Handle an error here.
}
)
}
private fun read(device: RxBleDevice) {
val dis = device.establishConnection(false)
.flatMapSingle { rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid) }
.subscribe(
{ characteristicValue ->
Log.i("BLE", "READ $characteristicValue")
// Read characteristic value.
},
{ throwable ->
Log.i("BLE", "ERROR READ ${throwable.localizedMessage}")
// Handle an error here.
}
)
}
制造商关于您正在使用的蓝牙设备的 manual/info 可能应该提及特性的 UUID。 根据我的经验,我们的团队有一位工程师,他为我们提供了有关设备的 ble 服务和特性及其 UUID 的文档。
您可以使用以下代码获取您设备中的所有 ble 服务、特征及其 UUID
rxBleConnection.discoverServices()
.map(rxBleDeviceServices -> {
List<BluetoothGattService> services = rxBleDeviceServices.getBluetoothGattServices();
for (BluetoothGattService service : services) {
// here you can work with service's uuid
String serviceUuid = service.getUuid().toString();
// or with all characteristics in service
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
// here you have your characteristic's UUID
String characteristicUuid = characteristic.getUuid().toString();
}
});
然后保存找到的 BluetoothGattCharacteristic 或 UUID 之一并将其传递给 rxBleConnection.readCharacteristic(bluetoothGattCharacteristic)