2017-06-05 52 views
0

根據谷歌BLE示例代碼,我設計了一個android應用程序,它需要連接另一個BLE設備(TI CC2640設備),該協議有兩個UUID,所以在SampleGattAttributes.java中我添加了是這樣的:在BluetoothLeService.javaAndroid BLE API:多個通知

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"; 

現在我需要讀/寫特性 :

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中進行處理。

+0

是否確定要等到onDescriptorWrite回調被調用,然後再啓用第二個特性的通知之前?您一次只能有一項傑出的操作。 – Emil

+0

其實我不等。我不添加任何延遲功能。 –

+0

您需要等待回調。否則它將無法工作。 – Emil

回答

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

我的錯誤,我不應該修改通知功能。