Android BluetoothGatt setCharacteristicNotification 仅设置第一个特征
Android BluetoothGatt setCharacteristicNotification only setting first characteristic
我一直无法通过同一个蓝牙 LE 设备同时使用两个不同的特性。它只记录第一个特征而不记录第二个特征。如果我调换它们,它仍然会记录第一个,但不会记录第二个。第一和第二特征都需要注册和接收。如果有帮助,我正在使用 Samsung Galaxy S4 运行 4.4.2.
这是调用 setCharacteristicNotification() 的代码部分
if (mBluetoothBeaconGatt != null && mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE) != null) {
characteristicBracelet = mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE)
.getCharacteristic(UUID_CUSTOM_BRACELET);
characteristicBeacon = mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE)
.getCharacteristic(UUID_CUSTOM_BEACON);
if(characteristicBracelet != null) {
Log.i(TAG, "Init Bracelet Characteristics");
this.setCharacteristicNotification(characteristicBracelet,
true);
}
if(characteristicBeacon != null) {
Log.i(TAG, "Init Beacon Characteristics");
this.setCharacteristicNotification(characteristicBeacon, true);
}
}
setCharacteristicNotification
if (UUID_CUSTOM_BEACON.equals(characteristic.getUuid())) {
if (mBluetoothBeaconGatt != null) {
Log.i(TAG, "Enabling indication for beacon");
mBluetoothBeaconGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic
.getDescriptor(UUID
.fromString(CustomGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor
.setValue((enabled) ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothBeaconGatt.writeDescriptor(descriptor);
}
} else if (UUID_CUSTOM_BRACELET.equals(characteristic.getUuid())) {
if (mBluetoothBraceletGatt != null || mBluetoothBeaconGatt != null) {
BluetoothGatt gatt = (mBluetoothBeaconGatt != null) ? mBluetoothBeaconGatt : mBluetoothBraceletGatt;
gatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic
.getDescriptor(UUID
.fromString(CustomGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor
.setValue((enabled) ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
测试变量是否为空验证它们是否为空。
如果您需要其他信息或这是重复的,请告诉我。
感谢您花时间提供帮助。
您不能同时执行多个蓝牙操作。一旦你获得 onDescriptorWrite
,你就可以 enable/disable 你的下一个特征:
private int index;
private boolean enabled;
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
index++;
setNotification();
}
}
public void notify(boolean enabled){
index = 0;
this.enabled = enabled;
setNotification();
}
private void setNotification(){
BluetoothGattCharacteristic characteristic;
switch(index){
case 0:
characteristic = mBluetoothGatt.getService(MY_SERVICE_UUID)
.getCharacteristic(UUID_CUSTOM_BRACELET);
break;
case 1:
characteristic = mBluetoothGatt.getService(MY_SERVICE_UUID)
.getCharacteristic(UUID_CUSTOM_BEACON);
break;
default:
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
descriptor.setValue((enabled) ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(desc);
}
来自 NewCircle 的精彩教程:https://newcircle.com/s/post/1553/bluetooth_smart_le_android_tutorial
我一直无法通过同一个蓝牙 LE 设备同时使用两个不同的特性。它只记录第一个特征而不记录第二个特征。如果我调换它们,它仍然会记录第一个,但不会记录第二个。第一和第二特征都需要注册和接收。如果有帮助,我正在使用 Samsung Galaxy S4 运行 4.4.2.
这是调用 setCharacteristicNotification() 的代码部分
if (mBluetoothBeaconGatt != null && mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE) != null) {
characteristicBracelet = mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE)
.getCharacteristic(UUID_CUSTOM_BRACELET);
characteristicBeacon = mBluetoothBeaconGatt.getService(UUID_CUSTOM_SERVICE)
.getCharacteristic(UUID_CUSTOM_BEACON);
if(characteristicBracelet != null) {
Log.i(TAG, "Init Bracelet Characteristics");
this.setCharacteristicNotification(characteristicBracelet,
true);
}
if(characteristicBeacon != null) {
Log.i(TAG, "Init Beacon Characteristics");
this.setCharacteristicNotification(characteristicBeacon, true);
}
}
setCharacteristicNotification
if (UUID_CUSTOM_BEACON.equals(characteristic.getUuid())) {
if (mBluetoothBeaconGatt != null) {
Log.i(TAG, "Enabling indication for beacon");
mBluetoothBeaconGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic
.getDescriptor(UUID
.fromString(CustomGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor
.setValue((enabled) ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothBeaconGatt.writeDescriptor(descriptor);
}
} else if (UUID_CUSTOM_BRACELET.equals(characteristic.getUuid())) {
if (mBluetoothBraceletGatt != null || mBluetoothBeaconGatt != null) {
BluetoothGatt gatt = (mBluetoothBeaconGatt != null) ? mBluetoothBeaconGatt : mBluetoothBraceletGatt;
gatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic
.getDescriptor(UUID
.fromString(CustomGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor
.setValue((enabled) ? BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
测试变量是否为空验证它们是否为空。
如果您需要其他信息或这是重复的,请告诉我。
感谢您花时间提供帮助。
您不能同时执行多个蓝牙操作。一旦你获得 onDescriptorWrite
,你就可以 enable/disable 你的下一个特征:
private int index;
private boolean enabled;
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
index++;
setNotification();
}
}
public void notify(boolean enabled){
index = 0;
this.enabled = enabled;
setNotification();
}
private void setNotification(){
BluetoothGattCharacteristic characteristic;
switch(index){
case 0:
characteristic = mBluetoothGatt.getService(MY_SERVICE_UUID)
.getCharacteristic(UUID_CUSTOM_BRACELET);
break;
case 1:
characteristic = mBluetoothGatt.getService(MY_SERVICE_UUID)
.getCharacteristic(UUID_CUSTOM_BEACON);
break;
default:
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
descriptor.setValue((enabled) ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
: BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(desc);
}
来自 NewCircle 的精彩教程:https://newcircle.com/s/post/1553/bluetooth_smart_le_android_tutorial