Android 关于葡萄糖的可撤销通知
我厌倦了使用Android BLE SDK与我的葡萄糖设备通信。 我需要UUID 2a18和2a34的setCharacteristicNotification。我对Android官方SDK的参考如下:Android 关于葡萄糖的可撤销通知,android,notifications,bluetooth-lowenergy,android-notifications,Android,Notifications,Bluetooth Lowenergy,Android Notifications,我厌倦了使用Android BLE SDK与我的葡萄糖设备通信。 我需要UUID 2a18和2a34的setCharacteristicNotification。我对Android官方SDK的参考如下: public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"onCharacteristicChanged",Toast.LENGTH_LONG).show();
setProgressBarIndeterminateVisibility(false);
};
});
}
07-29 10:28:17.924: D/BluetoothGatt(947): setCharacteristicNotification() - uuid: 00002a19-0000-1000-8000-00805f9b34fb enable: true
07-29 10:28:17.924: D/BluetoothGatt(947): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:28:18.484: D/BluetoothGatt(947): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
07-29 10:28:18.604: D/BluetoothGatt(947): onNotify() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
但它甚至不能进入onCharacteristicChanged回调
我的OnCharacteristic更改如下:
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"onCharacteristicChanged",Toast.LENGTH_LONG).show();
setProgressBarIndeterminateVisibility(false);
};
});
}
07-29 10:28:17.924: D/BluetoothGatt(947): setCharacteristicNotification() - uuid: 00002a19-0000-1000-8000-00805f9b34fb enable: true
07-29 10:28:17.924: D/BluetoothGatt(947): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:28:18.484: D/BluetoothGatt(947): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
07-29 10:28:18.604: D/BluetoothGatt(947): onNotify() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
如果我按如下方式设置电池电量通知,它会工作
BluetoothGattCharacteristic charBarrery =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_BATTERY))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_BATTERY_LEVEL_STRING));
mConnGatt.setCharacteristicNotification(charBarrery, enabled);
BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descBarrery);
我不知道电池和葡萄糖有什么不同
如果有人知道我该做什么,请帮助我
附加:
当我使用电池服务时,我的日志如下:
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"onCharacteristicChanged",Toast.LENGTH_LONG).show();
setProgressBarIndeterminateVisibility(false);
};
});
}
07-29 10:28:17.924: D/BluetoothGatt(947): setCharacteristicNotification() - uuid: 00002a19-0000-1000-8000-00805f9b34fb enable: true
07-29 10:28:17.924: D/BluetoothGatt(947): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:28:18.484: D/BluetoothGatt(947): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
07-29 10:28:18.604: D/BluetoothGatt(947): onNotify() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
但当我使用葡萄糖时,我的日志会丢失onNotify(),如下所示:
07-29 10:31:23.729: D/BluetoothGatt(1763): setCharacteristicNotification() - uuid: 00002a18-0000-1000-8000-00805f9b34fb enable: true
07-29 10:31:23.729: D/BluetoothGatt(1763): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:31:24.324: D/BluetoothGatt(1763): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a18-0000-1000-8000-00805f9b34fb
我不知道为什么logcat会丢失onNotify()
附加(8/4):
谢谢你的回复!
我尝试在记录访问控制点特性上启用指示,但失败。。
我的过程如下:
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"onCharacteristicChanged",Toast.LENGTH_LONG).show();
setProgressBarIndeterminateVisibility(false);
};
});
}
07-29 10:28:17.924: D/BluetoothGatt(947): setCharacteristicNotification() - uuid: 00002a19-0000-1000-8000-00805f9b34fb enable: true
07-29 10:28:17.924: D/BluetoothGatt(947): writeDescriptor() - uuid: 00002902-0000-1000-8000-00805f9b34fb
07-29 10:28:18.484: D/BluetoothGatt(947): onDescriptorWrite() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
07-29 10:28:18.604: D/BluetoothGatt(947): onNotify() - Device=B4:AB:2C:06:9E:F4 UUID=00002a19-0000-1000-8000-00805f9b34fb
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
for (BluetoothGattService service : gatt.getServices()) {
if ((service == null) || (service.getUuid() == null)) {
continue;
}
if (BleUuid.SERVICE_GLUCOSE.equalsIgnoreCase(service
.getUuid().toString())) {
BluetoothGattCharacteristic charGM =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_GLUCOSE_MEASUREMENT_STRING));
mConnGatt.setCharacteristicNotification(charGM, enabled);
BluetoothGattDescriptor descGM = charGM.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descGM.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descGM);
BluetoothGattCharacteristic charGMC =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_GLUCOSE_MEASUREMENT_CONTEXT_STRING));
mConnGatt.setCharacteristicNotification(charGMC, enabled);
BluetoothGattDescriptor descGMC = charGMC.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descGMC.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descGMC);
BluetoothGattCharacteristic charRACP =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING));
mConnGatt.setCharacteristicNotification(charRACP, enabled);
BluetoothGattDescriptor descRACP = charRACP.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descRACP.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mConnGatt.writeDescriptor(descRACP);
BluetoothGattCharacteristic charBarrery =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_BATTERY))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_BATTERY_LEVEL_STRING));
mConnGatt.setCharacteristicNotification(charBarrery, enabled);
BluetoothGattDescriptor descBarrery = charBarrery.getDescriptor(UUID.fromString(BleUuid.CHAR_CLIENT_CHARACTERISTIC_CONFIG_STRING));
descBarrery.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mConnGatt.writeDescriptor(descBarrery);
runOnUiThread(new Runnable() {
public void run() {
btnUpdateData.setEnabled(true);
};
});
}
}
};
case R.id.btnUpdateData:
try{
//***SEND 0x0101 TO RECORD ACCESS CONTROL POINT
BluetoothGattCharacteristic writeRACPchar =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING));
byte[] data = new byte[1];
data[0] = (byte)0x0101;
writeRACPchar.setValue(data);
mConnGatt.writeCharacteristic(writeRACPchar);
}catch(Exception e){
e.printStackTrace();
}
break;
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
};
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
感谢阅读。葡萄糖和电池服务在逻辑上存在差异。当您启用电池电量通知时,通常(取决于实现),设备会立即向您发送电池电量。这就是为什么你要去那里。当然,在某些实现中,它仅在值更改时通知您,或者使用其他规则时通知您,但在您的情况下,它的行为类似于此 葡萄糖服务的工作原理不同。在Glucose服务()中,您有3个必需的特征。让我们跳过这里的葡萄糖功能。 其中之一是血糖测量,在这里你可以得到血糖读数通知。您启用这些通知的实现是正确的,它将起作用。但为了获得通知,您必须使用记录访问控制点特性请求它们。它允许您获取所有葡萄糖读数,仅获取最新的、仅获取第一次的、从设备中删除保存的读数等。例如,当您:
case R.id.btnUpdateData:
try{
//***SEND 0x0101 TO RECORD ACCESS CONTROL POINT
BluetoothGattCharacteristic writeRACPchar =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING));
byte[] data = new byte[2];
data[0] = 0x01; // Report Stored records
data[1] = 0x01; // All records
writeRACPchar.setValue(data);
// or:
// byte[] data = new byte[2];
// writeRACPchar.setValue(data);
// writeRACPchar.setIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1, 0); // Report Stored records
// writeRACPchar.setIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1, 1); // Read all (offset 1)
mConnGatt.writeCharacteristic(writeRACPchar);
}catch(Exception e){
e.printStackTrace();
}
break;
您必须写入2个字节,每个字节为8个,但可以写入2位十六进制,f.e.0xAB r 0x01。值0x0101有2个字节,您可能不知道葡萄糖和电池服务在逻辑上存在差异。当您启用电池电量通知时,通常(取决于实现),设备会立即向您发送电池电量。这就是为什么你要去那里。当然,在某些实现中,它仅在值更改时通知您,或者使用其他规则时通知您,但在您的情况下,它的行为类似于此 葡萄糖服务的工作原理不同。在Glucose服务()中,您有3个必需的特征。让我们跳过这里的葡萄糖功能。 其中之一是血糖测量,在这里你可以得到血糖读数通知。您启用这些通知的实现是正确的,它将起作用。但为了获得通知,您必须使用记录访问控制点特性请求它们。它允许您获取所有葡萄糖读数,仅获取最新的、仅获取第一次的、从设备中删除保存的读数等。例如,当您:
case R.id.btnUpdateData:
try{
//***SEND 0x0101 TO RECORD ACCESS CONTROL POINT
BluetoothGattCharacteristic writeRACPchar =
mConnGatt.getService(UUID.fromString(BleUuid.SERVICE_GLUCOSE))
.getCharacteristic(UUID.fromString(BleUuid.CHAR_RECORD_ACCESS_CONTROL_POINT_STRING));
byte[] data = new byte[2];
data[0] = 0x01; // Report Stored records
data[1] = 0x01; // All records
writeRACPchar.setValue(data);
// or:
// byte[] data = new byte[2];
// writeRACPchar.setValue(data);
// writeRACPchar.setIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1, 0); // Report Stored records
// writeRACPchar.setIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 1, 1); // Read all (offset 1)
mConnGatt.writeCharacteristic(writeRACPchar);
}catch(Exception e){
e.printStackTrace();
}
break;
您必须写入2个字节,每个字节为8个,但可以写入2位十六进制,f.e.0xAB r 0x01。值0x0101有2个字节,您可能无法使用哪种葡萄糖设备?使用哪种葡萄糖设备?Hello@philips77,我也在尝试实现同样的功能。你能看看我的问题并帮我继续吗。这里是链接:Hello@philips77,我也在尝试实现同样的东西。你能看看我的问题并帮我继续吗。以下是链接: