首页 > 解决方案 > 将 gatt 特性写入 BLE 设备时出现问题

问题描述

我正在尝试使用Google 的示例项目将自定义特征写入 BLE 设备。

DeviceControlActivity我循环遍历获取BluetoothGattService的 s 时,我尝试为其中一个设置特征。在这一点上,我只是随机挑选一个来测试。

private void displayGattServices(List<BluetoothGattService> gattServices) {
    ...

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        uuid = gattService.getUuid().toString();
        if(uuid.equals("00001800-0000-1000-8000-00805f9b34fb") && !firstTime){
            firstTime = true;
            BluetoothGattCharacteristic customChar = new BluetoothGattCharacteristic(MY_CHARACTERISTIC,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
            BluetoothGattCharacteristic.PERMISSION_READ |BluetoothGattCharacteristic.PERMISSION_WRITE);

            byte[] val = new byte[20];
            val[0] = 71;
            val[1] = 97;
            val[2] = 108;
            val[3] = 97;
            val[4] = 120;
            val[5] = 121;
            val[6] = 32;
            val[7] = 70;
            val[8] = 105;
            val[9] = 116;
            val[10] = -30;
            val[11] = -109;
            val[12] = -108;
            val[13] = 32;
            val[14] = 40;
            val[15] = 70;
            val[16] = 57;
            val[17] = 66;
            val[18] = 57;
            val[19] = 41;
            customChar.setValue(val);
            boolean isAdded = gattService.addCharacteristic(customChar);
            Log.d(TAG,"CARAC ADDED? "+isAdded);
        }

    ...
}

此调用返回 true,根据文档,这意味着写入操作成功,但是当我重新扫描服务(不执行上面的代码)时,当我尝试读取所述特征的值时,我也addCharacteristic找不到状态 135 BluetoothGattCharacteristic. 根据这个帖子的意思是GATT_ILLEGAL_PARAMETER

请注意,字节数组实际上是从另一个特征复制的,因此通常它应该可以解决问题。

我尝试写入的设备是否可能不支持写入?还是服务本身?

编辑:

按照评论的建议,我尝试使用下面的代码修改现有特征但没有成功(我认为该特征是使用 READ only 标志创建的?)

for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            ...
            uuid = gattCharacteristic.getUuid().toString();
            // UUID for device name
            if(uuid.equals("00002a00-0000-1000-8000-00805f9b34fb") && !firstTime){
                firstTime = true;
                gattCharacteristic.setValue("Hello");
               boolean isAdded = mBluetoothLeService.writeCharacteristic(gattCharacteristic);
                Log.d(TAG,"CARAC ADDED? "+isAdded); // returns false
            }

我还尝试设置GattServer并创建具有新特性的新服务,但也没有任何成功。这是代码(这部分使用 Kotlin):

mBluetoothGattServer = mBluetoothManager?.openGattServer(this@MainActivity,gattCallback)
        mBluetoothGattServer?.connect(result.device,false)

val gattCallback = object: BluetoothGattServerCallback() {
    override fun onConnectionStateChange(device: BluetoothDevice?, status: Int, newState: Int) {
        super.onConnectionStateChange(device, status, newState)
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            val service = BluetoothGattService(UUID.fromString("f000aa01-0451-4000-b000-000000000000"), BluetoothGattService.SERVICE_TYPE_PRIMARY)
            val characteristic = BluetoothGattCharacteristic(UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb"), 
                    BluetoothGattCharacteristic.PROPERTY_READ or BluetoothGattCharacteristic.PROPERTY_WRITE or BluetoothGattCharacteristic.PROPERTY_NOTIFY, 
                    BluetoothGattCharacteristic.PERMISSION_READ or BluetoothGattCharacteristic.PERMISSION_WRITE)
            characteristic.addDescriptor(BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"), BluetoothGattCharacteristic.PERMISSION_WRITE))
            service.addCharacteristic(characteristic)
            mBluetoothGattServer?.addService(service)
         }
    }
    // Not sure if this is needed but it never triggers.
    override fun onCharacteristicWriteRequest(device: BluetoothDevice?, requestId: Int, characteristic: BluetoothGattCharacteristic?, preparedWrite: Boolean, responseNeeded: Boolean, offset: Int, value: ByteArray?) {
        super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value)
        mBluetoothGattServer?.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, "Hello".toByteArray())
    }

标签: androidbluetooth-lowenergygatt

解决方案


推荐阅读