Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
localdevicebroadcastreceiver.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 <QtCore/QLoggingCategory>
5#include <QtCore/qrandom.h>
8
10
11Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID)
12
13const char *scanModes[] = {"SCAN_MODE_NONE", "SCAN_MODE_CONNECTABLE", "SCAN_MODE_CONNECTABLE_DISCOVERABLE"};
14const char *bondModes[] = {"BOND_NONE", "BOND_BONDING", "BOND_BONDED"};
15
17 AndroidBroadcastReceiver(parent), previousScanMode(0)
18{
19 addAction(valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ActionBondStateChanged>());
20 addAction(valueForStaticField<QtJniTypes::BluetoothAdapter, JavaNames::ActionScanModeChanged>());
21 addAction(valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ActionAclConnected>());
22 addAction(valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ActionAclDisconnected>());
23
24 //cache integer values for host & bonding mode
25 //don't use the java fields directly but refer to them by name
26 for (uint i = 0; i < (sizeof(hostModePreset)/sizeof(hostModePreset[0])); i++) {
27 hostModePreset[i] = QJniObject::getStaticField<jint>(
28 QtJniTypes::className<QtJniTypes::BluetoothAdapter>(),
29 scanModes[i]);
30 }
31
32 for (uint i = 0; i < (sizeof(bondingModePreset)/sizeof(bondingModePreset[0])); i++) {
33 bondingModePreset[i] = QJniObject::getStaticField<jint>(
34 QtJniTypes::className<QtJniTypes::BluetoothDevice>(),
35 bondModes[i]);
36 }
37}
38
39void LocalDeviceBroadcastReceiver::onReceive(JNIEnv *env, jobject context, jobject intent)
40{
42 Q_UNUSED(env);
43
44 QJniObject intentObject(intent);
45 const QString action = intentObject.callMethod<jstring>("getAction").toString();
46 qCDebug(QT_BT_ANDROID) << QStringLiteral("LocalDeviceBroadcastReceiver::onReceive() - event: %1").arg(action);
47
48 if (action == valueForStaticField<QtJniTypes::BluetoothAdapter,
50
51 const QJniObject extrasBundle =
52 intentObject.callMethod<QtJniTypes::Bundle>("getExtras");
53 const QJniObject keyExtra = valueForStaticField<QtJniTypes::BluetoothAdapter,
55
56 int extra = extrasBundle.callMethod<jint>("getInt", keyExtra.object<jstring>());
57
58 if (previousScanMode != extra) {
59 previousScanMode = extra;
60
61 if (extra == hostModePreset[0])
63 else if (extra == hostModePreset[1])
65 else if (extra == hostModePreset[2])
67 else
68 qCWarning(QT_BT_ANDROID) << "Unknown Host State";
69 }
70 } else if (action == valueForStaticField<QtJniTypes::BluetoothDevice,
72 //get BluetoothDevice
73 QJniObject keyExtra = valueForStaticField<QtJniTypes::BluetoothDevice,
75 const QJniObject bluetoothDevice =
76 intentObject.callMethod<QtJniTypes::Parcelable>("getParcelableExtra",
77 keyExtra.object<jstring>());
78
79 //get new bond state
80 keyExtra = valueForStaticField<QtJniTypes::BluetoothDevice,
82 const QJniObject extrasBundle =
83 intentObject.callMethod<QtJniTypes::Bundle>("getExtras");
84 int bondState = extrasBundle.callMethod<jint>("getInt", keyExtra.object<jstring>());
85
86 QBluetoothAddress address(bluetoothDevice.callMethod<jstring>("getAddress").toString());
87 if (address.isNull())
88 return;
89
90 if (bondState == bondingModePreset[0])
92 else if (bondState == bondingModePreset[1])
93 ; //we ignore this as Qt doesn't have equivalent API value
94 else if (bondState == bondingModePreset[2])
96 else
97 qCWarning(QT_BT_ANDROID) << "Unknown BOND_STATE_CHANGED value:" << bondState;
98
99 } else if (action == valueForStaticField<QtJniTypes::BluetoothDevice,
101 action == valueForStaticField<QtJniTypes::BluetoothDevice,
103
104 const QString connectEvent = valueForStaticField<QtJniTypes::BluetoothDevice,
106 const bool isConnectEvent =
107 action == connectEvent ? true : false;
108
109 //get BluetoothDevice
110 const QJniObject keyExtra = valueForStaticField<QtJniTypes::BluetoothDevice,
112 QJniObject bluetoothDevice =
113 intentObject.callMethod<QtJniTypes::Parcelable>("getParcelableExtra",
114 keyExtra.object<jstring>());
115
116 QBluetoothAddress address(bluetoothDevice.callMethod<jstring>("getAddress").toString());
117 if (address.isNull())
118 return;
119
120 emit connectDeviceChanges(address, isConnectEvent);
121 }
122}
123
void addAction(const QJniObject &filter)
void pairingStateChanged(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
void connectDeviceChanges(const QBluetoothAddress &address, bool isConnectEvent)
void hostModeStateChanged(QBluetoothLocalDevice::HostMode state)
LocalDeviceBroadcastReceiver(QObject *parent=nullptr)
virtual void onReceive(JNIEnv *env, jobject context, jobject intent)
\inmodule QtBluetooth
\inmodule QtCore
\inmodule QtCore
Definition qobject.h:90
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
@ ExtraDevice
@ ExtraBondState
@ ActionScanModeChanged
@ ActionAclDisconnected
@ ActionBondStateChanged
@ ExtraScanMode
@ ActionAclConnected
QJniObject valueForStaticField()
QT_BEGIN_NAMESPACE const char * scanModes[]
const char * bondModes[]
Combined button and popup list for selecting options.
static void * context
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
GLuint GLuint64EXT address
#define QStringLiteral(str)
#define emit
#define Q_UNUSED(x)
unsigned int uint
Definition qtypes.h:29
char * toString(const MyType &t)
[31]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent