Skip to content

Commit

Permalink
Modify order of checks in nextCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvanwelie committed Apr 16, 2020
1 parent 42852a8 commit 3ba0751
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,9 @@ private void connectionStateChangeUnsuccessful(int status, int newState, long ti
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action == null) return;
if (action == null) return;
final BluetoothDevice receivedDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(receivedDevice == null) return;
if (receivedDevice == null) return;

// Ignore updates for other devices
if (!receivedDevice.getAddress().equalsIgnoreCase(getAddress())) return;
Expand Down Expand Up @@ -710,7 +710,7 @@ public void run() {
@Override
public void onReceive(final Context context, final Intent intent) {
final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if(device == null) return;
if (device == null) return;

// Skip other devices
if (bluetoothGatt == null || !device.getAddress().equals(bluetoothGatt.getDevice().getAddress()))
Expand Down Expand Up @@ -1563,12 +1563,13 @@ private void retryCommand() {
* If the read or write fails, the next command in the queue is executed.
*/
private void nextCommand() {
// Make sure only one thread can execute this method
synchronized (this) {
// If there is still a command being executed then bail out
if (commandQueueBusy) {
return;
}
// If there is still a command being executed, then bail out
if (commandQueueBusy) return;

// Check if there is something to do at all
final Runnable bluetoothCommand = commandQueue.peek();
if (bluetoothCommand == null) return;

// Check if we still have a valid gatt object
if (bluetoothGatt == null) {
Expand All @@ -1579,25 +1580,21 @@ private void nextCommand() {
}

// Execute the next command in the queue
final Runnable bluetoothCommand = commandQueue.peek();
if (bluetoothCommand != null) {
commandQueueBusy = true;
if (!isRetrying) {
nrTries = 0;
}

mainHandler.post(new Runnable() {
@Override
public void run() {
try {
bluetoothCommand.run();
} catch (Exception ex) {
Timber.e(ex, "command exception for device '%s'", getName());
completedCommand();
}
}
});
commandQueueBusy = true;
if (!isRetrying) {
nrTries = 0;
}
mainHandler.post(new Runnable() {
@Override
public void run() {
try {
bluetoothCommand.run();
} catch (Exception ex) {
Timber.e(ex, "command exception for device '%s'", getName());
completedCommand();
}
}
});
}
}

Expand Down Expand Up @@ -1910,7 +1907,7 @@ private int getTimoutThreshold() {
}

private byte[] copyOf(byte[] source) {
if( source == null) return new byte[0];
if (source == null) return new byte[0];
final int sourceLength = source.length;
final byte[] copy = new byte[sourceLength];
System.arraycopy(source, 0, copy, 0, sourceLength);
Expand Down

0 comments on commit 3ba0751

Please sign in to comment.