Commit f3f6bb0f authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- add PeripheralMessageQueue

parent d323e8c7
......@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -118,6 +118,7 @@ public class BLEHandler {
private BluetoothDevice m_centralDevice;
private BluetoothGattCharacteristic m_writeCharacteristic;
private BluetoothGattCharacteristic m_readCharacteristic;
public List<byte[]> PeripheralMessageQueue = new LinkedList<>();
//endregion
//region COMMON FUNCTIONS
......@@ -241,6 +242,7 @@ public class BLEHandler {
m_bluetoothGattServer.close();
m_bluetoothGattServer = null;
}
PeripheralMessageQueue.clear();
}
m_isInit = false;
......@@ -775,6 +777,15 @@ public class BLEHandler {
public void onNotificationSent(BluetoothDevice device, int status) {
super.onNotificationSent(device, status);
Log.d(TAG, "onNotificationSent to " + device.getName() + " status : " + status);
if (status == BluetoothGatt.GATT_SUCCESS && PeripheralMessageQueue.size() != 0) {
PeripheralMessageQueue.remove(0);
}
if (PeripheralMessageQueue.size() != 0) {
byte[] msg = PeripheralMessageQueue.get(0);
m_writeCharacteristic.setValue(msg);
m_bluetoothGattServer.notifyCharacteristicChanged(m_centralDevice, m_writeCharacteristic, false);
}
}
@Override
......@@ -841,12 +852,18 @@ public class BLEHandler {
}
public boolean sendDataToCentral(byte[] msg) {
boolean rt;
m_writeCharacteristic.setValue(msg);
rt = !isCentral() &&
m_centralDevice != null &&
m_bluetoothGattServer != null &&
m_bluetoothGattServer.notifyCharacteristicChanged(m_centralDevice, m_writeCharacteristic, false);
boolean rt = false;
if (m_writeCharacteristic != null) {
PeripheralMessageQueue.add(msg);
if (PeripheralMessageQueue.size() == 1) {
m_writeCharacteristic.setValue(msg);
rt = !isCentral() &&
m_centralDevice != null &&
m_bluetoothGattServer != null &&
m_bluetoothGattServer.notifyCharacteristicChanged(m_centralDevice, m_writeCharacteristic, false);
}
}
Log.d(TAG, "sendDataToCentral: byteMsg size : " + msg.length + ", result : " + rt);
return rt;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment