我厌倦了使用 Android BLE SDK 与我的 Glucose 设备进行通信。
我需要 UUID 2a18 和 2a34 的 setCharacteristicNotification。我参考Android官方SDK如下:
http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#notification
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);
但它甚至无法进入 onCharacteristicChanged 回调。
我的 onCharacteristicChanged 如下:
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);
};
});
}
如果我按如下方式设置电池电量通知,它就会起作用。
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);
我不知道电池和血糖通知有什么不同。
如果有人知道我应该做什么,请帮助我。
额外的:
当我使用电池服务时,我的logcat如下:
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
但是当我使用 Glucose 时,我的 logcat 丢失了 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 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);
};
});
}
}
};
-
发送0x0101记录访问控制点
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);
}
&
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
if (BleUuid.CHAR_SERIAL_NUMBEAR_STRING
.equalsIgnoreCase(characteristic.getUuid().toString())) {
displayResult(characteristic.getStringValue(0));
}else if(BleUuid.CHAR_MANUFACTURER_NAME_STRING
.equalsIgnoreCase(characteristic.getUuid().toString())){
displayResult(characteristic.getStringValue(0));
} else if(BleUuid.CHAR_BATTERY_LEVEL_STRING
.equalsIgnoreCase(characteristic.getUuid().toString())){
final byte[] data = characteristic.getValue();
String dataStr = "";
dataStr = String.format("%02X", data[0]);
int a = Integer.parseInt(dataStr, 16);
String result = "battery level: " + Integer.toString(a)+ "%";
displayResult(result);
} else {
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
displayResult(stringBuilder.toString());
}
}
}
&
private void displayResult(String result){
adUpdateData.add(result);
runOnUiThread(new Runnable() {
public void run() {
lvUpdateData.setAdapter(adUpdateData);
};
});
}
我试图理解“GLS_SPEC”pdf...
我使用了 Nordic Semiconductor 的葡萄糖服务示例应用程序,它可以工作。
我尝试学习如何实现该功能。
我注意到 LogCat 上显示了一些类似“unregisterIRListener() is called”的日志,但我不确定是否与我的问题有关......
谢谢阅读。