将Gatt特性写入BLE设备时出现问题

时间:2019-09-23 16:37:28

标签: android bluetooth-lowenergy gatt

我正在尝试使用Google's example project将自定义特征写入BLE设备。

DeviceControlActivity上,当我遍历提取的BluetoothGattService时,我尝试为其中一个设置特征。此时,我只是随机选择一个进行测试。

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);
        }

    ...
}

此对addCharacteristic的调用返回true,根据文档,这表示写入操作成功,但是当我重新扫描服务(不执行上述代码)时,我也找不到BluetoothGattCharacteristic尝试读取所说特征的值,我得到一个状态135。根据this post的意思是 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())
    }

0 个答案:

没有答案