QBluetoothLeUartClient class

The QBluetoothLeUartClient class can be used to talk to BluetoothLE devices via UART effordlessly. It can be used via C++ and QML.

C++ example:

#include <qbluetoothleuartclient.h>
class MyBluetoothLeClass : QObject {
public:
    MyBluetoothLeClass(QObject* parent = nullptr) : QObject(parent) {
        this->ble = new QBluetoothLeUartClient();

        connect(this->ble, &QBluetoothLeUartClient::foundNewDevice, this, &MyBluetoothLeClass::handleFoundNewDevice);
        connect(this->ble, &QBluetoothLeUartClient::connectedToDevice, this, &MyBluetoothLeClass::handleBluetoothDeviceConected);
        connect(this->ble, &QBluetoothLeUartClient::dataReceived, this, &MyBluetoothLeClass::handleDataReceived);

        this->ble->startScanningForDevices();
    }

private:
    QBluetoothLeUartClient *ble;

private slots:
    void handleFoundNewDevice(QBluetoothLeUartDevice* device) {
        qDebug() << "Found a device: name: " << device->getName() << " address: " << device->getAddress();

        if(device->getName() == "My device name"){
            this->ble->stopScanningForDevices();
            this->ble->connectToDevice(device);
        }
    }

    void handleBluetoothDeviceConected()  {
        this->ble->sendData("This is my test message");
    }

    void handleDataReceived(const QString &s) {
        qDebug() << "Data received: " << s;
    }
};

QML example:

import de.itsblue.bluetoothleuart 1.0

QBluetoothLeUartClient {
    id: ble
    Component.onCompleted: {
        ble.startScanningForDevices()
    }

    onFoundNewDevice: {
        console.log("Found a device: name: " + device.name + " address: " + device.address)
        if(device.name === "My device name") {
            ble.stopScanningForDevices()
            ble.connectToDevice(device)
       }
    }

    onConnectedToDevice: {
        ble.sendData("This is my test message")
    }

    onDataReceived: {
        console.log("Data received: " + data)
    }
}

To get all available devices in a model, use the QBluetoothLeUartDeviceModel.

Public types

enum BluetoothLeUartClientState { Idle = 0, AdapterTurnedOff, LocationPermissionDenied, Scanning, ScanFinished, Connecting, ScanningForService, ServiceFound, Connected }
The BluetoothLeUartState enum contains all state of the QBluetoothLeUart class.

Public static functions

static void init()
Function to register QMl types.

Public functions

void setUUIDs(const char uartServiceUUID[36], const char txUUID[36], const char rxUUID[36])
Function to set the UUIDs of the bluetooth service.

Public slots

auto startScanningForDevices() -> Q_INVOKABLE bool
Fuction to start scanning for devices.
auto stopScanningForDevices() -> Q_INVOKABLE bool
Function to stop scanning for devices.
auto getAvailableDevices() -> Q_INVOKABLE QList<QBluetoothLeUartDevice*>
Function to get all devices that were found during the last scan.
auto getAvailableDevicesDetailList() -> Q_INVOKABLE QVariantList
Function to get a variant list of all devices that were found during the last scan.
auto getAvailableDevicesModel() -> Q_INVOKABLE QBluetoothLeUartDeviceModel*
Function to get a QBluetoothLeUartDeviceModel with all available devices.
auto getCurrentDevice() -> Q_INVOKABLE QBluetoothLeUartDevice*
Function to get the currently connected device.
auto connectToDevice(int deviceId) -> Q_INVOKABLE bool
Function connect to a device using its internal id.
auto connectToDevice(QBluetoothLeUartDevice* device) -> Q_INVOKABLE bool
Function connect to a device using a QBluetoothLeUartDevice object.
auto disconnectFromDevice() -> Q_INVOKABLE bool
Function to disconnect from the current device.
auto sendData(QString data, bool asynchronous = true) -> Q_INVOKABLE bool
Function to send data to the connected device.
auto getState() const -> Q_INVOKABLE QBluetoothLeUartClient::BluetoothLeUartClientState
Function to get the current state of QBluetoothLeUart.

Enum documentation

enum QBluetoothLeUartClient::BluetoothLeUartClientState

The BluetoothLeUartState enum contains all state of the QBluetoothLeUart class.

