Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish writing messages in the write queue before moving on… #1268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 43 additions & 41 deletions android/src/main/java/it/innove/Peripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class Peripheral extends BluetoothGattCallback {
private Runnable discoverServicesRunnable;
private boolean commandQueueBusy = false;

private List<byte[]> writeQueue = new ArrayList<>();
private final Queue<byte[]> writeQueue = new LinkedList<>();

public Peripheral(BluetoothDevice device, int advertisingRSSI, byte[] scanRecord, ReactContext reactContext) {
this.device = device;
Expand Down Expand Up @@ -519,27 +519,28 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
super.onCharacteristicWrite(gatt, characteristic, status);

mainHandler.post(() -> {
if (writeQueue.size() > 0) {
byte[] data = writeQueue.get(0);
writeQueue.remove(0);
doWrite(characteristic, data, null);
} else if (status != BluetoothGatt.GATT_SUCCESS) {
if (status == GATT_AUTH_FAIL || status == GATT_INSUFFICIENT_AUTHENTICATION) {
Log.d(BleManager.LOG_TAG, "Write needs bonding");
// *not* doing completedCommand()
return;
}
for (Callback writeCallback : writeCallbacks) {
writeCallback.invoke("Error writing " + characteristic.getUuid() + " status=" + status, null);
}
writeCallbacks.clear();
} else if (!writeCallbacks.isEmpty()) {
for (Callback writeCallback : writeCallbacks) {
writeCallback.invoke();
if (!writeQueue.isEmpty()) {
byte[] data = writeQueue.poll();
doWrite(characteristic, data);
} else {
if (status != BluetoothGatt.GATT_SUCCESS) {
if (status == GATT_AUTH_FAIL || status == GATT_INSUFFICIENT_AUTHENTICATION) {
Log.d(BleManager.LOG_TAG, "Write needs bonding");
// *not* doing completedCommand()
return;
}
for (Callback writeCallback : writeCallbacks) {
writeCallback.invoke("Error writing " + characteristic.getUuid() + " status=" + status, null);
}
writeCallbacks.clear();
} else if (!writeCallbacks.isEmpty()) {
for (Callback writeCallback : writeCallbacks) {
writeCallback.invoke();
}
writeCallbacks.clear();
}
writeCallbacks.clear();
completedCommand();
}
completedCommand();
});
}

Expand Down Expand Up @@ -1056,25 +1057,26 @@ private BluetoothGattCharacteristic findCharacteristic(BluetoothGattService serv
return null;
}

public boolean doWrite(final BluetoothGattCharacteristic characteristic, byte[] data, final Callback callback) {
private void doWrite(final BluetoothGattCharacteristic characteristic, final byte[] data) {
characteristic.setValue(data);
if (!gatt.writeCharacteristic(characteristic)) {
// write without response, caller will handle the callback
for (Callback writeCallback : writeCallbacks) {
writeCallback.invoke("Write failed", null);
}
writeCallbacks.clear();
completedCommand();
}
}

private boolean enqueueWrite(final BluetoothGattCharacteristic characteristic, byte[] data, final Callback callback) {
final byte[] copyOfData = copyOf(data);
return enqueue(new Runnable() {
@Override
public void run() {
characteristic.setValue(copyOfData);
if (characteristic.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
&& callback != null) {
writeCallbacks.addLast(callback);
}
if (!gatt.writeCharacteristic(characteristic)) {
// write without response, caller will handle the callback
for (Callback writeCallback : writeCallbacks) {
writeCallback.invoke("Write failed", null);
}
writeCallbacks.clear();
completedCommand();
}
return enqueue(() -> {
if (characteristic.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
&& callback != null) {
writeCallbacks.addLast(callback);
}
doWrite(characteristic, copyOfData);
});
}

Expand All @@ -1100,7 +1102,7 @@ public void write(UUID serviceUUID, UUID characteristicUUID, byte[] data, Intege
characteristic.setWriteType(writeType);

if (data.length <= maxByteSize) {
if (!doWrite(characteristic, data, callback)) {
if (!enqueueWrite(characteristic, data, callback)) {
callback.invoke("Write failed");
} else {
if (BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE == writeType) {
Expand Down Expand Up @@ -1130,21 +1132,21 @@ public void write(UUID serviceUUID, UUID characteristicUUID, byte[] data, Intege

if (BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT == writeType) {
writeQueue.addAll(splittedMessage);
if (!doWrite(characteristic, firstMessage, callback)) {
if (!enqueueWrite(characteristic, firstMessage, callback)) {
writeQueue.clear();
callback.invoke("Write failed");
}
} else {
try {
boolean writeError = false;
if (!doWrite(characteristic, firstMessage, callback)) {
if (!enqueueWrite(characteristic, firstMessage, callback)) {
writeError = true;
callback.invoke("Write failed");
}
if (!writeError) {
Thread.sleep(queueSleepTime);
for (byte[] message : splittedMessage) {
if (!doWrite(characteristic, message, callback)) {
if (!enqueueWrite(characteristic, message, callback)) {
writeError = true;
callback.invoke("Write failed");
break;
Expand Down