首页 > 解决方案 > onCharacteristicWrite 中的 BLE Gatt 状态 133

问题描述

我正在尝试连接到一个 BLE 设备,该设备的一个特征是写入数据请求,另一个特征是发送带有所述数据的通知。我可以毫无问题地写入 CCCD,但是当我写入写入特性时,10 次中有 9 次出现 gatt 状态 133 错误。我检查了属性,设置了写入类型,并在每个命令之前添加了 600 毫秒的等待时间,但似乎没有任何帮助。

public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                    super.onDescriptorWrite(gatt, descriptor, status);
                    Log.d(MY_TAG, "Wrote " + descriptor.getUuid() + " with status " + status);
                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        Log.d(MY_TAG, "Write Successful");
                        enableAllNotifications();
                        try {
                            writeData(new byte[] {4, 0, 36});
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        Log.d(MY_TAG, "Write failed");
                    }
                }
            }; 

enableAllNotifications() 只是在我这边设置通知,因为 onCharacteristicChanged() 似乎只有在两者都设置了通知时才被调用。

private void writeData(byte[] message) throws InterruptedException {
        BluetoothGattCharacteristic characteristic = mainActivity.deviceServices.get(0)
                .getCharacteristic(ActiCharacteristicUuids.WriteUUID);
        characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
        characteristic.setValue(message);
        TimeUnit.MILLISECONDS.sleep(600);
        bluetoothGatt.writeCharacteristic(characteristic);
    }

public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
        Log.d(MY_TAG, "Inside onCharactersiticWrite with status: " + status);
        if (status != BluetoothGatt.GATT_SUCCESS) {
             Log.d(MY_TAG, "Write failed");
             retryWrite(characteristic);
        } else {
             Log.d(MY_TAG, "Write Successful");
             gatt.readCharacteristic(characteristic);
        }
}

我不知道这是否有帮助,但我一直在用三星 Galaxy S10+ 和三星 Galaxy S8 Active 进行测试,两者似乎都有这些问题。我没有其他设备可以测试这是否只是三星问题。

标签: javaandroidbluetoothbluetooth-lowenergyandroid-bluetooth

解决方案


我不知道为什么会这样,但我在主要活动中调用了 writeDescriptor。出于某种原因,将该调用更改为 readDescriptor,然后在 readDescriptor 中写入 CCC 即可解决问题。我猜是线程问题?因此,如果其他人遇到此问题,请尝试将所有写入(包括 writeDescriptor 和 writeCharacteristic)放在同一个线程上。


推荐阅读