added some documentation

This commit is contained in:
Dorian Zedler 2020-10-09 12:06:33 +02:00
parent 049951ebed
commit 7e7a8612c0
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
6 changed files with 2836 additions and 132 deletions

4
docs/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/html/
/latex/
/xml/
/m.css/

2494
docs/Doxyfile Normal file

File diff suppressed because it is too large Load diff

18
docs/Doxyfile-mcss Normal file
View file

@ -0,0 +1,18 @@
@INCLUDE = Doxyfile
GENERATE_HTML = NO
GENERATE_XML = YES
XML_PROGRAMLISTING = NO
SHOW_INCLUDE_FILES = YES
HTML_EXTRA_STYLESHEET = \
https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,600i%7CSource+Code+Pro:400,400i,600 \
../css/m-dark+documentation.compiled.css
M_THEME_COLOR = #0094ff
#M_FAVICON = favicon.png
# navbar
M_LINKS_NAVBAR1 =
M_LINKS_NAVBAR2 = "<a href=\"index.html\">Introduction</a>" \
annotated \
"<a href=\"ScStwSharedLibraries.pdf\">Download</a>"

View file

@ -10,11 +10,9 @@ QBluetoothLeUart::QBluetoothLeUart(QObject *parent) : QObject(parent)
this->setUUIDs("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e"); this->setUUIDs("6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e");
/* 1 Step: Bluetooth LE Device Discovery */ // init device discovery agent for scanning
this->bluetoothDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); this->bluetoothDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
/* Device Discovery Initialization */
connect(this->bluetoothDeviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &QBluetoothLeUart::handleDeviceDiscovered); connect(this->bluetoothDeviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &QBluetoothLeUart::handleDeviceDiscovered);
connect(bluetoothDeviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), connect(bluetoothDeviceDiscoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
this, SLOT(handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error))); this, SLOT(handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
@ -25,60 +23,39 @@ QBluetoothLeUart::~QBluetoothLeUart(){
} }
void QBluetoothLeUart::init() { // ------------------------------
#ifdef QBluetoothLeUart_QML // - Slots for QBluetoothLeUart -
qmlRegisterUncreatableType<QBluetoothLeUartDevice>("de.itsblue.bluetoothleuart", 1, 0, "QBluetoothDeviceInfo", "BluetoothDeviceInfo cannot be created"); // ------------------------------
qmlRegisterType<QBluetoothLeUart>("de.itsblue.bluetoothleuart", 1, 0, "QBluetoothLeUART");
qRegisterMetaType<QBluetoothLeUart::BluetoothLeUartState>("QBluetoothLeUart::BluetoothLeUartState");
qRegisterMetaType<QBluetoothLeUart::BluetoothScanError>("QBluetoothLeUart::BluetoothScanError");
#endif
}
void QBluetoothLeUart::startScanningForDevices(){ bool QBluetoothLeUart::startScanningForDevices(){
if(this->state != Idle)
return false;
foreach(QBluetoothLeUartDevice* oldDevice, this->availableDevices) foreach(QBluetoothLeUartDevice* oldDevice, this->availableDevices)
oldDevice->deleteLater(); oldDevice->deleteLater();
this->availableDevices.clear(); this->availableDevices.clear();
setState(Scanning); this->setState(Scanning);
bluetoothDeviceDiscoveryAgent->start(); this->bluetoothDeviceDiscoveryAgent->start();
qDebug()<< "Searching for low energy devices..." ; return true;
} }
void QBluetoothLeUart::stopScanningForDevices() { bool QBluetoothLeUart::stopScanningForDevices() {
if(this->state != Scanning)
return false;
this->bluetoothDeviceDiscoveryAgent->stop(); this->bluetoothDeviceDiscoveryAgent->stop();
this->setState(ScanFinished); this->setState(ScanFinished);
} return true;
void QBluetoothLeUart::handleDeviceDiscovered(const QBluetoothDeviceInfo &device)
{
// Is it a BLE device?
if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
//qWarning() << "Discovered BLE Device: name: " << device.name() << " Address: " << device.address().toString();
QBluetoothLeUartDevice *dev = new QBluetoothLeUartDevice(device, this);
availableDevices.append(dev);
emit this->foundNewDevice(dev);
emit this->avaliableDevicesChanged(this->availableDevices);
}
}
void QBluetoothLeUart::handleScanFinished()
{
if (availableDevices.size() == 0)
{
qWarning() << "No Low Energy devices found" << endl;
}
setState(ScanFinished);
} }
QList<QBluetoothLeUartDevice*> QBluetoothLeUart::getAvailableDevices() { QList<QBluetoothLeUartDevice*> QBluetoothLeUart::getAvailableDevices() {
return this->availableDevices; return this->availableDevices;
} }
QVariantList QBluetoothLeUart::getAvailableDevicesQML() { QVariantList QBluetoothLeUart::getAvailableDevicesDetailList() {
QVariantList result; QVariantList result;
for(int i=0; i < this->availableDevices.length(); i++) { for(int i=0; i < this->availableDevices.length(); i++) {
@ -97,28 +74,17 @@ QVariantList QBluetoothLeUart::getAvailableDevicesQML() {
return result; return result;
} }
void QBluetoothLeUart::handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error error) bool QBluetoothLeUart::connectToDevice(int deviceId) {
{
if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
emit this->scanningErrorOccured(AdapterTurnedOffError);
else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
emit this->scanningErrorOccured(InputOutputError);
else
emit this->scanningErrorOccured(UnknownError);
}
void QBluetoothLeUart::connectToDevice(int deviceId) {
if(deviceId < 0 || deviceId >= this->availableDevices.length()) if(deviceId < 0 || deviceId >= this->availableDevices.length())
return; return false;
this->connectToDevice(this->availableDevices[deviceId]); this->connectToDevice(this->availableDevices[deviceId]);
return true;
} }
void QBluetoothLeUart::connectToDevice(QBluetoothLeUartDevice *device){ bool QBluetoothLeUart::connectToDevice(QBluetoothLeUartDevice *device){
if(!this->availableDevices.contains(device))
m_qvMeasurements.clear(); return false;
this->currentBluetoothDevice = device; this->currentBluetoothDevice = device;
@ -128,7 +94,7 @@ void QBluetoothLeUart::connectToDevice(QBluetoothLeUartDevice *device){
bluetoothController = 0; bluetoothController = 0;
} }
/* 2 Step: QLowEnergyController */ // initialize QLowEnergyController
bluetoothController = new QLowEnergyController(currentBluetoothDevice->getDevice(), this); bluetoothController = new QLowEnergyController(currentBluetoothDevice->getDevice(), this);
bluetoothController ->setRemoteAddressType(QLowEnergyController::RandomAddress); bluetoothController ->setRemoteAddressType(QLowEnergyController::RandomAddress);
@ -142,9 +108,14 @@ void QBluetoothLeUart::connectToDevice(QBluetoothLeUartDevice *device){
/* Start connecting to device */ /* Start connecting to device */
bluetoothController->connectToDevice(); bluetoothController->connectToDevice();
setState(Connecting); setState(Connecting);
return true;
} }
void QBluetoothLeUart::disconnectFromDevice() { bool QBluetoothLeUart::disconnectFromDevice() {
if(this->state < ScanningForService)
return false;
this->bluetoothController->disconnectFromDevice(); this->bluetoothController->disconnectFromDevice();
disconnect(this->bluetoothController, &QLowEnergyController::serviceDiscovered, this, &QBluetoothLeUart::handleServiceDiscovered); disconnect(this->bluetoothController, &QLowEnergyController::serviceDiscovered, this, &QBluetoothLeUart::handleServiceDiscovered);
@ -155,33 +126,74 @@ void QBluetoothLeUart::disconnectFromDevice() {
disconnect(this->bluetoothController, &QLowEnergyController::disconnected, this, &QBluetoothLeUart::handleDeviceDisconnected); disconnect(this->bluetoothController, &QLowEnergyController::disconnected, this, &QBluetoothLeUart::handleDeviceDisconnected);
this->bluetoothController->deleteLater(); this->bluetoothController->deleteLater();
this->bluetoothController = nullptr;
this->bluetoothService->deleteLater();
this->bluetoothService = nullptr;
return true;
} }
void QBluetoothLeUart::handleDeviceDisconnected() bool QBluetoothLeUart::sendData(QString data){
if(this->state != Connected)
return false;
const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->txUUID)));
QByteArray Data;
Data.append(data);
bluetoothService->writeCharacteristic(RxChar, Data,QLowEnergyService::WriteWithoutResponse);
return true;
}
// -------------------------------------------
// - Slots for QBluetothDeviceDiscoveryAgent -
// -------------------------------------------
void QBluetoothLeUart::handleDeviceDiscovered(const QBluetoothDeviceInfo &device)
{ {
this->setState(Idle); // Is it a BLE device?
qDebug() << "UART service disconnected"; if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
qWarning() << "Remote device disconnected"; //qWarning() << "Discovered BLE Device: name: " << device.name() << " Address: " << device.address().toString();
QBluetoothLeUartDevice *dev = new QBluetoothLeUartDevice(device, this);
availableDevices.append(dev);
emit this->foundNewDevice(dev);
emit this->avaliableDevicesChanged(this->availableDevices);
}
} }
void QBluetoothLeUart::handleDeviceConnected() void QBluetoothLeUart::handleScanFinished()
{ {
qDebug() << "Device connected"; if (this->availableDevices.size() == 0)
bluetoothController->discoverServices();
setState(Connected);
}
void QBluetoothLeUart::handleControllerError(QLowEnergyController::Error error)
{ {
qDebug() << "Cannot connect to remote device."; qWarning() << "No Low Energy devices found" << endl;
qWarning() << "Controller Error:" << error;
} }
void QBluetoothLeUart::handleServiceDiscovered(const QBluetoothUuid &gatt){ emit this->scanFinished(this->availableDevices);
qDebug() << "Found service with ID: " << gatt; setState(ScanFinished);
}
if(gatt==QBluetoothUuid(QUuid(this->uartServiceUUID))){ void QBluetoothLeUart::handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
{
if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
emit this->scanningErrorOccured(AdapterTurnedOffError);
else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
emit this->scanningErrorOccured(InputOutputError);
else
emit this->scanningErrorOccured(UnknownError);
}
// ----------------------------------
// - Slots for QLowEnergyController -
// ----------------------------------
void QBluetoothLeUart::handleServiceDiscovered(const QBluetoothUuid &uuid){
qDebug() << "Found service with ID: " << uuid;
if(uuid == QBluetoothUuid(QUuid(this->uartServiceUUID))){
foundValidUARTService =true; foundValidUARTService =true;
qDebug() << "UART service found!"; qDebug() << "UART service found!";
} }
@ -212,7 +224,30 @@ void QBluetoothLeUart::handleServiceScanDone(){
setState(ServiceFound); setState(ServiceFound);
} }
/* Slots for QLowEnergyService */ void QBluetoothLeUart::handleControllerError(QLowEnergyController::Error error)
{
qDebug() << "Cannot connect to remote device.";
qWarning() << "Controller Error:" << error;
}
void QBluetoothLeUart::handleDeviceConnected()
{
qDebug() << "Device connected";
bluetoothController->discoverServices();
setState(ScanningForService);
}
void QBluetoothLeUart::handleDeviceDisconnected()
{
this->setState(Idle);
qDebug() << "UART service disconnected";
qWarning() << "Remote device disconnected";
}
// -------------------------------
// - Slots for QLowEnergyService -
// -------------------------------
void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState s) void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState s)
{ {
@ -221,18 +256,18 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState
case QLowEnergyService::ServiceDiscovered: case QLowEnergyService::ServiceDiscovered:
{ {
//looking for the TX characteristic //looking for the RX characteristic
const QLowEnergyCharacteristic TxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->txUUID))); const QLowEnergyCharacteristic TxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->rxUUID)));
if (!TxChar.isValid()){ if (!TxChar.isValid()){
qDebug() << "Tx characteristic not found"; qDebug() << "Rx characteristic not found";
this->disconnectFromDevice(); this->disconnectFromDevice();
return; return;
} }
//looking for the RX characteristic //looking for the TX characteristic
const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->rxUUID))); const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->txUUID)));
if (!RxChar.isValid()) { if (!RxChar.isValid()) {
qDebug() << "Rx characteristic not found"; qDebug() << "Tx characteristic not found";
this->disconnectFromDevice(); this->disconnectFromDevice();
return; return;
} }
@ -246,7 +281,7 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState
if (m_notificationDescTx.isValid()) { if (m_notificationDescTx.isValid()) {
// enable notification // enable notification
bluetoothService->writeDescriptor(m_notificationDescTx, QByteArray::fromHex("0100")); bluetoothService->writeDescriptor(m_notificationDescTx, QByteArray::fromHex("0100"));
setState(AcquireData); setState(Connected);
emit this->connectedToDevice(); emit this->connectedToDevice();
} }
@ -261,7 +296,7 @@ void QBluetoothLeUart::handleServiceStateChange(QLowEnergyService::ServiceState
void QBluetoothLeUart::handleServiceCharacteristicChange(const QLowEnergyCharacteristic &c,const QByteArray &value) void QBluetoothLeUart::handleServiceCharacteristicChange(const QLowEnergyCharacteristic &c,const QByteArray &value)
{ {
// ignore any other characteristic change // ignore any other characteristic change
if (c.uuid() != QBluetoothUuid(QUuid(this->txUUID))) if (c.uuid() != QBluetoothUuid(QUuid(this->rxUUID)))
return; return;
emit dataReceived((QString) value); emit dataReceived((QString) value);
@ -272,26 +307,23 @@ void QBluetoothLeUart::handleServiceDescriptorWritten(const QLowEnergyDescriptor
{ {
if (d.isValid() && d == bluetoothTransmissionDescriptor && value == QByteArray("0000")) { if (d.isValid() && d == bluetoothTransmissionDescriptor && value == QByteArray("0000")) {
//disabled notifications -> assume disconnect intent //disabled notifications -> assume disconnect intent
bluetoothController->disconnectFromDevice(); this->disconnectFromDevice();
delete bluetoothService;
bluetoothService = 0;
} }
} }
// --------------------
// - Helper functions -
// --------------------
void QBluetoothLeUart::sendData(QString s){ void QBluetoothLeUart::init() {
#ifdef QBluetoothLeUart_QML
const QLowEnergyCharacteristic RxChar = bluetoothService->characteristic(QBluetoothUuid(QUuid(this->rxUUID))); qmlRegisterUncreatableType<QBluetoothLeUartDevice>("de.itsblue.bluetoothleuart", 1, 0, "QBluetoothLeUartDevice", "BluetoothDeviceInfo cannot be created");
qmlRegisterType<QBluetoothLeUart>("de.itsblue.bluetoothleuart", 1, 0, "QBluetoothLeUart");
qDebug()<< s; qRegisterMetaType<QBluetoothLeUart::BluetoothLeUartState>("QBluetoothLeUart::BluetoothLeUartState");
QByteArray Data; qRegisterMetaType<QBluetoothLeUart::BluetoothScanError>("QBluetoothLeUart::BluetoothScanError");
Data.append(s); #endif
bluetoothService->writeCharacteristic(RxChar, Data,QLowEnergyService::WriteWithoutResponse);
} }
void QBluetoothLeUart::setState(QBluetoothLeUart::BluetoothLeUartState newState) void QBluetoothLeUart::setState(QBluetoothLeUart::BluetoothLeUartState newState)
{ {
if (state == newState) if (state == newState)
@ -305,8 +337,8 @@ QBluetoothLeUart::BluetoothLeUartState QBluetoothLeUart::getState() const {
return state; return state;
} }
void QBluetoothLeUart::setUUIDs(const char uartServiceUUID[36], const char rxUUID[36], const char txUUID[36]) { void QBluetoothLeUart::setUUIDs(const char uartServiceUUID[36], const char txUUID[36], const char rxUUID[36]) {
this->uartServiceUUID = uartServiceUUID; this->uartServiceUUID = uartServiceUUID;
this->rxUUID = rxUUID;
this->txUUID = txUUID; this->txUUID = txUUID;
this->rxUUID = rxUUID;
} }

View file

@ -12,21 +12,52 @@
#include <qbluetoothleuartdevice.h> #include <qbluetoothleuartdevice.h>
/*!
* \mainpage Qt BluetoothLE UART library
*
* \section intro_sec Introduction
*
* This library can be used to talk to BLE devices via UART in Qt.
* It was designed to make taklking to device like the ESP32 from Qt easier.
*
* \section section Installation
* \code{.sh}
* cd yourRepo
* git submodule add https://itsblue.dev/itsblue-development/QBluetoothLeUart.git
* git submodule update --init --recursive
* \endcode
*
* And in your MyProject.pro include the .pri file:
* \code{.pro}
* # Optional: enable QML stuff
* CONFIG += QBluetoothLeUart_QML
* # Include library
* include($$PWD/QBluetoothLeUart/QBluetoothLeUart.pri)
* \endcode
*/
/*!
* \brief The QBluetoothLeUart class can be used to talk to BluetoothLE devices via UART effordlessly.
* It can be used via C++ and QML.
*/
class QBluetoothLeUart : public QObject class QBluetoothLeUart : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QVariantList avaliableDevices READ getAvailableDevicesQML NOTIFY avaliableDevicesChanged) Q_PROPERTY(QVariantList avaliableDevices READ getAvailableDevicesDetailList NOTIFY avaliableDevicesChanged)
Q_PROPERTY(BluetoothLeUartState state READ getState NOTIFY stateChanged) Q_PROPERTY(BluetoothLeUartState state READ getState NOTIFY stateChanged)
public: public:
/*!
* \brief The BluetoothLeUartState enum contains all state of the QBluetoothLeUart class.
*/
enum BluetoothLeUartState { enum BluetoothLeUartState {
Idle = 0, Idle = 0, /*!< Waiting for instrucions */
Scanning, Scanning, /*!< Scanning for devices */
ScanFinished, ScanFinished, /*!< Scanning has finished, we are ready to connect */
Connecting, Connecting, /*!< Trying to connect */
Connected, ScanningForService, /*!< Connection was successfull, now scanning for services */
ServiceFound, ServiceFound, /*!< Services were found */
AcquireData Connected /*!< Connected. We are now ready to send and receive */
}; };
Q_ENUM(BluetoothLeUartState) Q_ENUM(BluetoothLeUartState)
@ -40,71 +71,183 @@ public:
QBluetoothLeUart(QObject *parent = nullptr); QBluetoothLeUart(QObject *parent = nullptr);
~QBluetoothLeUart(); ~QBluetoothLeUart();
/*!
* \brief Function to register QMl types
*/
static void init(); static void init();
void setUUIDs(const char uartServiceUUID[36], const char rxUUID[36], const char txUUID[36]); /*!
* \brief Function to set the UUIDs of the bluetooth service
* \param uartServiceUUID Service UUID
* \param txUUID UUID of the characteristic used to send data
* \param rxUUID UUID of the characteristic used to receive data
*/
void setUUIDs(const char uartServiceUUID[36], const char txUUID[36], const char rxUUID[36]);
private: private:
QString uartServiceUUID;
QString rxUUID;
QString txUUID;
QBluetoothLeUartDevice *currentBluetoothDevice; // The UUIDs
QString uartServiceUUID;
QString txUUID;
QString rxUUID;
// current state
QBluetoothLeUart::BluetoothLeUartState state;
// QBluetooth controllers
QBluetoothDeviceDiscoveryAgent *bluetoothDeviceDiscoveryAgent; QBluetoothDeviceDiscoveryAgent *bluetoothDeviceDiscoveryAgent;
//QList<QObject*> m_qlDevices;
QList<QBluetoothLeUartDevice*> availableDevices;
//QList<QString> m_qlFoundDevices;
QVector<quint16> m_qvMeasurements;
QLowEnergyController *bluetoothController; QLowEnergyController *bluetoothController;
QLowEnergyService *bluetoothService; QLowEnergyService *bluetoothService;
// Bluetooth device
QBluetoothLeUartDevice *currentBluetoothDevice;
QList<QBluetoothLeUartDevice*> availableDevices;
QLowEnergyDescriptor bluetoothTransmissionDescriptor; QLowEnergyDescriptor bluetoothTransmissionDescriptor;
bool foundValidUARTService; bool foundValidUARTService;
QBluetoothLeUart::BluetoothLeUartState state;
public slots: public slots:
/* Slots for user */ /*!
Q_INVOKABLE QBluetoothLeUart::BluetoothLeUartState getState() const; * \brief Fuction to start scanning for devices
*
* 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
*
* \see foundNewDevice()
* \see avaliableDevicesChanged()
* \see scanFinished()
* \see scanningErrorOccured()
*
* \return true if the scan started, false if the current state was not Idle
*/
Q_INVOKABLE bool startScanningForDevices();
Q_INVOKABLE void startScanningForDevices(); /*!
Q_INVOKABLE void stopScanningForDevices(); * \brief Function to stop scanning for devices
*
* \return true if the scan was stopped, false if the current state was not Scanning
*/
Q_INVOKABLE bool stopScanningForDevices();
/*!
* \brief Function to get all devices that were found during the last scan
*
* A QBluetoothLeUartDevice object can be used to connect to the specific device
*
* \see connectToDevice()
*
* \return List of all devices found during last scan
*/
Q_INVOKABLE QList<QBluetoothLeUartDevice*> getAvailableDevices(); Q_INVOKABLE QList<QBluetoothLeUartDevice*> getAvailableDevices();
Q_INVOKABLE QVariantList getAvailableDevicesQML();
Q_INVOKABLE void connectToDevice(QBluetoothLeUartDevice *device); /*!
Q_INVOKABLE void connectToDevice(int deviceId); * \brief Function to get a variant list of all devices that were found during the last scan
Q_INVOKABLE void sendData(QString s); *
Q_INVOKABLE void disconnectFromDevice(); * 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
*
* \see connectToDevice()
*
* \return Variant list of all devices found during last scan
*/
Q_INVOKABLE QVariantList getAvailableDevicesDetailList();
/*!
* \brief Function connect to a device using its internal id
*
* 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.
*
* \param deviceId the internal id of the device
*
* \see getAvailableDevicesDetailList()
* \see connectedToDevice()
* \see dataReceived()
*
* \return false if the device was not found in the internal list of discovered devices, true otherwise
*/
Q_INVOKABLE bool connectToDevice(int deviceId);
/*!
* \brief Function connect to a device using a QBluetoothLeUartDevice object
*
* 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.
*
* \param device The device to connect to
*
* \see getAvailableDevices()
* \see connectedToDevice()
* \see dataReceived()
*
* \return false if the device was not found in the internal list of discovered devices, true otherwise
*/
Q_INVOKABLE bool connectToDevice(QBluetoothLeUartDevice *device);
/*!
* \brief Function to disconnect from the current device
*
* \return false if no device was connected, true otherwise
*/
Q_INVOKABLE bool disconnectFromDevice();
/*!
* \brief Function to send data to the connected device
* \param data The data to send
* \return false if there was not device connected, true otherwise
*/
Q_INVOKABLE bool sendData(QString data);
/*!
* \brief Function to get the current state of QBluetoothLeUart
* \see BluetoothLeUartState
* \return The current state
*/
Q_INVOKABLE QBluetoothLeUart::BluetoothLeUartState getState() const;
private slots: private slots:
void setState(QBluetoothLeUart::BluetoothLeUartState newState); void setState(QBluetoothLeUart::BluetoothLeUartState newState);
/* Slots for QBluetothDeviceDiscoveryAgent */ // Slots for QBluetothDeviceDiscoveryAgent
void handleDeviceDiscovered(const QBluetoothDeviceInfo&); void handleDeviceDiscovered(const QBluetoothDeviceInfo&);
void handleScanFinished(); void handleScanFinished();
void handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error); void handleDeviceScanError(QBluetoothDeviceDiscoveryAgent::Error);
/* Slots for QLowEnergyController */ // Slots for QLowEnergyController
void handleServiceDiscovered(const QBluetoothUuid &); void handleServiceDiscovered(const QBluetoothUuid & uuid);
void handleServiceScanDone(); void handleServiceScanDone();
void handleControllerError(QLowEnergyController::Error); void handleControllerError(QLowEnergyController::Error);
void handleDeviceConnected(); void handleDeviceConnected();
void handleDeviceDisconnected(); void handleDeviceDisconnected();
/* Slotes for QLowEnergyService */ // Slots for QLowEnergyService
void handleServiceStateChange(QLowEnergyService::ServiceState s); void handleServiceStateChange(QLowEnergyService::ServiceState s);
void handleServiceCharacteristicChange(const QLowEnergyCharacteristic &c, const QByteArray &value); void handleServiceCharacteristicChange(const QLowEnergyCharacteristic &c, const QByteArray &value);
void handleServiceDescriptorWritten(const QLowEnergyDescriptor &d, const QByteArray &value); void handleServiceDescriptorWritten(const QLowEnergyDescriptor &d, const QByteArray &value);
signals: signals:
/* Signals for user */
void stateChanged(QBluetoothLeUart::BluetoothLeUartState newState); void stateChanged(QBluetoothLeUart::BluetoothLeUartState newState);
void avaliableDevicesChanged(QList<QBluetoothLeUartDevice*> avaliableDevices);
void foundNewDevice(QBluetoothLeUartDevice* device); void foundNewDevice(QBluetoothLeUartDevice* device);
void avaliableDevicesChanged(QList<QBluetoothLeUartDevice*> avaliableDevices);
void scanFinished(QList<QBluetoothLeUartDevice*> availableDevices);
void scanningErrorOccured(QBluetoothLeUart::BluetoothScanError error); void scanningErrorOccured(QBluetoothLeUart::BluetoothScanError error);
void dataReceived(QString s);
void connectedToDevice(); void connectedToDevice();
void dataReceived(QString s);
}; };
#endif // BLUETOOTHLEUART_H #endif // BLUETOOTHLEUART_H

View file

@ -7,6 +7,10 @@
#include <qbluetoothaddress.h> #include <qbluetoothaddress.h>
#include <qbluetoothuuid.h> #include <qbluetoothuuid.h>
/*!
* \brief The QBluetoothLeUartDevice class is a helper class for use with QBluetoothLeUart.
* It is used to get device details and connect to devices
*/
class QBluetoothLeUartDevice: public QObject class QBluetoothLeUartDevice: public QObject
{ {
Q_OBJECT Q_OBJECT
@ -18,7 +22,16 @@ public:
friend class QBluetoothLeUart; friend class QBluetoothLeUart;
/*!
* \brief Function to get the name of the device
* \return The name of the device
*/
Q_INVOKABLE QString getName(); Q_INVOKABLE QString getName();
/*!
* \brief Function to get the address of the device
* \return address of the device
*/
Q_INVOKABLE QString getAddress(); Q_INVOKABLE QString getAddress();
protected: protected: