读取特征和设置通知之间的区别
Difference between Reading a characteristic and setting up notifications
您好,我正在 android 应用中开发低功耗蓝牙
每当我在应用程序中调用我的 readCharacter()
时,我都能读取这些特征
但我想启用通知,以便在有可用数据时我可以阅读。
目前我正在使用下面的方法。
private byte[] readCharacteristic(){
if(!isReadEnabled){
enableTXNotification();
isReadEnabled = true;
}
byte[] arr = new byte[CHUCK_SIZE];
BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
if(RxService == null){
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return arr;
}
BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
if(TxChar == null){
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return arr;
}
arr = TxChar.getValue();
Log.d(TAG,Arrays.toString(arr));
return arr;
}
这是我的 enableTXNotification()
:
private void enableTXNotification(){
if (mBluetoothGatt == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
if (RxService == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
if (TxChar == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
mBluetoothGatt.setCharacteristicNotification(TxChar,true);
BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
所以我的问题是启用通知和阅读特性有什么区别?
如何在数据可用时收到通知
-阅读特性
请求读取给定的 BluetoothGattCharacteristic。读取结果通过 BluetoothGattCallback 异步报告,它会在 onCharacteristicRead 上为您提供结果。它用于获取存储在特定传感器中的 byte[]。就像如果你有 TEMPERATURE_NOTIFY_CHARACTERISTIC_UUID_STRING 并且你阅读了特征你会得到 byte[] (Arrays.toString(characteristic.getValue()))
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.readCharacteristic(characteristic);
}
-启用通知
BLE 应用程序通常要求在设备上的特定特性发生变化时收到通知。此代码段显示如何使用 setCharacteristicNotification() 方法为特征设置通知:
private BluetoothGatt mBluetoothGatt;
BluetoothGattCharacteristic characteristic;
boolean enabled;
...
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
您好,我正在 android 应用中开发低功耗蓝牙
每当我在应用程序中调用我的 readCharacter()
时,我都能读取这些特征
但我想启用通知,以便在有可用数据时我可以阅读。
目前我正在使用下面的方法。
private byte[] readCharacteristic(){
if(!isReadEnabled){
enableTXNotification();
isReadEnabled = true;
}
byte[] arr = new byte[CHUCK_SIZE];
BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
if(RxService == null){
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return arr;
}
BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
if(TxChar == null){
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return arr;
}
arr = TxChar.getValue();
Log.d(TAG,Arrays.toString(arr));
return arr;
}
这是我的 enableTXNotification()
:
private void enableTXNotification(){
if (mBluetoothGatt == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
BluetoothGattService RxService = mBluetoothGatt.getService(RX_SERVICE_UUID);
if (RxService == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
BluetoothGattCharacteristic TxChar = RxService.getCharacteristic(TX_CHAR_UUID);
if (TxChar == null) {
broadcastUpdate(BLUETOOTH_EVENT_SERVICES_NOT_SUPPORTED);
return;
}
mBluetoothGatt.setCharacteristicNotification(TxChar,true);
BluetoothGattDescriptor descriptor = TxChar.getDescriptor(CCCD);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
所以我的问题是启用通知和阅读特性有什么区别?
如何在数据可用时收到通知
-阅读特性 请求读取给定的 BluetoothGattCharacteristic。读取结果通过 BluetoothGattCallback 异步报告,它会在 onCharacteristicRead 上为您提供结果。它用于获取存储在特定传感器中的 byte[]。就像如果你有 TEMPERATURE_NOTIFY_CHARACTERISTIC_UUID_STRING 并且你阅读了特征你会得到 byte[] (Arrays.toString(characteristic.getValue()))
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.readCharacteristic(characteristic);
}
-启用通知 BLE 应用程序通常要求在设备上的特定特性发生变化时收到通知。此代码段显示如何使用 setCharacteristicNotification() 方法为特征设置通知:
private BluetoothGatt mBluetoothGatt;
BluetoothGattCharacteristic characteristic;
boolean enabled;
...
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);