Android BLE API:多个通知

时间:2017-06-05 07:27:41

标签: android notifications bluetooth-lowenergy

根据谷歌BLE示例代码,我设计了一个Android应用程序,它需要连接另一个BLE设备(TI CC2640设备),该协议有两个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中进行处理。

1 个答案:

答案 0 :(得分:1)

@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);
}

我的错误,我不应该修改通知功能。

相关问题