2017-06-05 21 views
0

Google BLEのサンプルコードによれば、アンドロイドアプリケーションを設計し、別のBLEデバイス(TI CC2640デバイス)を接続する必要があります。プロトコルには2つのUUID、 があるため、SampleGattAttributes.javaこのように:Android 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"; 

今私は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で処理する必要があります。

+0

2つ目の特性で通知を有効にする前に、onDescriptorWriteコールバックが呼び出されるまで待つことはできますか?一度に1つの未処理操作しか持つことはできません。 – 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); 
} 

私のミス、私は通知機能を見直すべきではありません。

関連する問題