diff options
author | Liang Qi <liang.qi@qt.io> | 2017-01-24 13:54:45 +0100 |
---|---|---|
committer | Liang Qi <liang.qi@qt.io> | 2017-01-24 13:54:45 +0100 |
commit | a8604157818e21d7b70e2853258fc4569db770be (patch) | |
tree | 2b5297bf4140bad1c7b357047f6515fa0f7c4e22 /src/android/bluetooth | |
parent | 498020954fd00e3de545dbc414ba09196e0f59b7 (diff) | |
parent | 468e3c38a893b9293a9f0ede432ce54d72b46966 (diff) |
Merge remote-tracking branch 'origin/5.8' into dev
Change-Id: I9684e4cc76336250b235a261fdc8924a2ca70086
Diffstat (limited to 'src/android/bluetooth')
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java index 53c7305f..cb7b2dc9 100644 --- a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java +++ b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java @@ -51,6 +51,7 @@ import android.content.Context; import android.os.Handler; import android.os.Looper; import android.util.Log; +import java.lang.reflect.Method; import java.util.concurrent.atomic.AtomicInteger; import java.lang.reflect.Method; @@ -187,9 +188,11 @@ public class QtBluetoothLE { case BluetoothGatt.GATT_SUCCESS: errorCode = 0; break; //QLowEnergyController::NoError case 8: // link loss + case 22: // BTA_GATT_CONN_LMP_TIMEOUT + Log.w(TAG, "Connection Error: Try to delay connect() call after previous activity"); errorCode = 5; break; //QLowEnergyController::ConnectionError default: - Log.w(TAG, "Unhandled error code on connectionStateChanged: " + status); + Log.w(TAG, "Unhandled error code on connectionStateChanged: " + status + " " + newState); errorCode = status; break; //TODO deal with all errors } leConnectionStateChange(qtObject, errorCode, qLowEnergyController_State); @@ -479,7 +482,31 @@ public class QtBluetoothLE { return false; } - mBluetoothGatt = mRemoteGattDevice.connectGatt(qtContext, false, gattCallback); + try { + // BluetoothDevice.,connectGatt(Context, boolean, BluetoothGattCallback, int) was + // officially introduced by Android API v23. Earlier Android versions have a private + // implementation already though. Let's check at runtime and use it if possible. + // + // In general the new connectGatt() seems to be much more reliable than the function + // that doesn't specify the transport layer. + + Class[] args = new Class[4]; + args[0] = android.content.Context.class; + args[1] = boolean.class; + args[2] = android.bluetooth.BluetoothGattCallback.class; + args[3] = int.class; + Method connectMethod = mRemoteGattDevice.getClass().getDeclaredMethod("connectGatt", args); + if (connectMethod != null) { + mBluetoothGatt = (BluetoothGatt) connectMethod.invoke(mRemoteGattDevice, qtContext, + false, gattCallback, + 2 /*TRANSPORT_LE*/); + Log.w(TAG, "Using Android v23 BluetoothDevice.connectGatt()"); + } + } catch (Exception ex) { + // fallback to less reliable API 18 version + mBluetoothGatt = mRemoteGattDevice.connectGatt(qtContext, false, gattCallback); + } + return mBluetoothGatt != null; } @@ -819,6 +846,12 @@ public class QtBluetoothLE { GattEntry serviceEntry = entries.get(serviceHandle); final int endHandle = serviceEntry.endHandle; + if (serviceHandle == endHandle) { + Log.w(TAG, "scheduleServiceDetailDiscovery: service is empty; nothing to discover"); + finishCurrentServiceDiscovery(serviceHandle); + return; + } + synchronized (readWriteQueue) { // entire block inside mutex to ensure all service discovery jobs go in one after the other // ensures that serviceDiscovered() signal is sent when required |