summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/android/devicediscoverybroadcastreceiver.cpp
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2014-10-20 08:39:04 +0200
committerAlex Blasche <alexander.blasche@digia.com>2014-10-28 13:59:43 +0100
commit04cb0d4af57cecdb6e47a6e2235083035580fa0d (patch)
treebcf62e2329f5aad1346f00b53e77c393dcc7961e /src/bluetooth/android/devicediscoverybroadcastreceiver.cpp
parenta7b0b599775864743d1436d3cbd9513f92eb2d06 (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/android/devicediscoverybroadcastreceiver.cpp')
-rw-r--r--src/bluetooth/android/devicediscoverybroadcastreceiver.cpp111
1 files changed, 67 insertions, 44 deletions
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