安卓 BLE API : multiple Notification

标签 android notifications bluetooth-lowenergy

根据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/

相关文章:

Java:将数组设置为空以释放内存

android - 基于 LibGdx 的 Android 应用程序中指定区域的 WebView

java - 每次调用函数时如何获取当前 gps 位置?

android - 如何确保 UUID 对于我的 BLE 服务或特性是唯一的?

c - Nordic nRF51822 + Nordic IoT SDK

android - Android 10 (api 29) 中没有这样的文件或目录

android - 我希望在打开应用程序后关闭通知

Android 通知声音未从内存中响起

android - 通知打开 Activity ,按后退按钮,防止打开后退堆栈 Activity ?

Android BLE BluetoothGatt 对象保持连接并触发 onCharacteristicChanged() 即使在调用 disconnect() 之后