我在将值写入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));
}要将新值写入characteristic,我已经获得了方法:
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);
}
}我在日志中发现,当我设置新的值时,每个特征都会再次读取...两次,然后第三次,那就是旧值再次被设置的时候。奇怪的是,这就是发生的事情。知道我做错了什么吗?提前感谢!
发布于 2016-11-19 18:38:39
一次只能有一个未完成的GATT操作(读/写描述符/特征)。您需要等待相应的完成回调(onCharacteristicRead等),直到可以发送下一个请求。
https://stackoverflow.com/questions/40673843
复制相似问题