如何解决Android BLE App 只能订阅 1 个 BLE 设备的特性通知
我是 Android 新手。我正在尝试从 2 个 BLE 设备订阅特征通知。现在,我的应用程序只能从第一个设备接收 BLE 数据。 我知道 BLE 是串行通信,所以我必须等到 onDescriptorWrite() 回调被调用,然后才能为第二个设备启用通知。我们每次只能有 1 个 GATT 操作。 我的问题是:
-
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,"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,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);
我认为应该是:
mBluetoothGatt1.setCharacteristicNotification(characteristic,enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt1.writeDescriptor(descriptor);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。