in amazonfreertossdk/src/main/java/software/amazon/freertos/amazonfreertossdk/AmazonFreeRTOSDevice.java [1116:1190]
private void processBleCommandQueue() {
try {
mutex.acquire();
if (mBleOperationInProgress) {
Log.d(TAG, "Ble operation is in progress. mqtt queue: " + mMqttQueue.size()
+ " network queue: " + mNetworkQueue.size());
} else {
if (mMqttQueue.peek() == null && mNetworkQueue.peek() == null) {
Log.d(TAG, "There's no ble command in the queue.");
mBleOperationInProgress = false;
} else {
mBleOperationInProgress = true;
BleCommand bleCommand;
if (mNetworkQueue.peek() != null && mMqttQueue.peek() != null) {
if (rr) {
bleCommand = mMqttQueue.poll();
} else {
bleCommand = mNetworkQueue.poll();
}
rr = !rr;
} else if (mNetworkQueue.peek() != null) {
bleCommand = mNetworkQueue.poll();
} else {
bleCommand = mMqttQueue.poll();
}
Log.d(TAG, "Processing BLE command: " + bleCommand.getType()
+ " remaining mqtt queue " + mMqttQueue.size()
+ ", network queue " + mNetworkQueue.size());
boolean commandSent = false;
switch (bleCommand.getType()) {
case WRITE_DESCRIPTOR:
if (writeDescriptor(bleCommand.getServiceUuid(), bleCommand.getCharacteristicUuid())) {
commandSent = true;
}
break;
case WRITE_CHARACTERISTIC:
if (writeCharacteristic(bleCommand.getServiceUuid(), bleCommand.getCharacteristicUuid(),
bleCommand.getData())) {
commandSent = true;
}
break;
case READ_CHARACTERISTIC:
if (readCharacteristic(bleCommand.getServiceUuid(), bleCommand.getCharacteristicUuid())) {
commandSent = true;
}
break;
case DISCOVER_SERVICES:
if (mBluetoothGatt.discoverServices()) {
commandSent = true;
} else {
Log.e(TAG, "Failed to discover services!");
}
break;
case REQUEST_MTU:
if (mBluetoothGatt.requestMtu(ByteBuffer.wrap(bleCommand.getData()).getInt())) {
commandSent = true;
} else {
Log.e(TAG, "Failed to set MTU.");
}
break;
default:
Log.w(TAG, "Unknown Ble command, cannot process.");
}
if (commandSent) {
mHandler.postDelayed(resetOperationInProgress, BLE_COMMAND_TIMEOUT);
} else {
mHandler.post(resetOperationInProgress);
}
}
}
mutex.release();
} catch (InterruptedException e) {
Log.e(TAG, "Mutex error", e);
}
}