您好,我正在尝试从BLE血糖仪读取数据。当我尝试读取“ 00002a18-0000-1000-8000-00805f9b34fb”的特征(只不过是BLOOD_GLUCOSE_MEASUREMENT UUID)时,character.getProperties方法返回16,而我的onCharacteristicRead方法本身未得到调用。请帮助我阅读BLOOD_GLUCOSE_MEASUREMENT特征。
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
return true;
}
return false;
}
};
我的readCharacteristic方法是
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
final byte[] data = characteristic.getValue();
if(data != null && data.length > 0){
final char[] out = new char[data.length * 3 - 1];
for(int j = 0; j < data.length; j++) {
int v = data[j] & 0xFF;
out[j * 3] = HEX_ARRAY[v >>> 4];
out[j * 3 + 1] = HEX_ARRAY[v & 0x0F];
if(j != data.length - 1)
out[j * 3 + 2] = '-';
}
}
mBluetoothGatt.readCharacteristic(characteristic);
}
我的setCharacteristicNotification方法是这样的:
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
for (BluetoothGattService service : mBluetoothGatt.getServices()) {
if ((service == null) || (service.getUuid() == null)) {
continue;
}
if (SampleGattAttributes.BLOOD_GLUCOSE_SERVICE.equalsIgnoreCase(service
.getUuid().toString())) {
BluetoothGattCharacteristic charGM =
mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
.getCharacteristic(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_MEASUREMENT));
mBluetoothGatt.setCharacteristicNotification(charGM, enabled);
BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descGM);
BluetoothGattCharacteristic charGMC =
mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
.getCharacteristic(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_MEASUREMENT_context));
mBluetoothGatt.setCharacteristicNotification(charGMC, enabled);
BluetoothGattDescriptor descGMC = charGMC.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descGMC.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descGMC);
BluetoothGattCharacteristic charRACP =
mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.BLOOD_GLUCOSE_SERVICE))
.getCharacteristic(UUID.fromString(SampleGattAttributes.RECORD_ACCESS_CONTROL_POINT));
mBluetoothGatt.setCharacteristicNotification(charRACP, enabled);
BluetoothGattDescriptor descRACP = charRACP.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descRACP.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(descRACP);
/*BluetoothGattCharacteristic charBarrery =
mBluetoothGatt.getService(UUID.fromString(SampleGattAttributes.SERVICE_BATTERY_SERVICE))
.getCharacteristic(UUID.fromString(SampleGattAttributes.CHAR_BATTERY_LEVEL));
mBluetoothGatt.setCharacteristicNotification(charBarrery, enabled);
BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descBarrery);*/
/* runOnUiThread(new Runnable() {
public void run() {
btnUpdateData.setEnabled(true);
};
});*/
}
}
}
答案 0 :(得分:0)
characteristic.getProperties()
返回16,
这是BluetoothGattCharacteristic.PROPERTY_NOTIFY
的值。
您已设置特征通知,
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
您需要立即致电readCharacteristic(desiredCharacteristic)
。
或/并在onDescriptorWrite()
回调上。
编辑:
致电
mBluetoothGatt.readCharacteristic(characteristic);
您需要在onCharacteristicRead()
回调中接收数据
onCharacteristicRead(BluetoothGatt gatt, ..., ...){
final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
final byte[] data = characteristic.getValue();
if(data != null && data.length > 0){
final char[] out = new char[data.length * 3 - 1];
for(int j = 0; j < data.length; j++) {
int v = data[j] & 0xFF;
out[j * 3] = HEX_ARRAY[v >>> 4];
out[j * 3 + 1] = HEX_ARRAY[v & 0x0F];
if(j != data.length - 1)
out[j * 3 + 2] = '-';
}
}
}