diff options
Diffstat (limited to 'src/android')
-rw-r--r-- | src/android/bluetooth/src/org/qtproject/qt5/android/bluetooth/QtBluetoothLEServer.java | 29 |
1 files changed, 23 insertions, 6 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); } |