1

我一直在 google 的 nexus 6&9 上进行 BLE 读写。

我想在远程设备上写一个特性,并在写入后通知远程设备。

以下代码写入特征

if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
    byte[] test = hexStringToByteArray("3132333435363738393031323334353637383930");
    //byte[] value = new byte[1];
    //value[0] = (byte) (21 & 0xFF);
    Log.d("text",characteristic.getDescriptors().size()+"");
    BluetoothGattDescriptor descriptor1 =  characteristic.getDescriptor(UUID.fromString(serviceOneCharUuid));
    descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    characteristic.setValue(test);
    if(mBluetoothGatt.writeDescriptor(descriptor1)){Log.d("text","writedescriptor");}
    if(mBluetoothGatt.writeCharacteristic(characteristic)){Log.d("text","setValue");}}

我看过一些帖子告诉我先写描述符,但它不起作用。

我尝试写入 BLE USB 设备,它显示写入功能按预期工作。而且,本地设备上的回调函数也可以正常工作。

但是,远程设备上的oncharacteristicchanged函数没有被调用这是写在一个名为BluetoothLeService的类中的所有回调函数的代码

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status){
        Log.d("text",new String(characteristic.getValue()));
    }
    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            Log.d("text","on read");
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        Log.d("text", new String(characteristic.getValue()));
    }

任何人都知道这些代码可能有什么问题,或者我错过了什么?

4

1 回答 1

0

我在调用 onCharacteristicChanged 时遇到了类似的问题。我让它工作的方式是等到调用 onDescriptorWrite 之后再将特征写入 BLE 设备。

于 2015-07-17T08:32:48.803 回答