2017-06-05 16 views
0

Nach Google BLE Beispielcode, ich entwerfe eine Android-Anwendung, es muss ein anderes BLE-Gerät (TI CC2640 Gerät) verbinden, das Protokoll hat zwei UUID, so in SampleGattAttributes.java, ich füge hinzu dies wie:Android BLE API: mehrere Benachrichtigung

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

jetzt muss ich lesen/schreiben Merkmal in 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); 
} 

dann kann ich nur Benachrichtigungen für die ersten Merkmale erhalten. Wie kann ich in DeviceControlActivity.java Benachrichtigungen von beiden Merkmalen erhalten und Daten anzeigen?

Ich bin hier für lange Zeit stecken geblieben, hoffe jemand kann mir helfen, danke.

In der Tat muss ich BroadcastUpdate Funktion nicht überarbeiten, weil EXTRA_DATA Prozess in DeviceControlActivity sein sollte.

+0

Sind Sie sicher, dass Sie warten, bis der Callback onDescriptorWrite aufgerufen wird, bevor Sie Benachrichtigungen für das zweite Merkmal aktivieren? Sie können jeweils nur eine ausstehende Operation ausführen. – Emil

+0

Tatsächlich warte ich nicht. Ich habe keine Verzögerungsfunktion hinzugefügt. –

+0

Sie müssen auf den Rückruf warten. Sonst wird es nicht funktionieren. – Emil

Antwort

1

@Emil, wie folgt aus:

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

Mein Fehler, ich sollte nicht Benachrichtigungsfunktion überarbeiten.

Verwandte Themen