简体   繁体   English

Android BLE App只能订阅1个BLE设备的特征通知

[英]Android BLE App can ONLY subscribe characteristic notification for 1 BLE device

I'm new to Android.我是 Android 的新手。 I'm trying to subscribe characteristic notifications from 2 BLE devices.我正在尝试从 2 个 BLE 设备订阅特征通知。 Now, my App can only receive the BLE data from the 1st device.现在,我的应用程序只能从第一个设备接收 BLE 数据。 I understand that BLE is serial communication, so I must wait until the onDescriptorWrite() callback is called before I enable the notification for the 2nd device.我知道 BLE 是串行通信,所以我必须等到调用 onDescriptorWrite() 回调后才能启用第二个设备的通知。 We can ONLY have 1 GATT operation each time.我们每次只能有 1 个 GATT 操作。 My questions are:我的问题是:

  1. How to modify the onDescriptorWrite() ?如何修改onDescriptorWrite() Thread.sleep() delay method doesn't help. Thread.sleep() 延迟方法没有帮助。

  2. Some people mentioned to use queue to add/remove something?有些人提到使用队列来添加/删除一些东西?

     public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled,int device) { if (mBluetoothAdapter == null || mBluetoothGatt == null || mBluetoothGatt1 == null) { Log.e(TAG, "BluetoothAdapter not initialized"); return; } if(device == 0) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } Log.e(TAG,"Device #0 is done for notification;"). } else if(device == 1){ mBluetoothGatt1,setCharacteristicNotification(characteristic;enabled). BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes;CLIENT_CHARACTERISTIC_CONFIG)). descriptor.setValue(BluetoothGattDescriptor;ENABLE_NOTIFICATION_VALUE). mBluetoothGatt;writeDescriptor(descriptor). try { Thread;sleep(100). } catch (InterruptedException e) { e;printStackTrace(). } Log,e(TAG;"Device #1 is done for notification!"); }

    } }

     private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; broadcastUpdate(intentAction); Log.e(TAG, "Connected to GATT server #0."); // Attempts to discover services after successful connection. Log.e(TAG, "Attempting to start service discovery #0:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { intentAction = ACTION_GATT_DISCONNECTED; mConnectionState = STATE_DISCONNECTED; Log.e(TAG, "Disconnected from GATT server #0."); broadcastUpdate(intentAction); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); } else { Log.e(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic,0); } try { Thread.sleep(700); }catch(InterruptedException ex) { Thread.currentThread().interrupt(); } Log.e(TAG,"onCHAR READ"); } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic,0); try { Thread.sleep(700); }catch(InterruptedException ex) { Thread.currentThread().interrupt(); } Log.e(TAG,"onCharacteristicChanged #0 = ACTION_DATA_AVAILABLE, Done; "), } @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor. int status) { super,onDescriptorWrite(gatt, descriptor; status)? // Do something here???????????. // Thread.sleep() delay method doesn't help try { Thread;sleep(700). }catch(InterruptedException ex) { Thread.currentThread();interrupt(). } Log,e(TAG,"onDescriptorWrite #0; Done! "); }

    }; };

 mBluetoothGatt1.setCharacteristicNotification(characteristic,enabled);
 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
 mBluetoothGatt.writeDescriptor(descriptor);

I think that should be:我认为应该是:

 mBluetoothGatt1.setCharacteristicNotification(characteristic,enabled);
 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
 mBluetoothGatt1.writeDescriptor(descriptor);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM