gpt4 book ai didi

java - bluetoothGatt.writeCharacteristic 始终返回 false,BLE 特征类型为 write_no_reponse

转载 作者:行者123 更新时间:2023-12-02 01:58:37 25 4
gpt4 key购买 nike

我正在编写一个 Android 应用程序来与 BLE 仪表通信。我已经能够扫描设备、连接到目标、发现服务、获取特征。然而,当我尝试编写 write_no_reponse 特性时,该方法总是返回 false。我确信这些特性是可写的,我使用其他应用程序来做到这一点。当我调试到android.bluetooth代码时,出现以下序列失败,可能它返回一个空服务,调试运行到这一步,然后单步跳出该方法:

BluetoothGattService service = characteristic.getService(); 

这会导致 writeCharacteristic 返回 false。但在 writeCharacteristic 之前,我有一个日志

Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
bluetoothGatt.writeCharacteristic(mCharacteristic);

且日志正确,不返回null;

非常感谢任何帮助!

我使用相同的代码来写入 WRITE 特性,它起作用了。我尝试使用

mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

设置写入类型,但也失败。

写入数据代码:

public void writeReadDataCommand() {
if (null != bluetoothGatt) {
mCharacteristic.setValue("123123");
bluetoothGatt.setCharacteristicNotification(mCharacteristic, true);
mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
Log.d(TAG, "onServicesDiscovered: writeType" + mCharacteristic.getService());
boolean isWrite = bluetoothGatt.writeCharacteristic(mCharacteristic);
if (isWrite) {
Log.d(TAG, "writeReadDataCommand: write data to BLE");
}
}
}

onServiceDiscovered 代码:

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);

if (status == BluetoothGatt.GATT_SUCCESS) {
bluetoothGatt = gatt;
BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE_UUID));
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));
Log.d(TAG, "onServicesDiscovered: writeType" + characteristic.getWriteType());

boolean isSuccess = gatt.setCharacteristicNotification(characteristic, true);
if (isSuccess) {
mCharacteristic = characteristic;
List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
if (null != descriptors && descriptors.size() > 0) {
for (BluetoothGattDescriptor descriptor : descriptors) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
}
connectCallback.findService();
} else {
Log.d(TAG, "onServicesDiscovered received: " + status);
}
}

并且 onCharacteristicWrite 回调不会触发

public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, "onCharacteristicWrite: " + Arrays.toString(characteristic.getValue()));
}

我有这个错误很长时间了,非常感谢,如果有人能帮忙。

最佳答案

characteristics

如图所示,我的服务中有 2 个特征 - 0 和 1。这些特征具有相同的 uuid。我做的第一件事是

BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(CHARACTERISTIC_UUID));

这段代码找到了0特征,然后我向其中写入数据。显然,我失败了。因为0特性只能READ和NOTIFY。所以我执行以下步骤将日期写入 BLE

                characteristics = service.getCharacteristics();
for(BluetoothGattCharacteristic characteristic : characteristics){
if(characteristic.getUuid().equals(UUID.fromString(CHARACTERISTIC_UUID))){
enableNotification(characteristic);
}
}
            for(BluetoothGattCharacteristic characteristic : characteristics){
if(characteristic.getUuid().equals(UUID.fromString(CHARACTERISTIC_UUID))){
characteristic.setValue("123123");
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

boolean isWrite = bluetoothGatt.writeCharacteristic(characteristic);
if (isWrite) {
Log.d(TAG, "writeReadDataCommand: write data to ble");
}
}
}

我尝试通知这两个特征并向这两个特征写入数据,尽管它们具有相同的UUID,但它们具有不同的功能。显然,一种特性将通知失败,一种将写入失败。但最终我成功写入了我的数据。

关于java - bluetoothGatt.writeCharacteristic 始终返回 false,BLE 特征类型为 write_no_reponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57386072/

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