diff options
author | Alex Blasche <alexander.blasche@digia.com> | 2014-10-20 08:39:04 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@digia.com> | 2014-10-28 13:59:43 +0100 |
commit | 04cb0d4af57cecdb6e47a6e2235083035580fa0d (patch) | |
tree | bcf62e2329f5aad1346f00b53e77c393dcc7961e /src/bluetooth | |
parent | a7b0b599775864743d1436d3cbd9513f92eb2d06 (diff) |
Android: Add BluetoothLE device scan
Change-Id: Ibbb1e9f141d494327082aebaf9e34ffe44039115
Reviewed-by: Timur Pocheptsov <Timur.Pocheptsov@digia.com>
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src/bluetooth')
8 files changed, 212 insertions, 75 deletions
diff --git a/src/bluetooth/android/androidbroadcastreceiver_p.h b/src/bluetooth/android/androidbroadcastreceiver_p.h index 347cd968..e09eb6e0 100644 --- a/src/bluetooth/android/androidbroadcastreceiver_p.h +++ b/src/bluetooth/android/androidbroadcastreceiver_p.h @@ -69,6 +69,8 @@ public: protected: friend void QtBroadcastReceiver_jniOnReceive(JNIEnv *, jobject, jlong, jobject, jobject); virtual void onReceive(JNIEnv *env, jobject context, jobject intent) = 0; + friend void QtBluetoothLE_leScanResult(JNIEnv *, jobject, jlong, jobject, jint); + virtual void onReceiveLeScan(JNIEnv *env, jobject jBluetoothDevice, jint rssi) = 0; QAndroidJniObject activityObject; diff --git a/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp b/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp index db02bc46..4818fa0f 100644 --- a/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp +++ b/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp @@ -96,6 +96,7 @@ DeviceDiscoveryBroadcastReceiver::DeviceDiscoveryBroadcastReceiver(QObject* pare addAction(valueForStaticField(JavaNames::BluetoothAdapter, JavaNames::ActionDiscoveryFinished)); } +// Runs in Java thread void DeviceDiscoveryBroadcastReceiver::onReceive(JNIEnv *env, jobject context, jobject intent) { Q_UNUSED(context); @@ -125,62 +126,84 @@ void DeviceDiscoveryBroadcastReceiver::onReceive(JNIEnv *env, jobject context, j if (!bluetoothDevice.isValid()) return; - const QString deviceName = bluetoothDevice.callObjectMethod<jstring>("getName").toString(); - const QBluetoothAddress deviceAddress(bluetoothDevice.callObjectMethod<jstring>("getAddress").toString()); keyExtra = valueForStaticField(JavaNames::BluetoothDevice, JavaNames::ExtraRssi); - int rssi = intentObject.callMethod<jshort>("getShortExtra", "(Ljava/lang/String;S)S", keyExtra.object<jstring>(), 0); - const QAndroidJniObject bluetoothClass = bluetoothDevice.callObjectMethod("getBluetoothClass", - "()Landroid/bluetooth/BluetoothClass;"); - if (!bluetoothClass.isValid()) - return; - int classType = bluetoothClass.callMethod<jint>("getDeviceClass"); - - - static QList<qint32> services; - if (services.count() == 0) - services << QBluetoothDeviceInfo::PositioningService - << QBluetoothDeviceInfo::NetworkingService - << QBluetoothDeviceInfo::RenderingService - << QBluetoothDeviceInfo::CapturingService - << QBluetoothDeviceInfo::ObjectTransferService - << QBluetoothDeviceInfo::AudioService - << QBluetoothDeviceInfo::TelephonyService - << QBluetoothDeviceInfo::InformationService; - - //Matching BluetoothClass.Service values - qint32 result = 0; - qint32 current = 0; - for (int i = 0; i < services.count(); i++) { - current = services.at(i); - int id = (current << 16); - if (bluetoothClass.callMethod<jboolean>("hasService", "(I)Z", id)) - result |= current; - } - result = result << 13; - classType |= result; + const QBluetoothDeviceInfo info = retrieveDeviceInfo(env, bluetoothDevice, rssi); + if (info.isValid()) + emit deviceDiscovered(info, false); + } +} - QBluetoothDeviceInfo info(deviceAddress, deviceName, classType); - info.setRssi(rssi); +// Runs in Java thread +void DeviceDiscoveryBroadcastReceiver::onReceiveLeScan( + JNIEnv *env, jobject jBluetoothDevice, jint rssi) +{ + qCDebug(QT_BT_ANDROID) << "DeviceDiscoveryBroadcastReceiver::onReceiveLeScan()"; + const QAndroidJniObject bluetoothDevice(jBluetoothDevice); + if (!bluetoothDevice.isValid()) + return; + + const QBluetoothDeviceInfo info = retrieveDeviceInfo(env, bluetoothDevice, rssi); + if (info.isValid()) + emit deviceDiscovered(info, true); +} - if (QtAndroidPrivate::androidSdkVersion() >= 18) { - jint javaBtType = bluetoothDevice.callMethod<jint>("getType"); +QBluetoothDeviceInfo DeviceDiscoveryBroadcastReceiver::retrieveDeviceInfo(JNIEnv *env, const QAndroidJniObject &bluetoothDevice, int rssi) +{ + const QString deviceName = bluetoothDevice.callObjectMethod<jstring>("getName").toString(); + const QBluetoothAddress deviceAddress(bluetoothDevice.callObjectMethod<jstring>("getAddress").toString()); + + const QAndroidJniObject bluetoothClass = bluetoothDevice.callObjectMethod("getBluetoothClass", + "()Landroid/bluetooth/BluetoothClass;"); + if (!bluetoothClass.isValid()) + return QBluetoothDeviceInfo(); + int classType = bluetoothClass.callMethod<jint>("getDeviceClass"); + + + static QList<qint32> services; + if (services.count() == 0) + services << QBluetoothDeviceInfo::PositioningService + << QBluetoothDeviceInfo::NetworkingService + << QBluetoothDeviceInfo::RenderingService + << QBluetoothDeviceInfo::CapturingService + << QBluetoothDeviceInfo::ObjectTransferService + << QBluetoothDeviceInfo::AudioService + << QBluetoothDeviceInfo::TelephonyService + << QBluetoothDeviceInfo::InformationService; + + //Matching BluetoothClass.Service values + qint32 result = 0; + qint32 current = 0; + for (int i = 0; i < services.count(); i++) { + current = services.at(i); + int id = (current << 16); + if (bluetoothClass.callMethod<jboolean>("hasService", "(I)Z", id)) + result |= current; + } - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - env->ExceptionClear(); - } else { - info.setCoreConfigurations(qtBtTypeForJavaBtType(javaBtType)); - } - } + result = result << 13; + classType |= result; + + QBluetoothDeviceInfo info(deviceAddress, deviceName, classType); + info.setRssi(rssi); + + if (QtAndroidPrivate::androidSdkVersion() >= 18) { + jint javaBtType = bluetoothDevice.callMethod<jint>("getType"); - emit deviceDiscovered(info); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + env->ExceptionClear(); + } else { + info.setCoreConfigurations(qtBtTypeForJavaBtType(javaBtType)); + } } + + return info; } QT_END_NAMESPACE diff --git a/src/bluetooth/android/devicediscoverybroadcastreceiver_p.h b/src/bluetooth/android/devicediscoverybroadcastreceiver_p.h index 4ed7027d..5378e9aa 100644 --- a/src/bluetooth/android/devicediscoverybroadcastreceiver_p.h +++ b/src/bluetooth/android/devicediscoverybroadcastreceiver_p.h @@ -59,10 +59,15 @@ class DeviceDiscoveryBroadcastReceiver : public AndroidBroadcastReceiver public: DeviceDiscoveryBroadcastReceiver(QObject* parent = 0); virtual void onReceive(JNIEnv *env, jobject context, jobject intent); + virtual void onReceiveLeScan(JNIEnv *env, jobject jBluetoothDevice, jint rssi); signals: - void deviceDiscovered(const QBluetoothDeviceInfo &info); + void deviceDiscovered(const QBluetoothDeviceInfo &info, bool isLeScanResult); void finished(); + +private: + QBluetoothDeviceInfo retrieveDeviceInfo(JNIEnv *env, const QAndroidJniObject& bluetoothDevice, + int rssi); }; QT_END_NAMESPACE diff --git a/src/bluetooth/android/jni_android.cpp b/src/bluetooth/android/jni_android.cpp index faeb411a..061d6ce2 100644 --- a/src/bluetooth/android/jni_android.cpp +++ b/src/bluetooth/android/jni_android.cpp @@ -183,12 +183,23 @@ static void QtBluetoothInputStreamThread_readyData(JNIEnv */*env*/, jobject /*ja reinterpret_cast<InputStreamThread*>(qtObject)->javaReadyRead(buffer, bufferLength); } +void QtBluetoothLE_leScanResult(JNIEnv *env, jobject, jlong qtObject, jobject bluetoothDevice, jint rssi) +{ + reinterpret_cast<AndroidBroadcastReceiver*>(qtObject)->onReceiveLeScan( + env, bluetoothDevice, rssi); +} + static JNINativeMethod methods[] = { {"jniOnReceive", "(JLandroid/content/Context;Landroid/content/Intent;)V", (void *) QtBroadcastReceiver_jniOnReceive}, }; +static JNINativeMethod methods_le[] = { + {"leScanResult", "(JLandroid/bluetooth/BluetoothDevice;I)V", + (void *) QtBluetoothLE_leScanResult}, +}; + static JNINativeMethod methods_server[] = { {"errorOccurred", "(JI)V", (void *) QtBluetoothSocketServer_errorOccurred}, @@ -218,12 +229,18 @@ static bool registerNatives(JNIEnv *env) jclass clazz; FIND_AND_CHECK_CLASS("org/qtproject/qt5/android/bluetooth/QtBluetoothBroadcastReceiver"); - if (env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])) < 0) { __android_log_print(ANDROID_LOG_FATAL, logTag, "RegisterNatives for BraodcastReceiver failed"); return false; } + FIND_AND_CHECK_CLASS("org/qtproject/qt5/android/bluetooth/QtBluetoothLE"); + if (env->RegisterNatives(clazz, methods_le, sizeof(methods_le) / sizeof(methods_le[0])) < 0) { + __android_log_print(ANDROID_LOG_FATAL, logTag, "RegisterNatives for QBLuetoothLE failed"); + return false; + } + + FIND_AND_CHECK_CLASS("org/qtproject/qt5/android/bluetooth/QtBluetoothSocketServer"); if (env->RegisterNatives(clazz, methods_server, sizeof(methods_server) / sizeof(methods_server[0])) < 0) { __android_log_print(ANDROID_LOG_FATAL, logTag, "RegisterNatives for SocketServer failed"); diff --git a/src/bluetooth/android/localdevicebroadcastreceiver_p.h b/src/bluetooth/android/localdevicebroadcastreceiver_p.h index 190ae631..c3830c8c 100644 --- a/src/bluetooth/android/localdevicebroadcastreceiver_p.h +++ b/src/bluetooth/android/localdevicebroadcastreceiver_p.h @@ -58,6 +58,7 @@ public: explicit LocalDeviceBroadcastReceiver(QObject *parent = 0); virtual ~LocalDeviceBroadcastReceiver() {} virtual void onReceive(JNIEnv *env, jobject context, jobject intent); + virtual void onReceiveLeScan(JNIEnv *, jobject, jint) {} bool pairingConfirmation(bool accept); signals: diff --git a/src/bluetooth/android/servicediscoverybroadcastreceiver_p.h b/src/bluetooth/android/servicediscoverybroadcastreceiver_p.h index 273af121..3e9e9c7f 100644 --- a/src/bluetooth/android/servicediscoverybroadcastreceiver_p.h +++ b/src/bluetooth/android/servicediscoverybroadcastreceiver_p.h @@ -60,6 +60,7 @@ class ServiceDiscoveryBroadcastReceiver : public AndroidBroadcastReceiver public: ServiceDiscoveryBroadcastReceiver(QObject* parent = 0); virtual void onReceive(JNIEnv *env, jobject context, jobject intent); + virtual void onReceiveLeScan(JNIEnv *, jobject, jint) {} static QList<QBluetoothUuid> convertParcelableArray(const QAndroidJniObject &obj); diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp b/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp index 14d385d8..28865d9c 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_android.cpp @@ -36,12 +36,20 @@ #include <QtCore/QLoggingCategory> #include <QtBluetooth/QBluetoothAddress> #include <QtBluetooth/QBluetoothDeviceInfo> +#include <QtCore/private/qjnihelpers_p.h> #include "android/devicediscoverybroadcastreceiver_p.h" +#include <QtAndroidExtras/QAndroidJniEnvironment> QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID) +enum { + NoScanActive = 0, + SDPScanActive = 1, + BtleScanActive = 2 +}; + QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( const QBluetoothAddress &deviceAdapter, QBluetoothDeviceDiscoveryAgent *parent) : inquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry), @@ -49,7 +57,8 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( errorString(QStringLiteral()), receiver(0), m_adapterAddress(deviceAdapter), - m_active(false), + m_active(NoScanActive), + leScanTimeout(0), pendingCancel(false), pendingStart(false), q_ptr(parent) @@ -61,7 +70,7 @@ QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate( QBluetoothDeviceDiscoveryAgentPrivate::~QBluetoothDeviceDiscoveryAgentPrivate() { - if (m_active) + if (m_active != NoScanActive) stop(); if (receiver) { @@ -76,7 +85,7 @@ bool QBluetoothDeviceDiscoveryAgentPrivate::isActive() const return true; if (pendingCancel) return false; - return m_active; + return m_active != NoScanActive; } void QBluetoothDeviceDiscoveryAgentPrivate::start() @@ -116,11 +125,12 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start() // install Java BroadcastReceiver if (!receiver) { + // SDP based device discovery receiver = new DeviceDiscoveryBroadcastReceiver(); qRegisterMetaType<QBluetoothDeviceInfo>("QBluetoothDeviceInfo"); - QObject::connect(receiver, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), - this, SLOT(processDiscoveredDevices(QBluetoothDeviceInfo))); - QObject::connect(receiver, SIGNAL(finished()), this, SLOT(processDiscoveryFinished())); + QObject::connect(receiver, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo,bool)), + this, SLOT(processDiscoveredDevices(QBluetoothDeviceInfo,bool))); + QObject::connect(receiver, SIGNAL(finished()), this, SLOT(processSdpDiscoveryFinished())); } discoveredDevices.clear(); @@ -133,7 +143,7 @@ void QBluetoothDeviceDiscoveryAgentPrivate::start() return; } - m_active = true; + m_active = SDPScanActive; qCDebug(QT_BT_ANDROID) << "QBluetoothDeviceDiscoveryAgentPrivate::start() - successfully executed."; @@ -143,35 +153,38 @@ void QBluetoothDeviceDiscoveryAgentPrivate::stop() { Q_Q(QBluetoothDeviceDiscoveryAgent); - if (!m_active) + if (m_active == NoScanActive) return; - pendingCancel = true; - pendingStart = false; - bool success = adapter.callMethod<jboolean>("cancelDiscovery"); - if (!success) { - lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError; - errorString = QBluetoothDeviceDiscoveryAgent::tr("Discovery cannot be stopped"); - emit q->error(lastError); - return; + if (m_active == SDPScanActive) { + pendingCancel = true; + pendingStart = false; + bool success = adapter.callMethod<jboolean>("cancelDiscovery"); + if (!success) { + lastError = QBluetoothDeviceDiscoveryAgent::InputOutputError; + errorString = QBluetoothDeviceDiscoveryAgent::tr("Discovery cannot be stopped"); + emit q->error(lastError); + return; + } + } else if (m_active == BtleScanActive) { + stopLowEnergyScan(); } } -void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveryFinished() +void QBluetoothDeviceDiscoveryAgentPrivate::processSdpDiscoveryFinished() { // We need to guard because Android sends two DISCOVERY_FINISHED when cancelling // Also if we have two active agents both receive the same signal. // If this one is not active ignore the device information - if (!m_active) + if (m_active != SDPScanActive) return; - m_active = false; - Q_Q(QBluetoothDeviceDiscoveryAgent); if (pendingCancel && !pendingStart) { - emit q->canceled(); + m_active = NoScanActive; pendingCancel = false; + emit q->canceled(); } else if (pendingStart) { pendingStart = pendingCancel = false; start(); @@ -179,21 +192,31 @@ void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveryFinished() // check that it didn't finish due to turned off Bluetooth Device const int state = adapter.callMethod<jint>("getState"); if (state != 12) { // BluetoothAdapter.STATE_ON + m_active = NoScanActive; lastError = QBluetoothDeviceDiscoveryAgent::PoweredOffError; errorString = QBluetoothDeviceDiscoveryAgent::tr("Device is powered off"); emit q->error(lastError); return; } - emit q->finished(); + + // start LE scan if supported + if (QtAndroidPrivate::androidSdkVersion() < 18) { + qCDebug(QT_BT_ANDROID) << "Skipping Bluetooth Low Energy device scan"; + emit q->finished(); + } else { + startLowEnergyScan(); + } } } void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevices( - const QBluetoothDeviceInfo &info) + const QBluetoothDeviceInfo &info, bool isLeResult) { // If we have two active agents both receive the same signal. // If this one is not active ignore the device information - if (!m_active) + if (m_active != SDPScanActive && !isLeResult) + return; + if (m_active != BtleScanActive && isLeResult) return; Q_Q(QBluetoothDeviceDiscoveryAgent); @@ -201,7 +224,8 @@ void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevices( for (int i = 0; i < discoveredDevices.size(); i++) { if (discoveredDevices[i].address() == info.address()) { if (discoveredDevices[i] == info) { - qCDebug(QT_BT_ANDROID) << "Duplicate: " << info.address(); + qCDebug(QT_BT_ANDROID) << "Duplicate: " << info.address() + << "isLeScanResult:" << isLeResult; return; } @@ -214,8 +238,65 @@ void QBluetoothDeviceDiscoveryAgentPrivate::processDiscoveredDevices( } discoveredDevices.append(info); - qCDebug(QT_BT_ANDROID) << "Device found: " << info.name() << info.address().toString(); + qCDebug(QT_BT_ANDROID) << "Device found: " << info.name() << info.address().toString() + << "isLeScanResult:" << isLeResult; emit q->deviceDiscovered(info); } +void QBluetoothDeviceDiscoveryAgentPrivate::startLowEnergyScan() +{ + Q_Q(QBluetoothDeviceDiscoveryAgent); + + m_active = BtleScanActive; + + QAndroidJniEnvironment env; + if (!leScanner.isValid()) { + leScanner = QAndroidJniObject("org/qtproject/qt5/android/bluetooth/QtBluetoothLE"); + if (env->ExceptionCheck() || !leScanner.isValid()) { + qCWarning(QT_BT_ANDROID) << "Cannot load BTLE device scan class"; + env->ExceptionDescribe(); + env->ExceptionClear(); + m_active = NoScanActive; + emit q->finished(); + } + + leScanner.setField<jlong>("qtObject", reinterpret_cast<long>(receiver)); + } + + jboolean result = leScanner.callMethod<jboolean>("scanForLeDevice", "(Z)Z", true); + if (!result) { + qCWarning(QT_BT_ANDROID) << "Cannot start BTLE device scanner"; + m_active = NoScanActive; + emit q->finished(); + } + + if (!leScanTimeout) { + leScanTimeout = new QTimer(this); + leScanTimeout->setSingleShot(true); + leScanTimeout->setInterval(10000); + connect(leScanTimeout, &QTimer::timeout, + this, &QBluetoothDeviceDiscoveryAgentPrivate::stopLowEnergyScan); + } + + leScanTimeout->start(); +} + +void QBluetoothDeviceDiscoveryAgentPrivate::stopLowEnergyScan() +{ + jboolean result = leScanner.callMethod<jboolean>("scanForLeDevice", "(Z)Z", false); + if (!result) + qCWarning(QT_BT_ANDROID) << "Cannot stop BTLE device scanner"; + + m_active = NoScanActive; + + Q_Q(QBluetoothDeviceDiscoveryAgent); + if (leScanTimeout->isActive()) { + // still active if this function was called from stop() + leScanTimeout->stop(); + emit q->canceled(); + } else { + // timeout -> regular stop + emit q->finished(); + } +} QT_END_NAMESPACE diff --git a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h index f25496dc..1e269e1f 100644 --- a/src/bluetooth/qbluetoothdevicediscoveryagent_p.h +++ b/src/bluetooth/qbluetoothdevicediscoveryagent_p.h @@ -49,6 +49,7 @@ #ifdef QT_ANDROID_BLUETOOTH #include <QtAndroidExtras/QAndroidJniObject> #include "android/devicediscoverybroadcastreceiver_p.h" +#include <QtCore/QTimer> #endif #include <QtCore/QVariantMap> @@ -117,14 +118,20 @@ private: #ifdef QT_ANDROID_BLUETOOTH private slots: - void processDiscoveryFinished(); - void processDiscoveredDevices(const QBluetoothDeviceInfo &info); + void processSdpDiscoveryFinished(); + void processDiscoveredDevices(const QBluetoothDeviceInfo &info, bool isLeResult); + friend void QtBluetoothLE_leScanResult(JNIEnv *, jobject, jlong, jobject); + void stopLowEnergyScan(); private: + void startLowEnergyScan(); + DeviceDiscoveryBroadcastReceiver *receiver; QBluetoothAddress m_adapterAddress; - bool m_active; + short m_active; QAndroidJniObject adapter; + QAndroidJniObject leScanner; + QTimer *leScanTimeout; bool pendingCancel, pendingStart; #elif defined(QT_BLUEZ_BLUETOOTH) |