summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@qt.io>2018-04-13 17:17:19 +0200
committerAlex Blasche <alexander.blasche@qt.io>2018-04-17 12:21:31 +0000
commite68b5e17b071ca11974474546be04c7d564eae3a (patch)
tree3f330a19d0a5d53e8c0658ad138430788664382b /src
parent184307884f560653421286cc9e5e6f0e86a4cef4 (diff)
Android: Fix start/stop behavior of QLowEnergyController (peripheral)
This patch addresses two bugs. Firstly it ensures that the correct Java function is executed when disconectFromDevice() is called on Android peripheral. Secondly, it turned out that calling disconnectFromDevice() while a connection to a central exists and restarting the advertisement resulted in a non-functional QLEController instance because BluetoothGattServer was not properly restarted. As a side effects the controller's state tracking stopped as well. Change-Id: I98851fc974ceff2a1fcb03fe754dbda9c4aba271 Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java29
-rw-r--r--src/bluetooth/qlowenergycontroller_android.cpp8
2 files changed, 29 insertions, 8 deletions
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 034190fd..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
@@ -213,7 +213,6 @@ public class QtBluetoothLEServer {
return;
}
- mGattServer = manager.openGattServer(qtContext, mGattServerListener);
mLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
if (!mBluetoothAdapter.isMultipleAdvertisementSupported())
@@ -229,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;
@@ -237,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);
@@ -401,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()
@@ -413,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,
@@ -422,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);
@@ -441,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);
}
diff --git a/src/bluetooth/qlowenergycontroller_android.cpp b/src/bluetooth/qlowenergycontroller_android.cpp
index c5c9ba56..54665f7b 100644
--- a/src/bluetooth/qlowenergycontroller_android.cpp
+++ b/src/bluetooth/qlowenergycontroller_android.cpp
@@ -180,8 +180,12 @@ void QLowEnergyControllerPrivateAndroid::disconnectFromDevice()
QLowEnergyController::ControllerState oldState = state;
setState(QLowEnergyController::ClosingState);
- if (hub)
- hub->javaObject().callMethod<void>("disconnect");
+ if (hub) {
+ if (role == QLowEnergyController::PeripheralRole)
+ hub->javaObject().callMethod<void>("disconnectServer");
+ else
+ hub->javaObject().callMethod<void>("disconnect");
+ }
if (oldState == QLowEnergyController::ConnectingState)
setState(QLowEnergyController::UnconnectedState);