2016-11-18 10 views
0

BLE特性値の書き込みに問題があります。バイト配列を正しく送信しますが、メジャーは変更されていません。私はいくつかの特徴があり、それを読むのに問題はありません。しかし、それらのすべてに新しい価値を書くことには問題があります。これは私の "読み" の部分である:アンドロイド| 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回目と3回目に再び表示され、古い値が再び設定されます。奇妙だが、何が起こるか」。私が間違っていることは何か考えていますか?前もって感謝します!

答えて

2

一度に1つのGATT操作しか実行できません(ディスクリプタ/特性の読み取り/書き込み)。次のリクエストを送信できるようになるまで、対応する完了コールバック(onCharacteristicReadなど)を待つ必要があります。

関連する問題