Я хочу считывать артериальное давление с устройства Medisana через Bluetooth на моем устройстве Android.
Вот мой код
override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
super.onServicesDiscovered(gatt, status)
if (status != BluetoothGatt.GATT_SUCCESS) {
Log.e(TAG, "onServicesDiscovered failed")
return
}
val characteristic = gatt?.getService(BLOOD_PRESSURE_SERVICE_UUID)?.
getCharacteristic(BLOOD_PRESSURE_CHARACTERISTICS_UUID)
if (characteristic == null) {
Log.e(TAG, "blood pressure measurement characteristics is not supported by the device")
} else {
gatt.setCharacteristicNotification(characteristic, true)
val descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID)
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE;
gatt.writeDescriptor(descriptor)
if (!gatt.readCharacteristic(characteristic)) {
Log.e(TAG, "Unable to read characteristic")
}
}
}
Вот константы, которые я скопировал с официального сайта Bluetooth
private val BLOOD_PRESSURE_SERVICE_UUID = UUIDUtils.convertFromInteger(0x1810)
private val BLOOD_PRESSURE_CHARACTERISTICS_UUID = UUIDUtils.convertFromInteger(0x2A35)
object UUIDUtils {
fun convertFromInteger(i: Int): UUID {
val MSB = 0x0000000000001000L
val LSB = -0x7fffff7fa064cb05L
val value = (i and -0x1).toLong()
return UUID(MSB or (value shl 32), LSB)
}
}
При выполнении кода в журналах отображается сообщение «Невозможно прочитать характеристику».Когда я отлаживаю readCharacteristic
, он возвращает в строке
public boolean readCharacteristic(BluetoothGattCharacteristic characteristic) {
if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) == 0) {
return false;
}
Таким образом, характеристика не читается, но как сделать ее читабельной.Я новичок в Bluetooth, и все это сбивает меня с толку.