Skip to content

Commit

Permalink
BluetoothGattClientPort : reduce JNI call cpu overhead to improve BLE…
Browse files Browse the repository at this point in the history
… connection reliability
  • Loading branch information
brunotl committed Aug 30, 2024
1 parent 20def96 commit 629d88d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
7 changes: 3 additions & 4 deletions Common/Source/xcs/Android/NativeInputListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ Java_org_LK8000_NativeInputListener_dataReceived(JNIEnv *env, jobject obj,
extern "C"
JNIEXPORT void JNICALL
Java_org_LK8000_NativeInputListener_onCharacteristicChanged(JNIEnv* env, jobject obj,
jobject service, jobject characteristic,
jlong serviceMsb, jlong serviceLsb,
jlong characteristicMsb, jlong characteristicLsb,
jbyteArray data, jint length) {

using namespace Java::UUID;

if (length > 0) {
jlong ptr = env->GetLongField(obj, ptr_field);
if (ptr == 0) {
Expand All @@ -70,7 +69,7 @@ Java_org_LK8000_NativeInputListener_onCharacteristicChanged(JNIEnv* env, jobject
auto& handler = *reinterpret_cast<DataHandler*>(ptr);

Java::ByteArrayElements array(env, data);
handler.OnCharacteristicChanged(to_uuid_t(env, service), to_uuid_t(env, characteristic), array, length);
handler.OnCharacteristicChanged( uuid_t(serviceMsb, serviceLsb), uuid_t(characteristicMsb, characteristicLsb), array, length);
}
}

Expand Down
42 changes: 21 additions & 21 deletions android/src/org/LK8000/BluetoothGattClientPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ void stopLeScan() {
}

public BluetoothGattClientPort(Context context, BluetoothDevice device) {
startLeScan(context, device.getAddress());
if (BluetoothHelper.isUnknownType(device)) {
// unknown device : scan for device...
startLeScan(context, device.getAddress());
}
else {
connectDevice(context, device);
}
}

private void connectDevice(Context context, BluetoothDevice device) {
Expand Down Expand Up @@ -165,12 +171,15 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
Log.e(TAG, "Discovering GATT services request failed");
newPortState = STATE_FAILED;
}
} else {
}
else if (BluetoothProfile.STATE_DISCONNECTED == newState) {
hm10DataCharacteristic = null;
queueCommand.clear();
if (!shutdown && !gatt.connect()) {
Log.w(TAG,
"Received GATT disconnected event, and reconnect attempt failed");
if (shutdown) {
gatt.close();
this.gatt = null;
}
else if (!gatt.connect()) {
Log.w(TAG, "Received GATT disconnected event, and reconnect attempt failed");
newPortState = STATE_FAILED;
}
}
Expand All @@ -184,19 +193,11 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
}

@SuppressLint("MissingPermission")
void requestMtu(BluetoothGatt gatt) {
maxChunkSize = 20; // default mtu - 3
if (!gatt.requestMtu(517)) { // TODO: check if OnMtuChanged is always called ... ?
queueCommand.completed();
}
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (BluetoothGatt.GATT_SUCCESS == status) {
maxChunkSize = 20; // default mtu - 3
queueCommand.add(() -> requestMtu(gatt));
queueCommand.add(() -> configureCharacteristics(gatt));
gatt.requestMtu(517);
} else {
Log.e(TAG, "Discovering GATT services failed");
portState = STATE_FAILED;
Expand All @@ -209,8 +210,8 @@ public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
super.onMtuChanged(gatt, mtu, status);
if (BluetoothGatt.GATT_SUCCESS == status) {
maxChunkSize = mtu - 3;
configureCharacteristics(gatt);
}
queueCommand.completed();
}

private boolean doEnableNotification(UUID service, UUID characteristic) {
Expand All @@ -221,8 +222,6 @@ private boolean doEnableNotification(UUID service, UUID characteristic) {
}

private void configureCharacteristics(BluetoothGatt gatt) {
queueCommand.completed();

BluetoothGattService hm10Service = gatt.getService(HM10_SERVICE);
if (hm10Service != null) {
hm10DataCharacteristic = hm10Service.getCharacteristic(RX_TX_CHARACTERISTIC_UUID);
Expand Down Expand Up @@ -346,7 +345,10 @@ public void onCharacteristicChanged(@NonNull BluetoothGatt gatt, @NonNull Blueto
if (listener != null) {
final UUID service = characteristic.getService().getUuid();
final UUID uuid = characteristic.getUuid();
listener.onCharacteristicChanged(service, uuid, value, value.length);
listener.onCharacteristicChanged(
service.getMostSignificantBits(), service.getLeastSignificantBits(),
uuid.getMostSignificantBits(), uuid.getLeastSignificantBits(),
value, value.length);
}
}

Expand All @@ -369,8 +371,6 @@ public void close() {
if (gatt != null) {
gatt.disconnect();
waitForClose();
gatt.close();
gatt = null;
}
}

Expand Down
1 change: 1 addition & 0 deletions android/src/org/LK8000/BluetoothHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public static boolean startLeScan(String address, ScanCallback cb) {
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT);
}

// TODO: check for no more 5 call in 30sec time frame...
scanner.startScan(buildFilter(address), settings.build(), cb);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion android/src/org/LK8000/InputListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ public interface InputListener {

boolean doEnableNotification(UUID service, UUID characteristic);

void onCharacteristicChanged(UUID service, UUID characteristic, byte[] value, int length);
void onCharacteristicChanged(long serviceMsb, long serviceLsb, long characteristicMsb, long characteristicLsb, byte[] value, int length);
}
4 changes: 4 additions & 0 deletions android/src/org/LK8000/LeScanCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public void onScanResult(int callbackType, ScanResult result) {

@Override
public void onScanFailed(int errorCode) {

if (errorCode == ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED) {
// TODO : ask to user for device reboot
}
Log.e(TAG, "onScanFailed " + errorCode);
}
}
4 changes: 2 additions & 2 deletions android/src/org/LK8000/MultiPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ public boolean doEnableNotification(UUID service, UUID characteristic) {
}

@Override
public void onCharacteristicChanged(UUID service, UUID characteristic, byte[] value, int length) {
public void onCharacteristicChanged(long serviceMsb, long serviceLsb, long characteristicMsb, long characteristicLsb, byte[] value, int length) {
InputListener l = inputListener;
if (l != null)
l.onCharacteristicChanged(service, characteristic, value, length);
l.onCharacteristicChanged(serviceMsb, serviceLsb, characteristicMsb, characteristicLsb, value, length);
}

protected void stateChanged() {
Expand Down
2 changes: 1 addition & 1 deletion android/src/org/LK8000/NativeInputListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ class NativeInputListener implements InputListener {
public native boolean doEnableNotification(UUID service, UUID characteristic);

@Override
public native void onCharacteristicChanged(UUID service, UUID characteristic, byte[] value, int length);
public native void onCharacteristicChanged(long serviceMsb, long serviceLsb, long characteristicMsb, long characteristicLsb, byte[] value, int length);
}

0 comments on commit 629d88d

Please sign in to comment.