Enumerators
Idle

Waiting for instrucions

AdapterTurnedOff

The bluetooth adapter is turned off

LocationPermissionDenied

The location permssion was denied and we are therfor unable to scan!

Scanning

Scanning for devices

ScanFinished

Scanning has finished, we are ready to connect

Connecting

Trying to connect

ScanningForService

Connection was successfull, now scanning for services

ServiceFound

Services were found

Connected

Connected. We are now ready to send and receive

Function documentation

void QBluetoothLeUartClient::setUUIDs(const char uartServiceUUID[36], const char txUUID[36], const char rxUUID[36])

Function to set the UUIDs of the bluetooth service.

Parameters
uartServiceUUID Service UUID
txUUID UUID of the characteristic used to send data
rxUUID UUID of the characteristic used to receive data

Q_INVOKABLE bool QBluetoothLeUartClient::startScanningForDevices() public slot

Fuction to start scanning for devices.

Returns true if the scan started, false if the current state was not Idle

This function will start the device scanning process and might emit the following signals during scanning:

  • foundNewDevice() when a new device is found
  • avaliableDevicesChanged() when a new device is found
  • scanFinished() when the scan has finished
  • scanningErrorOccured() when an error occured

Q_INVOKABLE bool QBluetoothLeUartClient::stopScanningForDevices() public slot

Function to stop scanning for devices.

Returns true if the scan was stopped, false if the current state was not Scanning

Q_INVOKABLE QList<QBluetoothLeUartDevice*> QBluetoothLeUartClient::getAvailableDevices() public slot

Function to get all devices that were found during the last scan.

Returns List of all devices found during last scan

A QBluetoothLeUartDevice object can be used to connect to the specific device

Q_INVOKABLE QVariantList QBluetoothLeUartClient::getAvailableDevicesDetailList() public slot

Function to get a variant list of all devices that were found during the last scan.

Returns Variant list of all devices found during last scan

This will return a QVariantList that contains QVariantMaps. The maps contain the following keys:

  • "id" (int): the internal id of the device (used to connect to it)
  • "name" (QString): the name of the device
  • "address" (QString): the bluetooth address of the device

Q_INVOKABLE QBluetoothLeUartDeviceModel* QBluetoothLeUartClient::getAvailableDevicesModel() public slot

Function to get a QBluetoothLeUartDeviceModel with all available devices.

Returns A QBluetoothLeUartDeviceModel with all available devices

Q_INVOKABLE QBluetoothLeUartDevice* QBluetoothLeUartClient::getCurrentDevice() public slot

Function to get the currently connected device.

Returns The currently connected device or nullptr if no device is connected

Q_INVOKABLE bool QBluetoothLeUartClient::connectToDevice(int deviceId) public slot

Function connect to a device using its internal id.

Parameters
deviceId the internal id of the device
Returns false if the device was not found in the internal list of discovered devices, true otherwise

The id can be found using getAvailableDevicesDetailList() The connectedToDevice() signal will be emited as soon as the connection was successfull. As soon as that signal was emited, the sendData() slot can be used to send data and the dataReceived() signal will be emited whenever data is received.

Q_INVOKABLE bool QBluetoothLeUartClient::connectToDevice(QBluetoothLeUartDevice* device) public slot

Function connect to a device using a QBluetoothLeUartDevice object.

Parameters
device The device to connect to
Returns false if the device was not found in the internal list of discovered devices, true otherwise

The QBluetoothLeUartDevice can be found using getAvailableDevices() The connectedToDevice() signal will be emited as soon as the connection was successfull. As soon as that signal was emited, the sendData() slot can be used to send data and the dataReceived() signal will be emited whenever data is received.

Q_INVOKABLE bool QBluetoothLeUartClient::disconnectFromDevice() public slot

Function to disconnect from the current device.

Returns false if no device was connected, true otherwise

Q_INVOKABLE bool QBluetoothLeUartClient::sendData(QString data, bool asynchronous = true) public slot

Function to send data to the connected device.

Parameters
data The data to send
asynchronous
Returns false if there was not device connected, true otherwise

Q_INVOKABLE QBluetoothLeUartClient::BluetoothLeUartClientState QBluetoothLeUartClient::getState() const public slot

Function to get the current state of QBluetoothLeUart.

Returns The current state