根据google BLE Sample code,我设计了一个android应用程序,它需要连接另一个BLE设备(TI CC2640设备),协议(protocol)有两个UUID, 所以在 SampleGattAttributes.java 中,我这样添加:
public static String UBI_ONGOING_INFO_SERVICE_UUID = "3d963d11-d107-4d7d-918f-61ca2aa836af";
public static String UBI_SYSTEM_INFO_SERVICE_UUID = "3d963d13-d107-4d7d-918f-61ca2aa836af";
现在我需要读/写特征 在 BluetoothLeService.java 中:
public final static String EXTRA_DATA_ONGOING =
"com.example.bluetooth.le.EXTRA_DATA_ONGOING";
public final static String EXTRA_DATA_SYSTEM_INFO =
"com.example.bluetooth.le.EXTRA_DATA_SYSTEM_INFO";
public final static UUID UUID_UBI_ONGOING_INFO_SERVICE_UUID =
UUID.fromString(SampleGattAttributes.UBI_ONGOING_INFO_SERVICE_UUID);
public final static UUID UUID_UBI_SYSTEM_INFO_SERVICE_UUID =
UUID.fromString(SampleGattAttributes.UBI_SYSTEM_INFO_SERVICE_UUID);
private void broadcastUpdate(final String action,final BluetoothGattCharacteristic characteristic){
...
...
if (UUID_UBI_SYSTEM_INFO_SERVICE_UUID.equals(characteristic.getUuid())){
final byte[] data1 = characteristic.getValue();
....
....
intent.putExtra(EXTRA_DATA, stringBuilder.toString());
intent.putExtra(EXTRA_DATA_SYSTEM_INFO, strBuilder.toString());
}else{
...
...
intent.putExtra(EXTRA_DATA, stringBuilder.toString());
intent.putExtra(EXTRA_DATA_ONGOING, strBuilder.toString());
}
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,boolean enabled) {
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
if (UUID_UBI_SERVICE_SERVICE_UUID.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(00002902-0000-1000-8000-00805f9b34fb));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
if (UUID_UBI_SYSTEM_INFO_SERVICE_UUID.equals(characteristic.getUuid())){
BluetoothGattDescriptor descriptor1 = characteristic.getDescriptor(00002902-0000-1000-8000-00805f9b34fb);
descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor1);
}
然后,我只能收到第一个特征的通知。 我怎样才能收到来自两个特征的通知并显示数据 在 DeviceControlActivity.java 中?
我卡在这里好久了,希望有人能帮帮我,谢谢。
事实上,我不需要修改 broadcastUpdate 函数,因为 EXTRA_DATA 应该在 DeviceControlActivity 中处理。
最佳答案
@Emil,像这样:
if (UUID_UBI_SERVICE_SERVICE_UUID.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(00002902-0000-1000-8000-00805f9b34fb));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
//add sleep delay 500
try {
Thread.sleep(500);
}catch (InterruptedException e) {
e.printStackTrace();
}
if (UUID_UBI_SYSTEM_INFO_SERVICE_UUID.equals(characteristic.getUuid())){
BluetoothGattDescriptor descriptor1 = characteristic.getDescriptor(00002902-0000-1000-8000-00805f9b34fb);
descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor1);
}
我的错误,我不应该修改通知功能。
关于安卓 BLE API : multiple Notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363826/