QLowEnergyController Class
The QLowEnergyController class provides access to Bluetooth Low Energy Devices. More...
| Header: | #include <QLowEnergyController> |
| qmake: | QT += bluetooth |
| Since: | Qt 5.4 |
| Inherits: | QObject |
This class was introduced in Qt 5.4.
Public Types
| enum | ControllerState { UnconnectedState, ConnectingState, ConnectedState, DiscoveringState, DiscoveredState, …, AdvertisingState } |
| enum | Error { NoError, UnknownError, UnknownRemoteDeviceError, NetworkError, InvalidBluetoothAdapterError, …, AuthorizationError } |
| enum | RemoteAddressType { PublicAddress, RandomAddress } |
| enum | Role { CentralRole, PeripheralRole } |
Detailed Description
QLowEnergyController acts as the entry point for Bluetooth Low Energy development.
Bluetooth Low Energy defines two types of devices; the peripheral and the central. Each role performs a different task. The peripheral device provides data which is utilized by central devices. An example might be a humidity sensor which measures the moisture in a winter garden. A device such as a mobile phone might read the sensor's value and display it to the user in the greater context of all sensors in the same environment. In this case the sensor is the peripheral device and the mobile phone acts as the central device.
A controller in the central role is created via the createCentral() factory method. Such an object essentially acts as a placeholder towards a remote Low Energy peripheral device, enabling features such as service discovery and state tracking.
After having created a controller object in the central role, the first step is to establish a connection via connectToDevice(). Once the connection has been established, the controller's state() changes to QLowEnergyController::ConnectedState and the connected() signal is emitted. It is important to mention that some platforms such as a BlueZ based Linux cannot maintain two connected instances of QLowEnergyController to the same remote device. In such cases the second call to connectToDevice() may fail. This limitation may disappear at some stage in the future. The disconnectFromDevice() function is used to break the existing connection.
The second step after establishing the connection is to discover the services offered by the remote peripheral device. This process is started via discoverServices() and has finished once the discoveryFinished() signal has been emitted. The discovered services can be enumerated via services().
The last step is to create service objects. The createServiceObject() function acts as factory for each service object and expects the service UUID as parameter. The calling context should take ownership of the returned QLowEnergyService instance.
Any QLowEnergyService, QLowEnergyCharacteristic or QLowEnergyDescriptor instance which is later created from this controller's connection becomes invalid as soon as the controller disconnects from the remote Bluetooth Low Energy device.
A controller in the peripheral role is created via the createPeripheral() factory method. Such an object acts as a peripheral device itself, enabling features such as advertising services and allowing clients to get notified about changes to characteristic values.
After having created a controller object in the peripheral role, the first step is to populate the set of GATT services offered to client devices via calls to addService(). Afterwards, one would call startAdvertising() to let the device broadcast some data and, depending on the type of advertising being done, also listen for incoming connections from GATT clients.
See also QLowEnergyService, QLowEnergyCharacteristic, QLowEnergyDescriptor, QLowEnergyAdvertisingParameters, and QLowEnergyAdvertisingData.
Member Type Documentation
enum QLowEnergyController::ControllerState
Indicates the state of the controller object.
| Constant | Value | Description |
|---|---|---|
QLowEnergyController::UnconnectedState | 0 | The controller is not connected to a remote device. |
QLowEnergyController::ConnectingState | 1 | The controller is attempting to connect to a remote device. |
QLowEnergyController::ConnectedState | 2 | The controller is connected to a remote device. |
QLowEnergyController::DiscoveringState | 3 | The controller is retrieving the list of services offered by the remote device. |
QLowEnergyController::DiscoveredState | 4 | The controller has discovered all services offered by the remote device. |
QLowEnergyController::ClosingState | 5 | The controller is about to be disconnected from the remote device. |
QLowEnergyController::AdvertisingState | 6 | The controller is currently advertising data. This value was introduced by Qt 5.7. |
enum QLowEnergyController::Error
Indicates all possible error conditions found during the controller's existence.
| Constant | Value | Description |
|---|---|---|
QLowEnergyController::NoError | 0 | No error has occurred. |
QLowEnergyController::UnknownError | 1 | An unknown error has occurred. |
QLowEnergyController::UnknownRemoteDeviceError | 2 | The remote Bluetooth Low Energy device with the address passed to the constructor of this class cannot be found. |
QLowEnergyController::NetworkError | 3 | The attempt to read from or write to the remote device failed. |
QLowEnergyController::InvalidBluetoothAdapterError | 4 | The local Bluetooth device with the address passed to the constructor of this class cannot be found or there is no local Bluetooth device. |
QLowEnergyController::ConnectionError | 5 | The attempt to connect to the remote device failed. This value was introduced by Qt 5.5. |
QLowEnergyController::AdvertisingError | 6 | The attempt to start advertising failed. This value was introduced by Qt 5.7. |
QLowEnergyController::RemoteHostClosedError | 7 | The remote device closed the connection. This value was introduced by Qt 5.10. |
QLowEnergyController::AuthorizationError | 8 | The local Bluetooth device closed the connection due to insufficient authorization. This value was introduced by Qt 5.14. |
enum QLowEnergyController::RemoteAddressType
Indicates what type of Bluetooth address the remote device uses.
| Constant | Value | Description |
|---|---|---|
QLowEnergyController::PublicAddress | 0 | The remote device uses a public Bluetooth address. |
QLowEnergyController::RandomAddress | 1 | A random address is a Bluetooth Low Energy security feature. Peripherals using such addresses may frequently change their Bluetooth address. This information is needed when trying to connect to a peripheral. |
enum QLowEnergyController::Role
Indicates the role of the controller object.
| Constant | Value | Description |
|---|---|---|
QLowEnergyController::CentralRole | 0 | The controller acts as a client interacting with a remote device which is in the peripheral role. The controller can initiate connections, discover services and read and write characteristics. |
QLowEnergyController::PeripheralRole | 1 | The controller can be used to advertise services and handle incoming connections and client requests, acting as a GATT server. A remote device connected to the controller is in the central role. |
Note: The peripheral role is not supported on Windows. In addition on Linux, handling the "Signed Write" ATT command on the server side requires BlueZ 5 and kernel version 3.7 or newer.
This enum was introduced or modified in Qt 5.7.
See also QLowEnergyController::createCentral() and QLowEnergyController::createPeripheral().