|
libbluedevil v1.8
|
The Manager task is to serve as entry point to the library, being a singleton class. It will also inform about the service state (operational or not), provide the default adapter connected to the system, as well as notify of adapters being connected or disconnected.
For brevity, we will directly include the whole namespace:
using namespace BlueDevil;
In order to include the Manager API, you have to perform:
#include <bluedevil/bluedevilmanager.h>
So, all the dance usually starts as:
Manager *const manager = Manager::self(); // If Bluetooth is operational, we can directly retrieve the default adapter, and start working // with it. Otherwise, we can connect to the defaultAdapterChanged signal, so we will be notified // when we have an adapter ready to be used. if (manager->isBluetoothOperational()) { Adapter *const defaultAdapter = manager->defaultAdapter(); // Do something interesting with the adapter... } else { connect(manager, SIGNAL(defaultAdapterChanged(Adapter*)), this, SLOT(defaultAdapterChanged(Adapter*))); }
It is very common that in some few calls we will be using the Manager, Adapter and Device APIs. In order to decrease the number of includes that you need to do, you have a handy trick:
#include <bluedevil/bluedevil.h>
An adapter is a device physically connected to the system. Its main job is to let other devices discover it as well as discover remote devices.
As it has been described before, you can have access to the default adapter designated by the system by asking it to the manager. We can perform a very typical task as discover remote devices.
Adapter *const adapter = Manager::self()->defaultAdapter(); connect(adapter, SIGNAL(deviceFound(Device*)), this, SLOT(deviceFound(Device*))); adapter->startDiscovery(); QTimer::singleShot(10000, adapter, SLOT(stopDiscovery())));
This snippet will discover devices for 10 seconds. For each device discovered, the slot deviceFound() will be called.
The Adapter API also allows you to set up other Adapter settings such as if the adapter is powered or not, its visibility, the visibility timeout...
This class represents a remote device. This class basically retrieves information, but it is also possible to set certain properties for this device, such as whether this device is trusted or not, or blocked, or the local alias for this device.
We can have a look at the slot deviceFound() that we named early before on the Adapter example:
void MyClass::deviceFound(Device *device) { qDebug() << "Device found: " << device->name() << " (" << device->address() << ")"; qDebug() << "\tServices: " << device->UUIDs(); }
The UUIDs are the services supported by this device, so for each found device, we ask (and print) its name, its hardware address (MAC) and the supported services by this device.
1.7.3