android | BLE将值写入特征

时间:2016-11-18 09:47:20

标签: android bluetooth-lowenergy

我在为BLE特性写入值时遇到了问题。我发送正确的字节数组,但主要没有被更改。我的特征很少,读它们没有问题。然而,写入每个新值都存在问题。这是我的“阅读”部分:

doc.findAll('img', {'class': 'vvvv'})

为特征写新值我有方法:

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

我在日志中发现,当我设置新值时,每个特征都会再次读取...两次,而不是第三次再次读取,这就是旧值再次设置的时候。奇怪,但发生了什么'。知道我做错了什么吗?提前谢谢!

1 个答案:

答案 0 :(得分:2)

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