| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2010 Rafael Fernández López <[email protected]> |
| 3 |
* SPDX-FileCopyrightText: 2010 UFO Coders <[email protected]> |
| 4 |
* SPDX-FileCopyrightText: 2014-2015 David Rosca <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "devicereceiver.h" |
| 10 |
|
| 11 |
#include <QCoreApplication> |
| 12 |
#include <QDebug> |
| 13 |
|
| 14 |
#include "adapter.h" |
| 15 |
#include "device.h" |
| 16 |
#include "initmanagerjob.h" |
| 17 |
#include "pendingcall.h" |
| 18 |
|
| 19 |
using namespace BluezQt; |
| 20 |
|
| 21 |
Manager *g_manager = nullptr; |
| 22 |
|
| 23 |
DeviceReceiver::DeviceReceiver(QObject *parent) |
| 24 |
: QObject(parent) |
| 25 |
{ |
| 26 |
} |
| 27 |
|
| 28 |
void DeviceReceiver::scanDevices() |
| 29 |
{ |
| 30 |
AdapterPtr usableAdapter = g_manager->usableAdapter(); |
| 31 |
if (!usableAdapter && g_manager->adapters().isEmpty()) { |
| 32 |
qDebug() << "!!! No bluetooth adapters were found. Waiting for bluetooth adapters. Ctrl + C to cancel..."; |
| 33 |
connect(g_manager, &Manager::adapterAdded, this, &DeviceReceiver::adapterAdded); |
| 34 |
return; |
| 35 |
} else if (!usableAdapter) { |
| 36 |
usableAdapter = g_manager->adapters().at(0); |
| 37 |
PendingCall *powerOnCall = usableAdapter->setPowered(true); |
| 38 |
powerOnCall->waitForFinished(); |
| 39 |
} |
| 40 |
|
| 41 |
qDebug() << "*** Will scan devices until stopped..."; |
| 42 |
qDebug(); |
| 43 |
|
| 44 |
connect(usableAdapter.data(), &Adapter::deviceAdded, this, &DeviceReceiver::deviceFound); |
| 45 |
connect(usableAdapter.data(), &Adapter::deviceChanged, this, &DeviceReceiver::devicePropertyChanged); |
| 46 |
|
| 47 |
usableAdapter->startDiscovery(); |
| 48 |
} |
| 49 |
|
| 50 |
void DeviceReceiver::deviceFound(const BluezQt::DevicePtr &device) |
| 51 |
{ |
| 52 |
qDebug() << "*** Remote device found:" << device->name() << "(" << device->address() << ")"; |
| 53 |
qDebug(); |
| 54 |
} |
| 55 |
|
| 56 |
void DeviceReceiver::devicePropertyChanged(const BluezQt::DevicePtr &device) |
| 57 |
{ |
| 58 |
qDebug() << "*** Device with address" << device->address() << "changed some property"; |
| 59 |
qDebug() << "\tAddress:\t" << device->address(); |
| 60 |
qDebug() << "\tClass:\t\t" << device->deviceClass(); |
| 61 |
qDebug() << "\tIcon:\t\t" << device->icon(); |
| 62 |
qDebug() << "\tLegacy Pairing:\t" << (device->hasLegacyPairing() ? "yes" : "no"); |
| 63 |
qDebug() << "\tName:\t\t" << device->name(); |
| 64 |
qDebug() << "\tPaired:\t\t" << (device->isPaired() ? "yes" : "no"); |
| 65 |
qDebug() << "\tTrusted:\t" << (device->isTrusted() ? "yes" : "no"); |
| 66 |
qDebug() << "\tServices:\n" << device->uuids(); |
| 67 |
qDebug(); |
| 68 |
} |
| 69 |
|
| 70 |
void DeviceReceiver::adapterAdded(const BluezQt::AdapterPtr &adapter) |
| 71 |
{ |
| 72 |
Q_UNUSED(adapter) |
| 73 |
|
| 74 |
qDebug() << "*** An adapter has been connected."; |
| 75 |
qDebug(); |
| 76 |
scanDevices(); |
| 77 |
} |
| 78 |
|
| 79 |
static void stopDiscovering() |
| 80 |
{ |
| 81 |
if (g_manager) { |
| 82 |
for (const AdapterPtr &adapter : g_manager->adapters()) { |
| 83 |
adapter->stopDiscovery(); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
int main(int argc, char **argv) |
| 89 |
{ |
| 90 |
QCoreApplication app(argc, argv); |
| 91 |
|
| 92 |
qAddPostRoutine(stopDiscovering); |
| 93 |
|
| 94 |
g_manager = new Manager(); |
| 95 |
InitManagerJob *initJob = g_manager->init(); |
| 96 |
initJob->exec(); |
| 97 |
|
| 98 |
if (initJob->error()) { |
| 99 |
qWarning() << "Error initializing manager:" << initJob->errorText(); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
DeviceReceiver *deviceReceiver = new DeviceReceiver(); |
| 104 |
deviceReceiver->scanDevices(); |
| 105 |
|
| 106 |
return app.exec(); |
| 107 |
} |
| 108 |
|
| 109 |
#include "moc_devicereceiver.cpp" |