9#include <QCoreApplication>
10#include <QtCore/QLoggingCategory>
11#include <QtCore/QJniEnvironment>
12#include <QtCore/QJniObject>
13#include <QtBluetooth/QBluetoothLocalDevice>
14#include <QtBluetooth/QBluetoothAddress>
30 this, &QBluetoothLocalDevicePrivate::processHostModeChange);
32 this, &QBluetoothLocalDevicePrivate::processPairingStateChanged);
34 this, &QBluetoothLocalDevicePrivate::processConnectDeviceChanges);
39 receiver->unregisterReceiver();
44QJniObject *QBluetoothLocalDevicePrivate::adapter()
53 if (!adapter.isValid()) {
54 qCWarning(QT_BT_ANDROID) <<
"Device does not support Bluetooth";
59 qCWarning(QT_BT_ANDROID) <<
"Local device initialize() failed due to missing permissions";
66 if (localAddress !=
address.toString()) {
76 return obj ? true :
false;
81 qCDebug(QT_BT_ANDROID) <<
"Processing host mode change:" << newMode
82 <<
", pending transition:" << pendingConnectableHostModeTransition;
83 if (!pendingConnectableHostModeTransition) {
92 const bool success = (bool)QJniObject::callStaticMethod<jboolean>(
93 QtJniTypes::className<QtJniTypes::QtBtBroadcastReceiver>(),
96 qCWarning(QT_BT_ANDROID) <<
"Transitioning Bluetooth from OFF to ON failed";
100 pendingConnectableHostModeTransition =
false;
107 for (
qsizetype i = 0;
i < pendingPairings.size(); ++
i) {
108 if (pendingPairings.at(
i).first ==
address)
115void QBluetoothLocalDevicePrivate::processPairingStateChanged(
135 if (isConnectEvent) {
136 if (connectedDevices.contains(
address))
138 connectedDevices.append(
address);
141 connectedDevices.removeAll(
address);
160 if (d_ptr->adapter())
161 return d_ptr->adapter()->callMethod<jstring>(
"getName").
toString();
169 if (d_ptr->adapter())
170 result = d_ptr->adapter()->callMethod<jstring>(
"getAddress").
toString();
181 if (d_ptr->adapter()) {
183 if (QNativeInterface::QAndroidApplication::sdkVersion() >= 31) {
184 success = (bool)QJniObject::callStaticMethod<jboolean>(
185 QtJniTypes::className<QtJniTypes::QtBtBroadcastReceiver>(),
188 success = (bool)d_ptr->adapter()->callMethod<jboolean>(
"enable");
191 qCWarning(QT_BT_ANDROID) <<
"Enabling bluetooth failed";
209 bool success =
false;
210 if (d_ptr->adapter()) {
211 if (QNativeInterface::QAndroidApplication::sdkVersion() >= 31) {
212 success = (bool)QJniObject::callStaticMethod<jboolean>(
213 QtJniTypes::className<QtJniTypes::QtBtBroadcastReceiver>(),
216 success = (bool)d_ptr->adapter()->callMethod<jboolean>(
"disable");
220 qCWarning(QT_BT_ANDROID) <<
"Unable to power off the adapter";
232 d_ptr->pendingConnectableHostModeTransition =
true;
234 const bool success = (bool)QJniObject::callStaticMethod<jboolean>(
235 QtJniTypes::className<QtJniTypes::QtBtBroadcastReceiver>(),
238 qCWarning(QT_BT_ANDROID) <<
"Unable to enable the Bluetooth";
247 qCWarning(QT_BT_ANDROID) <<
"Local device setHostMode() failed due to "
248 "missing permissions";
252 const bool success = (bool)QJniObject::callStaticMethod<jboolean>(
253 QtJniTypes::className<QtJniTypes::QtBtBroadcastReceiver>(),
256 qCWarning(QT_BT_ANDROID) <<
"Unable to set Bluetooth as discoverable";
262 qCWarning(QT_BT_ANDROID) <<
"setHostMode() unsupported host mode:" << nextMode;
269 if (d_ptr->adapter()) {
270 jint scanMode = d_ptr->adapter()->callMethod<jint>(
"getScanMode");
291 qCWarning(QT_BT_ANDROID) <<
"Local device allDevices() failed due to "
292 "missing permissions";
301 info.setName(
o.callMethod<jstring>(
"getName").toString());
322 if (previousPairing == newPairing) {
329 if (!d_ptr->adapter()) {
330 qCWarning(QT_BT_ANDROID) <<
"Unable to pair, invalid adapter";
338 jboolean success = QJniObject::callStaticMethod<jboolean>(
339 QtJniTypes::className<QtJniTypes::QtBtBroadcastReceiver>(),
341 inputString.object<jstring>(),
342 jboolean(newPairing ==
Paired ? JNI_TRUE : JNI_FALSE));
356 if (
address.isNull() || !d_ptr->adapter())
361 = d_ptr->adapter()->callMethod<QtJniTypes::BluetoothDevice>(
"getRemoteDevice",
362 inputString.object<jstring>());
364 if (!remoteDevice.isValid())
367 jint bondState = remoteDevice.callMethod<jint>(
"getBondState");
392 QtJniTypes::className<QtJniTypes::QtBtBroadcastReceiver>(),
"getConnectedDevices");
395 return d_ptr->connectedDevices;
397 jobjectArray connectedDevicesArray =
connectedDevices.object<jobjectArray>();
398 if (!connectedDevicesArray)
399 return d_ptr->connectedDevices;
405 jint
size = env->GetArrayLength(connectedDevicesArray);
406 for (
int i = 0;
i <
size;
i++) {
407 p = env->GetObjectArrayElement(connectedDevicesArray,
i);
413 return knownAddresses;
QJniObject getDefaultBluetoothAdapter()
QT_BEGIN_NAMESPACE bool ensureAndroidPermission(QBluetoothPermission::CommunicationModes modes)
void pairingStateChanged(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
void connectDeviceChanges(const QBluetoothAddress &address, bool isConnectEvent)
void hostModeStateChanged(QBluetoothLocalDevice::HostMode state)
~QBluetoothLocalDevicePrivate()
void powerOn()
Powers on the device after returning it to the hostMode() state, if it was powered off.
Pairing
This enum describes the pairing state between the two Bluetooth devices.
void errorOccurred(QBluetoothLocalDevice::Error error)
Signal emitted if there's an exceptional error while pairing.
void requestPairing(const QBluetoothAddress &address, Pairing pairing)
Set the pairing status with address.
HostMode
This enum describes the most of the local Bluetooth device.
@ HostDiscoverableLimitedInquiry
HostMode hostMode() const
Returns the current host mode of this local Bluetooth device.
void deviceConnected(const QBluetoothAddress &address)
Error
This enum describes errors that maybe returned.
@ MissingPermissionsError
QList< QBluetoothAddress > connectedDevices() const
QBluetoothLocalDevice(QObject *parent=nullptr)
Constructs a QBluetoothLocalDevice with parent.
QString name() const
Returns the name assgined by the user to this Bluetooth device.
static QList< QBluetoothHostInfo > allDevices()
Returns a list of all available local Bluetooth devices.
Pairing pairingStatus(const QBluetoothAddress &address) const
Returns the current bluetooth pairing status of address, if it's unpaired, paired,...
QBluetoothAddress address() const
Returns the MAC address of this Bluetooth device.
void deviceDisconnected(const QBluetoothAddress &address)
void pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
Pairing or unpairing has completed with address.
void setHostMode(QBluetoothLocalDevice::HostMode mode)
Sets the host mode of this local Bluetooth device to mode.
void hostModeStateChanged(QBluetoothLocalDevice::HostMode state)
The state of the host has transitioned to a different HostMode.
void append(parameter_type t)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Combined button and popup list for selecting options.
void registerQBluetoothLocalDeviceMetaType()
std::pair< T1, T2 > QPair
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define Q_ARG(Type, data)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLuint64EXT address
GLdouble GLdouble GLdouble GLdouble q
constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
QFileInfo info(fileName)
[8]
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
char * toString(const MyType &t)
[31]
bool contains(const AT &t) const noexcept
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent