4#include <QtCore/QLoggingCategory>
5#include <QtCore/QRandomGenerator>
6#include <QtDBus/QDBusContext>
26 d_ptr->currentMode = hostMode();
33 d_ptr->currentMode = hostMode();
39 return d_ptr->adapter->alias();
55 d_ptr->adapter->setPowered(
true);
65 if (
d->pendingHostModeChange != -1) {
66 qCWarning(QT_BT_BLUEZ) <<
"setHostMode() ignored due to already pending mode change";
77 d->adapter->setPowered(
true);
79 d->adapter->setDiscoverable(
true);
85 d->adapter->setPowered(
true);
87 d->adapter->setDiscoverable(
false);
91 d->adapter->setPowered(
false);
99 if (!d_ptr->adapter->powered())
101 else if (d_ptr->adapter->discoverable())
103 else if (d_ptr->adapter->powered())
112 return d_ptr->connectedDevices();
117 QList<QBluetoothHostInfo> localDevices;
122 QDBusPendingReply<ManagedObjectList>
reply =
manager.GetManagedObjects();
123 reply.waitForFinished();
128 for (ManagedObjectList::const_iterator
it = managedObjectList.
constBegin();
142 if (hostInfo.address().isNull())
144 hostInfo.setName(ifaceValues.value(
QStringLiteral(
"Name")).toString());
145 localDevices.append(hostInfo);
162 if (current_pairing == pairing) {
163 if (d_ptr->adapter) {
165 if (d_ptr->pairingDiscoveryTimer && d_ptr->pairingDiscoveryTimer->isActive()) {
166 d_ptr->pairingDiscoveryTimer->stop();
169 if (d_ptr->pairingTarget) {
170 qCDebug(QT_BT_BLUEZ) <<
"Cancelling pending pairing request to" << d_ptr->pairingTarget->address();
172 d_ptr->pairingRequestCanceled =
true;
173 cancelReply.waitForFinished();
174 delete d_ptr->pairingTarget;
175 d_ptr->pairingTarget =
nullptr;
194 if (pairingDiscoveryTimer && pairingDiscoveryTimer->isActive()) {
195 pairingDiscoveryTimer->stop();
200 delete pairingTarget;
201 pairingTarget =
nullptr;
208 QDBusPendingReply<ManagedObjectList>
reply =
manager->GetManagedObjects();
209 reply.waitForFinished();
210 if (
reply.isError()) {
216 for (ManagedObjectList::const_iterator
it = managedObjectList.
constBegin();
it != managedObjectList.
constEnd(); ++
it) {
221 const QString &iface = jt.key();
229 qCDebug(QT_BT_BLUEZ) <<
"Initiating direct pair to" << targetAddress.toString();
231 processPairing(
path.path(), targetPairing);
242 pairing = targetPairing;
243 if (!pairingDiscoveryTimer) {
244 pairingDiscoveryTimer =
new QTimer(
this);
245 pairingDiscoveryTimer->setSingleShot(
true);
246 pairingDiscoveryTimer->setInterval(20000);
248 this, &QBluetoothLocalDevicePrivate::pairingDiscoveryTimedOut);
251 qCDebug(QT_BT_BLUEZ) <<
"Initiating discovery for pairing on" << targetAddress.toString();
252 pairingDiscoveryTimer->start();
262void QBluetoothLocalDevicePrivate::processPairing(
const QString &objectPath,
266 delete pairingTarget;
269 if (pairingDiscoveryTimer && pairingDiscoveryTimer->isActive()) {
270 pairingDiscoveryTimer->stop();
283 delete pairingTarget;
284 pairingTarget =
nullptr;
304 if (!pairingTarget->paired()) {
305 qCDebug(QT_BT_BLUEZ) <<
"Sending pairing request to" << pairingTarget->address();
310 this, &QBluetoothLocalDevicePrivate::pairingCompleted);
316 pairingTarget->setTrusted(
true);
318 pairingTarget->setTrusted(
false);
320 delete pairingTarget;
321 pairingTarget =
nullptr;
331void QBluetoothLocalDevicePrivate::pairingDiscoveryTimedOut()
333 qCWarning(QT_BT_BLUEZ) <<
"Discovery for pairing purposes failed. Cannot find parable device.";
348 QDBusPendingReply<ManagedObjectList>
reply = d_ptr->manager->GetManagedObjects();
349 reply.waitForFinished();
354 for (ManagedObjectList::const_iterator
it = managedObjectList.
constBegin();
it != managedObjectList.
constEnd(); ++
it) {
386 pendingHostModeChange(-1),
393 connectDeviceChanges();
401void QBluetoothLocalDevicePrivate::connectDeviceChanges()
405 QDBusPendingReply<ManagedObjectList>
reply =
manager->GetManagedObjects();
406 reply.waitForFinished();
413 for (ManagedObjectList::const_iterator
it = managedObjectList.
constBegin();
it != managedObjectList.
constEnd(); ++
it) {
430 this, &QBluetoothLocalDevicePrivate::PropertiesChanged);
431 deviceChangeMonitors.insert(
path.path(), monitor);
435 connectedDevicesSet.insert(
address);
446 delete adapterProperties;
448 delete pairingTarget;
453void QBluetoothLocalDevicePrivate::initializeAdapter()
466 this, &QBluetoothLocalDevicePrivate::InterfacesAdded);
468 this, &QBluetoothLocalDevicePrivate::InterfacesRemoved);
475 deviceAdapterPath = adapterPath;
485 this, &QBluetoothLocalDevicePrivate::PropertiesChanged);
489void QBluetoothLocalDevicePrivate::PropertiesChanged(
const QString &
interface,
502 if (!adapter->powered()) {
505 if (adapter->discoverable())
510 if (pendingHostModeChange != -1) {
512 if (
static_cast<int>(
mode) != pendingHostModeChange) {
513 adapter->setDiscoverable(
514 pendingHostModeChange
516 pendingHostModeChange = -1;
519 pendingHostModeChange = -1;
523 if (
mode != currentMode)
532 qobject_cast<OrgFreedesktopDBusPropertiesInterface*>(
sender());
536 const QString currentPath = senderIface->path();
537 bool isConnected = changed_properties.value(
QStringLiteral(
"Connected"),
false).toBool();
541 bool isInSet = connectedDevicesSet.contains(changedAddress);
542 if (isConnected && !isInSet) {
543 connectedDevicesSet.insert(changedAddress);
545 }
else if (!isConnected && isInSet) {
546 connectedDevicesSet.remove(changedAddress);
554 if (interfaces_and_properties.contains(
QStringLiteral(
"org.bluez.Device1"))
555 && !deviceChangeMonitors.contains(object_path.path())) {
564 this, &QBluetoothLocalDevicePrivate::PropertiesChanged);
565 deviceChangeMonitors.insert(object_path.path(), monitor);
570 connectedDevicesSet.insert(
address);
576 if (pairingDiscoveryTimer && pairingDiscoveryTimer->isActive()
577 && interfaces_and_properties.contains(
QStringLiteral(
"org.bluez.Device1"))) {
582 processPairing(object_path.path(), pairing);
586void QBluetoothLocalDevicePrivate::InterfacesRemoved(
const QDBusObjectPath &object_path,
589 if (deviceChangeMonitors.contains(object_path.path())
590 && interfaces.contains(
QLatin1String(
"org.bluez.Device1"))) {
594 delete deviceChangeMonitors.take(object_path.path());
601 bool found = connectedDevicesSet.remove(
address);
607 if (adapter && object_path.path() == adapter->path()
608 && interfaces.contains(
QLatin1String(
"org.bluez.Adapter1"))) {
609 qCDebug(QT_BT_BLUEZ) <<
"Adapter" << adapter->path() <<
"was removed";
615 delete adapterProperties;
616 adapterProperties =
nullptr;
618 delete pairingTarget;
619 pairingTarget =
nullptr;
623 deviceChangeMonitors.clear();
624 connectedDevicesSet.clear();
633QList<QBluetoothAddress> QBluetoothLocalDevicePrivate::connectedDevices()
const
635 return connectedDevicesSet.values();
643 if (
reply.isError()) {
645 const bool canceledByUs =
647 && pairingRequestCanceled;
651 pairingRequestCanceled =
false;
656 pairingRequestCanceled =
false;
659 if (!pairingTarget) {
660 qCWarning(QT_BT_BLUEZ) <<
"Pairing target expected but found null pointer.";
666 if (!pairingTarget->paired()) {
667 qCWarning(QT_BT_BLUEZ) <<
"Device was not paired as requested";
676 pairingTarget->setTrusted(
true);
678 pairingTarget->setTrusted(
false);
680 delete pairingTarget;
681 pairingTarget =
nullptr;
683 emit q->pairingFinished(targetAddress, pairing);
691#include "moc_qbluetoothlocaldevice_p.cpp"
QString findAdapterForAddress(const QBluetoothAddress &wantedAddress, bool *ok=nullptr)
QMap< QDBusObjectPath, InterfaceList > ManagedObjectList
QT_BEGIN_NAMESPACE void initializeBluez5()
IOBluetoothDevice * device
void InterfacesAdded(const QDBusObjectPath &object_path, InterfaceList interfaces_and_properties)
void InterfacesRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces)
void PropertiesChanged(const QString &interface, const QVariantMap &changed_properties, const QStringList &invalidated_properties, const QDBusMessage &msg)
QBluetoothLocalDevicePrivate(QBluetoothLocalDevice *, const QBluetoothAddress &=QBluetoothAddress())
~QBluetoothLocalDevicePrivate()
void requestPairing(const QBluetoothAddress &address, Pairing pairing)
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.
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 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.
static QDBusConnection systemBus()
Returns a QDBusConnection object opened with the system bus.
void finished(QDBusPendingCallWatcher *self=nullptr)
This signal is emitted when the pending call has finished and its reply is available.
T value(const Key &key, const T &defaultValue=T()) const
NetworkError error() const
Returns the error that was found during the processing of this request.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
void deleteLater()
\threadsafe
const_iterator constBegin() const noexcept
const_iterator constEnd() const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
QString & replace(qsizetype i, qsizetype len, QChar after)
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
QString right(qsizetype n) const &
void timeout(QPrivateSignal)
This signal is emitted when the timer times out.
static QtBluezDiscoveryManager * instance()
qDeleteAll(list.begin(), list.end())
QSet< QString >::iterator it
Combined button and popup list for selecting options.
constexpr QBindableInterface iface
bool objectPathIsForThisDevice(const QString &adapterPath, const QString &objectPath)
QT_BEGIN_NAMESPACE void registerQBluetoothLocalDeviceMetaType()
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char * interface
#define qCWarning(category,...)
#define qCDebug(category,...)
#define Q_DECLARE_LOGGING_CATEGORY(name)
#define Q_ARG(Type, data)
GLuint GLuint64EXT address
GLdouble GLdouble GLdouble GLdouble q
GLsizei const GLchar *const * path
#define QStringLiteral(str)
QFutureWatcher< int > watcher
QNetworkAccessManager manager
char * toString(const MyType &t)
[31]
QT_BEGIN_NAMESPACE bool toBool(const QString &str)