Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
servicediscoverybroadcastreceiver.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "android/servicediscoverybroadcastreceiver_p.h"
5#include <QCoreApplication>
6#include <QtCore/QLoggingCategory>
7#include <QtCore/QJniEnvironment>
8#include <QtBluetooth/QBluetoothAddress>
9#include <QtBluetooth/QBluetoothDeviceInfo>
10#include "android/jni_android_p.h"
11
13
14Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID)
15
16ServiceDiscoveryBroadcastReceiver::ServiceDiscoveryBroadcastReceiver(QObject* parent): AndroidBroadcastReceiver(parent)
17{
18 addAction(QJniObject::fromString(
19 valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ActionUuid>()));
20}
21
22void ServiceDiscoveryBroadcastReceiver::onReceive(JNIEnv *env, jobject context, jobject intent)
23{
24 Q_UNUSED(context);
25 Q_UNUSED(env);
26
27 QJniObject intentObject(intent);
28 const QString action = intentObject.callMethod<QString>("getAction");
29
30 qCDebug(QT_BT_ANDROID) << "ServiceDiscoveryBroadcastReceiver::onReceive() - event:" << action;
31
32 if (action == valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ActionUuid>()) {
33 QString keyExtra = valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ExtraUuid>();
34 const QJniArray parcelableUuids = intentObject.callMethod<QtJniTypes::Parcelable[]>(
35 "getParcelableArrayExtra", keyExtra);
36 if (!parcelableUuids.isValid()) {
37 emit uuidFetchFinished(QBluetoothAddress(), QList<QBluetoothUuid>());
38 return;
39 }
40 const QList<QBluetoothUuid> result = ServiceDiscoveryBroadcastReceiver::convertParcelableArray(parcelableUuids);
41
42 keyExtra = valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ExtraDevice>();
43 const QtJniTypes::Parcelable bluetoothDevice =
44 intentObject.callMethod<QtJniTypes::Parcelable>("getParcelableExtra", keyExtra);
45 QBluetoothAddress address;
46 if (bluetoothDevice.isValid()) {
47 address = QBluetoothAddress(bluetoothDevice.callMethod<QString>("getAddress"));
48 emit uuidFetchFinished(address, result);
49 } else {
50 emit uuidFetchFinished(QBluetoothAddress(), QList<QBluetoothUuid>());
51 }
52 }
53}
54
55QList<QBluetoothUuid> ServiceDiscoveryBroadcastReceiver::convertParcelableArray(const QJniArray<QJniObject> &parcelUuidArray)
56{
57 QList<QBluetoothUuid> result;
58 if (!parcelUuidArray.isValid())
59 return result;
60
61 for (const auto &parcel : parcelUuidArray) {
62 QBluetoothUuid uuid(parcel.callMethod<QString>("toString"));
63 //qCDebug(QT_BT_ANDROID) << uuid.toString();
64 result.append(uuid);
65 }
66
67 return result;
68}
69
70QT_END_NAMESPACE
virtual void onReceive(JNIEnv *env, jobject context, jobject intent)