diff options
Diffstat (limited to 'src/android/bluetooth/src')
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLE.java | 39 | ||||
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java | 38 |
2 files changed, 59 insertions, 18 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 1b527ae3..4063537b 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 @@ -1141,16 +1141,24 @@ public class QtBluetoothLE { if (handle == HANDLE_FOR_MTU_EXCHANGE) return; - GattEntry entry = entries.get(handle); - if (entry == null) - return; - if (entry.valueKnown) - return; - entry.valueKnown = true; + try { + synchronized (this) { - GattEntry serviceEntry = entries.get(entry.associatedServiceHandle); - if (serviceEntry != null && serviceEntry.endHandle == handle) - finishCurrentServiceDiscovery(entry.associatedServiceHandle); + GattEntry entry = entries.get(handle); + if (entry == null) + return; + if (entry.valueKnown) + return; + entry.valueKnown = true; + + GattEntry serviceEntry = entries.get(entry.associatedServiceHandle); + if (serviceEntry != null && serviceEntry.endHandle == handle) + finishCurrentServiceDiscovery(entry.associatedServiceHandle); + } + } catch (IndexOutOfBoundsException outOfBounds) { + Log.w(TAG, "interruptCurrentIO(): Unknown gatt entry, index: " + + handle + " size: " + entries.size()); + } } /* @@ -1271,9 +1279,16 @@ public class QtBluetoothLE { } // last entry of current discovery run? - GattEntry serviceEntry = entries.get(entry.associatedServiceHandle); - if (serviceEntry.endHandle == handle) - finishCurrentServiceDiscovery(entry.associatedServiceHandle); + synchronized (this) { + try { + GattEntry serviceEntry = entries.get(entry.associatedServiceHandle); + if (serviceEntry.endHandle == handle) + finishCurrentServiceDiscovery(entry.associatedServiceHandle); + } catch (IndexOutOfBoundsException outOfBounds) { + Log.w(TAG, "performNextIO(): Unknown service for entry, index: " + + entry.associatedServiceHandle + " size: " + entries.size()); + } + } } else { int errorCode = 0; diff --git a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java index 00217904..cdd16686 100644 --- a/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java +++ b/src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java @@ -82,6 +82,12 @@ public class QtBluetoothLEServer { private BluetoothGattServer mGattServer = null; private BluetoothLeAdvertiser mLeAdvertiser = null; + private String mRemoteName = ""; + public String remoteName() { return mRemoteName; } + + private String mRemoteAddress = ""; + public String remoteAddress() { return mRemoteAddress; } + /* As per Bluetooth specification each connected device can have individual and persistent Client characteristic configurations (see Bluetooth Spec 5.0 Vol 3 Part G 3.3.3.3) @@ -207,7 +213,6 @@ public class QtBluetoothLEServer { return; } - mGattServer = manager.openGattServer(qtContext, mGattServerListener); mLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); if (!mBluetoothAdapter.isMultipleAdvertisementSupported()) @@ -223,7 +228,7 @@ public class QtBluetoothLEServer { { @Override public void onConnectionStateChange(BluetoothDevice device, int status, int newState) { - Log.w(TAG, "Our gatt server connection state changed, new state: " + newState); + Log.w(TAG, "Our gatt server connection state changed, new state: " + newState + " " + status); super.onConnectionStateChange(device, status, newState); int qtControllerState = 0; @@ -231,6 +236,7 @@ public class QtBluetoothLEServer { case BluetoothProfile.STATE_DISCONNECTED: qtControllerState = 0; // QLowEnergyController::UnconnectedState clientCharacteristicManager.markDeviceConnectivity(device, false); + mGattServer.close(); break; case BluetoothProfile.STATE_CONNECTED: clientCharacteristicManager.markDeviceConnectivity(device, true); @@ -238,6 +244,9 @@ public class QtBluetoothLEServer { break; } + mRemoteName = device.getName(); + mRemoteAddress = device.getAddress(); + int qtErrorCode; switch (status) { case BluetoothGatt.GATT_SUCCESS: @@ -392,10 +401,18 @@ public class QtBluetoothLEServer { public boolean connectServer() { - if (mGattServer == null) + if (mGattServer != null) + return true; + + BluetoothManager manager = (BluetoothManager) qtContext.getSystemService(Context.BLUETOOTH_SERVICE); + if (manager == null) { + Log.w(TAG, "Bluetooth service not available."); return false; + } - return true; + mGattServer = manager.openGattServer(qtContext, mGattServerListener); + + return (mGattServer != null); } public void disconnectServer() @@ -404,6 +421,10 @@ public class QtBluetoothLEServer { return; mGattServer.close(); + mGattServer = null; + + mRemoteName = mRemoteAddress = ""; + leServerConnectionStateChange(qtObject, 0 /*NoError*/, 0 /*QLowEnergyController::UnconnectedState*/); } public boolean startAdvertising(AdvertiseData advertiseData, @@ -413,7 +434,10 @@ public class QtBluetoothLEServer { if (mLeAdvertiser == null) return false; - connectServer(); + if (!connectServer()) { + Log.w(TAG, "Server::startAdvertising: Cannot open GATT server"); + return false; + } Log.w(TAG, "Starting to advertise."); mLeAdvertiser.startAdvertising(settings, advertiseData, scanResponse, mAdvertiseListener); @@ -432,8 +456,10 @@ public class QtBluetoothLEServer { public void addService(BluetoothGattService service) { - if (mGattServer == null) + if (!connectServer()) { + Log.w(TAG, "Server::addService: Cannot open GATT server"); return; + } mGattServer.addService(service); } |