2016-11-18 113 views
0

我有写入BLE特征值的问题。我发送正确的字节数组,但主要是没有改变。我没有几个特征,阅读它们没有问题。但是,向每个人写入新的价值都有一个问题。这是我的 “阅读” 部分:android | 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); 
    } 
} 

我发现在日志中,当我设置新的价值时,每个特征都会再次读取......两次,而且还是第三次读取,这就是当旧的价值被再次设置时。奇怪,但发生了什么'。任何想法我做错了什么?提前致谢!

回答

2

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