5#include "android/jni_android_p.h"
7#include <QCoreApplication>
9#include <QtCore/QLoggingCategory>
10#include <QtCore/QRandomGenerator>
11#include <QtCore/QJniEnvironment>
16Q_GLOBAL_STATIC(HubMapType, hubMap)
18QReadWriteLock LowEnergyNotificationHub::lock;
20Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID)
22LowEnergyNotificationHub::LowEnergyNotificationHub(
const QBluetoothAddress &remote,
23 bool isPeripheral, QObject *parent)
24 : QObject(parent), javaToCtoken(0)
29 qCDebug(QT_BT_ANDROID) <<
"Creating Android Peripheral/Server support for BTLE";
30 jBluetoothLe = QJniObject::construct<QtJniTypes::QtBtLEServer>(
31 QNativeInterface::QAndroidApplication::context());
33 qCDebug(QT_BT_ANDROID) <<
"Creating Android Central/Client support for BTLE";
34 const QJniObject address =
35 QJniObject::fromString(remote.toString());
36 jBluetoothLe = QJniObject::construct<QtJniTypes::QtBtLECentral>(
37 address.object<jstring>(), QNativeInterface::QAndroidApplication::context());
40 if (!jBluetoothLe.isValid())
return;
46 javaToCtoken = QRandomGenerator::global()->generate();
47 if (!hubMap()->contains(javaToCtoken))
51 hubMap()->insert(javaToCtoken,
this);
54 jBluetoothLe.setField<jlong>(
"qtObject", javaToCtoken);
60 hubMap()->remove(javaToCtoken);
73 QMetaObject::invokeMethod(hub,
"connectionUpdated", Qt::QueuedConnection,
74 Q_ARG(QLowEnergyController::ControllerState,
75 (QLowEnergyController::ControllerState)newState),
76 Q_ARG(QLowEnergyController::Error,
77 (QLowEnergyController::Error)errorCode));
81 JNIEnv *, jobject, jlong qtObject, jint mtu)
89 QMetaObject::invokeMethod(hub,
"mtuChanged", Qt::QueuedConnection, Q_ARG(
int, mtu));
93 int rssi,
bool success)
101 QMetaObject::invokeMethod(hub,
"remoteRssiRead", Qt::QueuedConnection,
102 Q_ARG(
int, rssi), Q_ARG(
bool, success));
107 JNIEnv *, jobject, jlong qtObject, jint errorCode, jstring uuidList)
115 const QString uuids = QJniObject(uuidList).toString();
116 QMetaObject::invokeMethod(hub,
"servicesDiscovered", Qt::QueuedConnection,
117 Q_ARG(QLowEnergyController::Error,
118 (QLowEnergyController::Error)errorCode),
119 Q_ARG(QString, uuids));
123 JNIEnv *, jobject, jlong qtObject, jstring uuid, jint startHandle,
132 const QString serviceUuid = QJniObject(uuid).toString();
133 QMetaObject::invokeMethod(hub,
"serviceDetailsDiscoveryFinished",
134 Qt::QueuedConnection,
135 Q_ARG(QString, serviceUuid),
136 Q_ARG(
int, startHandle),
137 Q_ARG(
int, endHandle));
141 JNIEnv *env, jobject, jlong qtObject, jstring sUuid, jint handle,
142 jstring cUuid, jint properties, jbyteArray data)
150 const QBluetoothUuid serviceUuid(QJniObject(sUuid).toString());
151 if (serviceUuid.isNull())
154 const QBluetoothUuid charUuid(QJniObject(cUuid).toString());
155 if (charUuid.isNull())
160 jsize length = env->GetArrayLength(data);
161 payload.resize(length);
162 env->GetByteArrayRegion(data, 0, length,
163 reinterpret_cast<
signed char*>(payload.data()));
166 QMetaObject::invokeMethod(hub,
"characteristicRead", Qt::QueuedConnection,
167 Q_ARG(QBluetoothUuid, serviceUuid),
169 Q_ARG(QBluetoothUuid, charUuid),
170 Q_ARG(
int, properties),
171 Q_ARG(QByteArray, payload));
176 JNIEnv *env, jobject, jlong qtObject, jstring sUuid, jstring cUuid,
177 jint handle, jstring dUuid, jbyteArray data)
185 const QBluetoothUuid serviceUuid(QJniObject(sUuid).toString());
186 if (serviceUuid.isNull())
189 const QBluetoothUuid charUuid(QJniObject(cUuid).toString());
190 const QBluetoothUuid descUuid(QJniObject(dUuid).toString());
191 if (charUuid.isNull() || descUuid.isNull())
196 jsize length = env->GetArrayLength(data);
197 payload.resize(length);
198 env->GetByteArrayRegion(data, 0, length,
199 reinterpret_cast<
signed char*>(payload.data()));
202 QMetaObject::invokeMethod(hub,
"descriptorRead", Qt::QueuedConnection,
203 Q_ARG(QBluetoothUuid, serviceUuid),
204 Q_ARG(QBluetoothUuid, charUuid),
206 Q_ARG(QBluetoothUuid, descUuid),
207 Q_ARG(QByteArray, payload));
211 JNIEnv *env, jobject, jlong qtObject, jint charHandle,
212 jbyteArray data, jint errorCode)
222 jsize length = env->GetArrayLength(data);
223 payload.resize(length);
224 env->GetByteArrayRegion(data, 0, length,
225 reinterpret_cast<
signed char*>(payload.data()));
228 QMetaObject::invokeMethod(hub,
"characteristicWritten", Qt::QueuedConnection,
229 Q_ARG(
int, charHandle),
230 Q_ARG(QByteArray, payload),
231 Q_ARG(QLowEnergyService::ServiceError,
232 (QLowEnergyService::ServiceError)errorCode));
236 JNIEnv *env, jobject, jlong qtObject, jint descHandle,
237 jbyteArray data, jint errorCode)
247 jsize length = env->GetArrayLength(data);
248 payload.resize(length);
249 env->GetByteArrayRegion(data, 0, length,
250 reinterpret_cast<
signed char*>(payload.data()));
253 QMetaObject::invokeMethod(hub,
"descriptorWritten", Qt::QueuedConnection,
254 Q_ARG(
int, descHandle),
255 Q_ARG(QByteArray, payload),
256 Q_ARG(QLowEnergyService::ServiceError,
257 (QLowEnergyService::ServiceError)errorCode));
261 JNIEnv *env, jobject, jlong qtObject, QtJniTypes::BluetoothGattDescriptor descriptor,
272 jsize length = env->GetArrayLength(newValue);
273 payload.resize(length);
274 env->GetByteArrayRegion(newValue, 0, length,
275 reinterpret_cast<
signed char*>(payload.data()));
278 QMetaObject::invokeMethod(hub,
"serverDescriptorWritten", Qt::QueuedConnection,
279 Q_ARG(QJniObject, descriptor),
280 Q_ARG(QByteArray, payload));
284 JNIEnv *env, jobject, jlong qtObject, jint charHandle, jbyteArray data)
294 jsize length = env->GetArrayLength(data);
295 payload.resize(length);
296 env->GetByteArrayRegion(data, 0, length,
297 reinterpret_cast<
signed char*>(payload.data()));
300 QMetaObject::invokeMethod(hub,
"characteristicChanged", Qt::QueuedConnection,
301 Q_ARG(
int, charHandle), Q_ARG(QByteArray, payload));
305 JNIEnv *env, jobject, jlong qtObject,
306 QtJniTypes::BluetoothGattCharacteristic characteristic, jbyteArray newValue)
316 jsize length = env->GetArrayLength(newValue);
317 payload.resize(length);
318 env->GetByteArrayRegion(newValue, 0, length,
319 reinterpret_cast<
signed char*>(payload.data()));
322 QMetaObject::invokeMethod(hub,
"serverCharacteristicChanged", Qt::QueuedConnection,
323 Q_ARG(QJniObject, characteristic),
324 Q_ARG(QByteArray, payload));
328 JNIEnv *, jobject, jlong qtObject, jint attributeHandle,
int errorCode)
336 QMetaObject::invokeMethod(hub,
"serviceError", Qt::QueuedConnection,
337 Q_ARG(
int, attributeHandle),
338 Q_ARG(QLowEnergyService::ServiceError,
339 (QLowEnergyService::ServiceError)errorCode));
343 JNIEnv *, jobject, jlong qtObject, jint status)
351 QMetaObject::invokeMethod(hub,
"advertisementError", Qt::QueuedConnection,
~LowEnergyNotificationHub()
QT_BEGIN_NAMESPACE typedef QHash< long, LowEnergyNotificationHub * > HubMapType