繁体   English   中英

android | BLE向特性写入值

[英]android | BLE write value to characteristic

我在将值写入BLE特性时遇到问题。 我正确地发送了字节数组,但是主要部分未更改。 我的特征很少,阅读它们没有问题。 但是,向他们每个人写入新价值存在一个问题。 这是我的“阅读”部分:

  @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                List<BluetoothGattService> services = gatt.getServices();
                for (BluetoothGattService service : services) {
                    registerService(service);
                    characteristics = new ArrayList<BluetoothGattCharacteristic>();
                    characteristics.add(services.get(4).getCharacteristics().get(0));
                    characteristics.add(services.get(4).getCharacteristics().get(1));
                    characteristics.add(services.get(4).getCharacteristics().get(2));
                    characteristics.add(services.get(4).getCharacteristics().get(3));
                    characteristics.add(services.get(4).getCharacteristics().get(4));
                    characteristics.add(services.get(4).getCharacteristics().get(5));
                    characteristics.add(services.get(4).getCharacteristics().get(6));
                    characteristics.add(services.get(4).getCharacteristics().get(7));
                    characteristics.add(services.get(4).getCharacteristics().get(8));
                    characteristics.add(services.get(4).getCharacteristics().get(9));
                    characteristics.add(services.get(4).getCharacteristics().get(10));
                    characteristics.add(services.get(4).getCharacteristics().get(11));
                    characteristics.add(services.get(5).getCharacteristics().get(0));
                    characteristics.add(services.get(6).getCharacteristics().get(0));
                    if (Characteristics.PARAMETERS_SERVICE_UUID.equals(service.getUuid()))
                        registerParametersCharacteristics(characteristics);
                    Log.e(TAG, "onServicesDiscovered: " + service.getUuid() );
                    requesReadCharacteristics(gatt);
                }
                callback.connectedStateChanged(true);
            } else
                disconnect();

    }

    public void requesReadCharacteristics(BluetoothGatt gatt) {
        if (characteristics.get(characteristics.size() - 1).getUuid().equals(Characteristics.TEMPERATURE_CHARACTERISTIC_UUID)) {
            Log.e(TAG, "requesReadCharacteristics: TRUE");
        }
        if (characteristics.get(characteristics.size() - 1).getUuid().equals(Characteristics.ACCELEROMETER_CHARACTERISTIC_UUID)) {
            Log.e(TAG, "requesReadCharacteristics: TRUE");
        }
        gatt.readCharacteristic(characteristics.get(characteristics.size() - 1));
    }

为了给特性写新的值,我有方法:

public void setMajor(int major) {
        byte[] bytes = new byte[]{(byte) (major & 0xFF), (byte) ((major >> 8) & 0xFF)};
        if (majorCharacteristic != null) {
            BluetoothCommunicationManager.getInstance().add(new WriteCharacteristicCommand(majorCharacteristic, bluetoothGatt, bytes));
            Log.e(TAG, "setMajor:" + Arrays.toString(bytes));
        }
        else
            Log.e(TAG, "setMajor: NU" + Arrays.toString(bytes));
    }

和一个处理它的类:

public class WriteCharacteristicCommand implements BTLECommand {
    private final BluetoothGatt gatt;
    private final BluetoothGattCharacteristic characteristic;
    private final byte[] value;

    public WriteCharacteristicCommand(BluetoothGattCharacteristic characteristic, BluetoothGatt gatt, byte[] value) {
        this.gatt = gatt;
        this.characteristic = characteristic;
        this.value = value;
    }

    @Override
    public void process() {
        characteristic.setValue(value);
        gatt.writeCharacteristic(characteristic);
    }
}

我在日志中发现,当我设置新值时,每个特性都将再次读取...两次,然后第三次读取,这是在重新设置旧值时。 奇怪,但是发生了什么。 知道我在做什么错吗? 提前致谢!

一次只能执行一项出色的GATT操作(读/写描述符/特征)。 您需要等待相应的完成回调(onCharacteristicRead等),直到您可以发送下一个请求为止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM