diff --git a/Common/Source/xcs/Android/NativeInputListener.cpp b/Common/Source/xcs/Android/NativeInputListener.cpp index e9a86dbe6..d3407b355 100644 --- a/Common/Source/xcs/Android/NativeInputListener.cpp +++ b/Common/Source/xcs/Android/NativeInputListener.cpp @@ -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) { @@ -70,7 +69,7 @@ Java_org_LK8000_NativeInputListener_onCharacteristicChanged(JNIEnv* env, jobject auto& handler = *reinterpret_cast(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); } } diff --git a/android/src/org/LK8000/BluetoothGattClientPort.java b/android/src/org/LK8000/BluetoothGattClientPort.java index 4ff1ef2fb..230ef3cd8 100644 --- a/android/src/org/LK8000/BluetoothGattClientPort.java +++ b/android/src/org/LK8000/BluetoothGattClientPort.java @@ -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) { @@ -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; } } @@ -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; @@ -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) { @@ -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); @@ -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); } } @@ -369,8 +371,6 @@ public void close() { if (gatt != null) { gatt.disconnect(); waitForClose(); - gatt.close(); - gatt = null; } } diff --git a/android/src/org/LK8000/BluetoothHelper.java b/android/src/org/LK8000/BluetoothHelper.java index 9bc9004bf..bd9110771 100644 --- a/android/src/org/LK8000/BluetoothHelper.java +++ b/android/src/org/LK8000/BluetoothHelper.java @@ -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; } diff --git a/android/src/org/LK8000/InputListener.java b/android/src/org/LK8000/InputListener.java index 12c23fe4a..40bd43a44 100644 --- a/android/src/org/LK8000/InputListener.java +++ b/android/src/org/LK8000/InputListener.java @@ -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); } diff --git a/android/src/org/LK8000/LeScanCallback.java b/android/src/org/LK8000/LeScanCallback.java index 3ffdc847a..47bb1debe 100644 --- a/android/src/org/LK8000/LeScanCallback.java +++ b/android/src/org/LK8000/LeScanCallback.java @@ -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); } } diff --git a/android/src/org/LK8000/MultiPort.java b/android/src/org/LK8000/MultiPort.java index d783c4169..5af8aeeb3 100644 --- a/android/src/org/LK8000/MultiPort.java +++ b/android/src/org/LK8000/MultiPort.java @@ -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() { diff --git a/android/src/org/LK8000/NativeInputListener.java b/android/src/org/LK8000/NativeInputListener.java index 22f05476f..224974fd4 100644 --- a/android/src/org/LK8000/NativeInputListener.java +++ b/android/src/org/LK8000/NativeInputListener.java @@ -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); }