gpt4 book ai didi

android - BluetoothGattDescriptor 始终为 NULL

转载 作者:行者123 更新时间:2023-11-30 01:43:29 24 4
gpt4 key购买 nike

我的 CLIENT_CHARACTERISTIC_CONFIG 是

public final static UUID CLIENT_CHARACTERISTIC_CONFIG =  UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

我这样做是为了改变特性:

            BluetoothGatt mBluetoothGatt = device.connectGatt(this.context, false, mGattCallback);
mBluetoothGatt.connect();


BluetoothGattService mCustomService = bleScanner.getBluetoothGatt().getService(UUID.fromString(bleServiceUUID));
if(mCustomService == null){
return;
}

BluetoothGattCharacteristic mCharacteristic = mCustomService.getCharacteristic(UUID.fromString(bleCharacteristicUUID));
if(mReadCharacteristic == null){
return;
}
String message = "myMessage";
mCharacteristic.setValue(message.getBytes());

if (!mBluetoothGatt.writeCharacteristic(characteristic)) {
Log.e(TAG, "Failed to write characteristic");
} else {
Log.e(TAG, "Writed characteristic " + message);
}

在传感器上更改characteristic 之后,onCharacteristicWriteBluetoothGattCallback 中调用

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

if (status == BluetoothGatt.GATT_SUCCESS) {

if (!mBluetoothGatt.setCharacteristicNotification(characteristic, true)) {
DLog.e(TAG, "Notification Setup failed: "+characteristic.getUuid());
}

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
if (descriptor!=null){
descriptor.setValue( BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor1);
}
}
}

但是 descriptor 总是 NULL。并且永远不会调用 onCharacteristicChanged。<强>为什么?我的 CLIENT_CHARACTERISTIC_CONFIG 正确吗?

最佳答案

这是因为某些设备(我的是三星 E5 Android 4.3->更新到 5.1.1)支持 BLE 但不支持 BluetoothGattDescriptor 0x2902 (见链接)

要查看设备中支持的所有描述符,请调用此命令:

for (BluetoothGattDescriptor descriptor:characteristic.getDescriptors()){
Log.e(TAG, "BluetoothGattDescriptor: "+descriptor.getUuid().toString());
}

[更新]部分 BLE 设备不支持Writed characteristic,例如iBeacon

关于android - BluetoothGattDescriptor 始终为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34090160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com