summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/qlowenergycontroller_android.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bluetooth/qlowenergycontroller_android.cpp')
-rw-r--r--src/bluetooth/qlowenergycontroller_android.cpp103
1 files changed, 56 insertions, 47 deletions
diff --git a/src/bluetooth/qlowenergycontroller_android.cpp b/src/bluetooth/qlowenergycontroller_android.cpp
index a1decd96..54665f7b 100644
--- a/src/bluetooth/qlowenergycontroller_android.cpp
+++ b/src/bluetooth/qlowenergycontroller_android.cpp
@@ -37,9 +37,10 @@
**
****************************************************************************/
-#include "qlowenergycontroller_p.h"
+#include "qlowenergycontroller_android_p.h"
#include <QtCore/QLoggingCategory>
#include <QtAndroidExtras/QAndroidJniEnvironment>
+#include <QtAndroidExtras/QAndroidJniObject>
#include <QtBluetooth/QLowEnergyServiceData>
#include <QtBluetooth/QLowEnergyCharacteristicData>
#include <QtBluetooth/QLowEnergyDescriptorData>
@@ -69,16 +70,14 @@ static QAndroidJniObject javaUuidfromQtUuid(const QBluetoothUuid& uuid)
return javaUuid;
}
-QLowEnergyControllerPrivate::QLowEnergyControllerPrivate()
- : QObject(),
- state(QLowEnergyController::UnconnectedState),
- error(QLowEnergyController::NoError),
+QLowEnergyControllerPrivateAndroid::QLowEnergyControllerPrivateAndroid()
+ : QLowEnergyControllerPrivate(),
hub(0)
{
registerQLowEnergyControllerMetaType();
}
-QLowEnergyControllerPrivate::~QLowEnergyControllerPrivate()
+QLowEnergyControllerPrivateAndroid::~QLowEnergyControllerPrivateAndroid()
{
if (role == QLowEnergyController::PeripheralRole) {
if (hub)
@@ -86,7 +85,7 @@ QLowEnergyControllerPrivate::~QLowEnergyControllerPrivate()
}
}
-void QLowEnergyControllerPrivate::init()
+void QLowEnergyControllerPrivateAndroid::init()
{
// Android Central/Client support starts with v18
// Peripheral/Server support requires Android API v21
@@ -106,13 +105,13 @@ void QLowEnergyControllerPrivate::init()
// we only connect to the peripheral role specific signals
// TODO add connections as they get added later on
connect(hub, &LowEnergyNotificationHub::connectionUpdated,
- this, &QLowEnergyControllerPrivate::connectionUpdated);
+ this, &QLowEnergyControllerPrivateAndroid::connectionUpdated);
connect(hub, &LowEnergyNotificationHub::advertisementError,
- this, &QLowEnergyControllerPrivate::advertisementError);
+ this, &QLowEnergyControllerPrivateAndroid::advertisementError);
connect(hub, &LowEnergyNotificationHub::serverCharacteristicChanged,
- this, &QLowEnergyControllerPrivate::serverCharacteristicChanged);
+ this, &QLowEnergyControllerPrivateAndroid::serverCharacteristicChanged);
connect(hub, &LowEnergyNotificationHub::serverDescriptorWritten,
- this, &QLowEnergyControllerPrivate::serverDescriptorWritten);
+ this, &QLowEnergyControllerPrivateAndroid::serverDescriptorWritten);
} else {
if (version < 18) {
qWarning() << "Qt Bluetooth LE Central/Client support not available"
@@ -123,25 +122,25 @@ void QLowEnergyControllerPrivate::init()
hub = new LowEnergyNotificationHub(remoteDevice, isPeripheral, this);
// we only connect to the central role specific signals
connect(hub, &LowEnergyNotificationHub::connectionUpdated,
- this, &QLowEnergyControllerPrivate::connectionUpdated);
+ this, &QLowEnergyControllerPrivateAndroid::connectionUpdated);
connect(hub, &LowEnergyNotificationHub::servicesDiscovered,
- this, &QLowEnergyControllerPrivate::servicesDiscovered);
+ this, &QLowEnergyControllerPrivateAndroid::servicesDiscovered);
connect(hub, &LowEnergyNotificationHub::serviceDetailsDiscoveryFinished,
- this, &QLowEnergyControllerPrivate::serviceDetailsDiscoveryFinished);
+ this, &QLowEnergyControllerPrivateAndroid::serviceDetailsDiscoveryFinished);
connect(hub, &LowEnergyNotificationHub::characteristicRead,
- this, &QLowEnergyControllerPrivate::characteristicRead);
+ this, &QLowEnergyControllerPrivateAndroid::characteristicRead);
connect(hub, &LowEnergyNotificationHub::descriptorRead,
- this, &QLowEnergyControllerPrivate::descriptorRead);
+ this, &QLowEnergyControllerPrivateAndroid::descriptorRead);
connect(hub, &LowEnergyNotificationHub::characteristicWritten,
- this, &QLowEnergyControllerPrivate::characteristicWritten);
+ this, &QLowEnergyControllerPrivateAndroid::characteristicWritten);
connect(hub, &LowEnergyNotificationHub::descriptorWritten,
- this, &QLowEnergyControllerPrivate::descriptorWritten);
+ this, &QLowEnergyControllerPrivateAndroid::descriptorWritten);
connect(hub, &LowEnergyNotificationHub::characteristicChanged,
- this, &QLowEnergyControllerPrivate::characteristicChanged);
+ this, &QLowEnergyControllerPrivateAndroid::characteristicChanged);
}
}
-void QLowEnergyControllerPrivate::connectToDevice()
+void QLowEnergyControllerPrivateAndroid::connectToDevice()
{
if (!hub)
return; // Android version below v18
@@ -170,7 +169,7 @@ void QLowEnergyControllerPrivate::connectToDevice()
}
}
-void QLowEnergyControllerPrivate::disconnectFromDevice()
+void QLowEnergyControllerPrivateAndroid::disconnectFromDevice()
{
/* Catch an Android timeout bug. If the device is connecting but cannot
* physically connect it seems to ignore the disconnect call below.
@@ -181,14 +180,18 @@ void QLowEnergyControllerPrivate::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);
}
-void QLowEnergyControllerPrivate::discoverServices()
+void QLowEnergyControllerPrivateAndroid::discoverServices()
{
if (hub && hub->javaObject().callMethod<jboolean>("discoverServices")) {
qCDebug(QT_BT_ANDROID) << "Service discovery initiated";
@@ -199,7 +202,7 @@ void QLowEnergyControllerPrivate::discoverServices()
}
}
-void QLowEnergyControllerPrivate::discoverServiceDetails(const QBluetoothUuid &service)
+void QLowEnergyControllerPrivateAndroid::discoverServiceDetails(const QBluetoothUuid &service)
{
if (!serviceList.contains(service)) {
qCWarning(QT_BT_ANDROID) << "Discovery of unknown service" << service.toString()
@@ -234,7 +237,7 @@ void QLowEnergyControllerPrivate::discoverServiceDetails(const QBluetoothUuid &s
qCDebug(QT_BT_ANDROID) << "Discovery of" << service << "started";
}
-void QLowEnergyControllerPrivate::writeCharacteristic(
+void QLowEnergyControllerPrivateAndroid::writeCharacteristic(
const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle,
const QByteArray &newValue,
@@ -288,7 +291,7 @@ void QLowEnergyControllerPrivate::writeCharacteristic(
service->setError(QLowEnergyService::CharacteristicWriteError);
}
-void QLowEnergyControllerPrivate::writeDescriptor(
+void QLowEnergyControllerPrivateAndroid::writeDescriptor(
const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle,
const QLowEnergyHandle descHandle,
@@ -339,7 +342,7 @@ void QLowEnergyControllerPrivate::writeDescriptor(
service->setError(QLowEnergyService::DescriptorWriteError);
}
-void QLowEnergyControllerPrivate::readCharacteristic(
+void QLowEnergyControllerPrivateAndroid::readCharacteristic(
const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle charHandle)
{
@@ -367,7 +370,7 @@ void QLowEnergyControllerPrivate::readCharacteristic(
service->setError(QLowEnergyService::CharacteristicReadError);
}
-void QLowEnergyControllerPrivate::readDescriptor(
+void QLowEnergyControllerPrivateAndroid::readDescriptor(
const QSharedPointer<QLowEnergyServicePrivate> service,
const QLowEnergyHandle /*charHandle*/,
const QLowEnergyHandle descriptorHandle)
@@ -393,7 +396,7 @@ void QLowEnergyControllerPrivate::readDescriptor(
service->setError(QLowEnergyService::DescriptorReadError);
}
-void QLowEnergyControllerPrivate::connectionUpdated(
+void QLowEnergyControllerPrivateAndroid::connectionUpdated(
QLowEnergyController::ControllerState newState,
QLowEnergyController::Error errorCode)
{
@@ -409,7 +412,7 @@ void QLowEnergyControllerPrivate::connectionUpdated(
}
// called if server/peripheral
-void QLowEnergyControllerPrivate::peripheralConnectionUpdated(
+void QLowEnergyControllerPrivateAndroid::peripheralConnectionUpdated(
QLowEnergyController::ControllerState newState,
QLowEnergyController::Error errorCode)
{
@@ -431,15 +434,21 @@ void QLowEnergyControllerPrivate::peripheralConnectionUpdated(
Q_Q(QLowEnergyController);
if (oldState == QLowEnergyController::ConnectedState
&& newState != QLowEnergyController::ConnectedState) {
+ remoteDevice.clear();
+ remoteName.clear();
emit q->disconnected();
} else if (newState == QLowEnergyController::ConnectedState
&& oldState != QLowEnergyController::ConnectedState) {
+ if (hub) {
+ remoteDevice = QBluetoothAddress(hub->javaObject().callObjectMethod<jstring>("remoteAddress").toString());
+ remoteName = hub->javaObject().callObjectMethod<jstring>("remoteName").toString();
+ }
emit q->connected();
}
}
// called if client/central
-void QLowEnergyControllerPrivate::centralConnectionUpdated(
+void QLowEnergyControllerPrivateAndroid::centralConnectionUpdated(
QLowEnergyController::ControllerState newState,
QLowEnergyController::Error errorCode)
{
@@ -484,7 +493,7 @@ void QLowEnergyControllerPrivate::centralConnectionUpdated(
}
}
-void QLowEnergyControllerPrivate::servicesDiscovered(
+void QLowEnergyControllerPrivateAndroid::servicesDiscovered(
QLowEnergyController::Error errorCode, const QString &foundServices)
{
Q_Q(QLowEnergyController);
@@ -515,7 +524,7 @@ void QLowEnergyControllerPrivate::servicesDiscovered(
}
}
-void QLowEnergyControllerPrivate::serviceDetailsDiscoveryFinished(
+void QLowEnergyControllerPrivateAndroid::serviceDetailsDiscoveryFinished(
const QString &serviceUuid, int startHandle, int endHandle)
{
const QBluetoothUuid service(serviceUuid);
@@ -563,7 +572,7 @@ void QLowEnergyControllerPrivate::serviceDetailsDiscoveryFinished(
pointer->setState(QLowEnergyService::ServiceDiscovered);
}
-void QLowEnergyControllerPrivate::characteristicRead(
+void QLowEnergyControllerPrivateAndroid::characteristicRead(
const QBluetoothUuid &serviceUuid, int handle,
const QBluetoothUuid &charUuid, int properties, const QByteArray &data)
{
@@ -594,7 +603,7 @@ void QLowEnergyControllerPrivate::characteristicRead(
}
}
-void QLowEnergyControllerPrivate::descriptorRead(
+void QLowEnergyControllerPrivateAndroid::descriptorRead(
const QBluetoothUuid &serviceUuid, const QBluetoothUuid &charUuid,
int descHandle, const QBluetoothUuid &descUuid, const QByteArray &data)
{
@@ -635,7 +644,7 @@ void QLowEnergyControllerPrivate::descriptorRead(
}
}
-void QLowEnergyControllerPrivate::characteristicWritten(
+void QLowEnergyControllerPrivateAndroid::characteristicWritten(
int charHandle, const QByteArray &data, QLowEnergyService::ServiceError errorCode)
{
QSharedPointer<QLowEnergyServicePrivate> service =
@@ -664,7 +673,7 @@ void QLowEnergyControllerPrivate::characteristicWritten(
emit service->characteristicWritten(characteristic, data);
}
-void QLowEnergyControllerPrivate::descriptorWritten(
+void QLowEnergyControllerPrivateAndroid::descriptorWritten(
int descHandle, const QByteArray &data, QLowEnergyService::ServiceError errorCode)
{
QSharedPointer<QLowEnergyServicePrivate> service =
@@ -691,7 +700,7 @@ void QLowEnergyControllerPrivate::descriptorWritten(
emit service->descriptorWritten(descriptor, data);
}
-void QLowEnergyControllerPrivate::serverDescriptorWritten(
+void QLowEnergyControllerPrivateAndroid::serverDescriptorWritten(
const QAndroidJniObject &jniDesc, const QByteArray &newValue)
{
qCDebug(QT_BT_ANDROID) << "Server descriptor change notification" << newValue.toHex();
@@ -756,7 +765,7 @@ void QLowEnergyControllerPrivate::serverDescriptorWritten(
}
}
-void QLowEnergyControllerPrivate::characteristicChanged(
+void QLowEnergyControllerPrivateAndroid::characteristicChanged(
int charHandle, const QByteArray &data)
{
QSharedPointer<QLowEnergyServicePrivate> service =
@@ -781,7 +790,7 @@ void QLowEnergyControllerPrivate::characteristicChanged(
emit service->characteristicChanged(characteristic, data);
}
-void QLowEnergyControllerPrivate::serverCharacteristicChanged(
+void QLowEnergyControllerPrivateAndroid::serverCharacteristicChanged(
const QAndroidJniObject &characteristic, const QByteArray &newValue)
{
qCDebug(QT_BT_ANDROID) << "Server characteristic change notification" << newValue.toHex();
@@ -830,7 +839,7 @@ void QLowEnergyControllerPrivate::serverCharacteristicChanged(
QLowEnergyCharacteristic(servicePrivate, foundHandle), newValue);
}
-void QLowEnergyControllerPrivate::serviceError(
+void QLowEnergyControllerPrivateAndroid::serviceError(
int attributeHandle, QLowEnergyService::ServiceError errorCode)
{
// ignore call if it isn't really an error
@@ -846,7 +855,7 @@ void QLowEnergyControllerPrivate::serviceError(
service->setError(errorCode);
}
-void QLowEnergyControllerPrivate::advertisementError(int errorCode)
+void QLowEnergyControllerPrivateAndroid::advertisementError(int errorCode)
{
Q_Q(QLowEnergyController);
@@ -997,7 +1006,7 @@ static QAndroidJniObject createJavaAdvertiseSettings(const QLowEnergyAdvertising
}
-void QLowEnergyControllerPrivate::startAdvertising(const QLowEnergyAdvertisingParameters &params,
+void QLowEnergyControllerPrivateAndroid::startAdvertising(const QLowEnergyAdvertisingParameters &params,
const QLowEnergyAdvertisingData &advertisingData,
const QLowEnergyAdvertisingData &scanResponseData)
{
@@ -1024,13 +1033,13 @@ void QLowEnergyControllerPrivate::startAdvertising(const QLowEnergyAdvertisingPa
}
}
-void QLowEnergyControllerPrivate::stopAdvertising()
+void QLowEnergyControllerPrivateAndroid::stopAdvertising()
{
setState(QLowEnergyController::UnconnectedState);
hub->javaObject().callMethod<void>("stopAdvertising");
}
-void QLowEnergyControllerPrivate::requestConnectionUpdate(const QLowEnergyConnectionParameters &params)
+void QLowEnergyControllerPrivateAndroid::requestConnectionUpdate(const QLowEnergyConnectionParameters &params)
{
// Possible since Android v21
// Android does not permit specification of specific latency or min/max
@@ -1169,7 +1178,7 @@ static int setupDescPermissions(const QLowEnergyDescriptorData &descData)
return permissions;
}
-void QLowEnergyControllerPrivate::addToGenericAttributeList(const QLowEnergyServiceData &serviceData,
+void QLowEnergyControllerPrivateAndroid::addToGenericAttributeList(const QLowEnergyServiceData &serviceData,
QLowEnergyHandle startHandle)
{
QSharedPointer<QLowEnergyServicePrivate> service = serviceForHandle(startHandle);