Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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(QJniObject::fromString(
20 valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ActionBondStateChanged>()));
21 addAction(QJniObject::fromString(
22 valueForStaticField<QtJniTypes::BluetoothAdapter, JavaNames::ActionScanModeChanged>()));
23 addAction(QJniObject::fromString(
24 valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ActionAclConnected>()));
25 addAction(QJniObject::fromString(
26 valueForStaticField<QtJniTypes::BluetoothDevice, JavaNames::ActionAclDisconnected>()));
27
28 //cache integer values for host & bonding mode
29 //don't use the java fields directly but refer to them by name
30 for (uint i = 0; i < (sizeof(hostModePreset)/sizeof(hostModePreset[0])); i++) {
31 hostModePreset[i] = QJniObject::getStaticField<jint>(
32 QtJniTypes::Traits<QtJniTypes::BluetoothAdapter>::className(),
33 scanModes[i]);
34 }
35
36 for (uint i = 0; i < (sizeof(bondingModePreset)/sizeof(bondingModePreset[0])); i++) {
37 bondingModePreset[i] = QJniObject::getStaticField<jint>(
38 QtJniTypes::Traits<QtJniTypes::BluetoothDevice>::className(),
39 bondModes[i]);
40 }
41}
42
43void LocalDeviceBroadcastReceiver::onReceive(JNIEnv *env, jobject context, jobject intent)
44{
46 Q_UNUSED(env);
47
48 QJniObject intentObject(intent);
49 const QString action = intentObject.callMethod<jstring>("getAction").toString();
50 qCDebug(QT_BT_ANDROID) << QStringLiteral("LocalDeviceBroadcastReceiver::onReceive() - event: %1").arg(action);
51
52 if (action == valueForStaticField<QtJniTypes::BluetoothAdapter,
54
55 const QJniObject extrasBundle =
56 intentObject.callMethod<QtJniTypes::Bundle>("getExtras");
57 const QJniObject keyExtra =
58 QJniObject::fromString(valueForStaticField<QtJniTypes::BluetoothAdapter,
60
61 int extra = extrasBundle.callMethod<jint>("getInt", keyExtra.object<jstring>());
62
63 if (previousScanMode != extra) {
64 previousScanMode = extra;
65
66 if (extra == hostModePreset[0])
68 else if (extra == hostModePreset[1])
70 else if (extra == hostModePreset[2])
72 else
73 qCWarning(QT_BT_ANDROID) << "Unknown Host State";
74 }
75 } else if (action == valueForStaticField<QtJniTypes::BluetoothDevice,
77 //get BluetoothDevice
78 QJniObject keyExtra = QJniObject::fromString(
79 valueForStaticField<QtJniTypes::BluetoothDevice,
81 const QJniObject bluetoothDevice =
82 intentObject.callMethod<QtJniTypes::Parcelable>("getParcelableExtra",
83 keyExtra.object<jstring>());
84
85 //get new bond state
86 keyExtra = QJniObject::fromString(valueForStaticField<QtJniTypes::BluetoothDevice,
88 const QJniObject extrasBundle =
89 intentObject.callMethod<QtJniTypes::Bundle>("getExtras");
90 int bondState = extrasBundle.callMethod<jint>("getInt", keyExtra.object<jstring>());
91
92 QBluetoothAddress address(bluetoothDevice.callMethod<jstring>("getAddress").toString());
93 if (address.isNull())
94 return;
95
96 if (bondState == bondingModePreset[0])
98 else if (bondState == bondingModePreset[1])
99 ; //we ignore this as Qt doesn't have equivalent API value
100 else if (bondState == bondingModePreset[2])
102 else
103 qCWarning(QT_BT_ANDROID) << "Unknown BOND_STATE_CHANGED value:" << bondState;
104
105 } else if (action == valueForStaticField<QtJniTypes::BluetoothDevice,
107 action == valueForStaticField<QtJniTypes::BluetoothDevice,
109
110 const QString connectEvent = valueForStaticField<QtJniTypes::BluetoothDevice,
112 const bool isConnectEvent =
113 action == connectEvent ? true : false;
114
115 //get BluetoothDevice
116 const QJniObject keyExtra =
117 QJniObject::fromString(valueForStaticField<QtJniTypes::BluetoothDevice,
119 QJniObject bluetoothDevice =
120 intentObject.callMethod<QtJniTypes::Parcelable>("getParcelableExtra",
121 keyExtra.object<jstring>());
122
123 QBluetoothAddress address(bluetoothDevice.callMethod<jstring>("getAddress").toString());
124 if (address.isNull())
125 return;
126
127 emit connectDeviceChanges(address, isConnectEvent);
128 }
129}
130
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:103
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString valueForStaticField()
@ ExtraDevice
@ ExtraBondState
@ ActionScanModeChanged
@ ActionAclDisconnected
@ ActionBondStateChanged
@ ExtraScanMode
@ ActionAclConnected
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:34
char * toString(const MyType &t)
[31]