Advertisement
Koteyk0o

C++ BLE Code

Jan 26th, 2021 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.16 KB | None | 0 0
  1. #include "bluetooth_bridge.h"
  2. #include <QtDebug>
  3.  
  4. bluetooth_Bridge::bluetooth_Bridge(QObject *parent) : QObject(parent)
  5. {
  6.  
  7. }
  8.  
  9.  
  10.  
  11. void bluetooth_Bridge::clear_Devices() {
  12.     qDeleteAll(_BLE_Devices);
  13.     _BLE_Devices.clear();
  14.  
  15.     emit devices_Changed();
  16. }
  17.  
  18.  
  19.  
  20. void bluetooth_Bridge::scan_Devices() {
  21.     clear_Devices();
  22.  
  23.     QBluetoothLocalDevice *local_Device = new QBluetoothLocalDevice;
  24.  
  25.     if (local_Device->isValid()) {
  26.         qDebug() << "Bluetooth Available.";
  27.         local_Device->powerOn();
  28.         local_Device->setHostMode(QBluetoothLocalDevice::HostDiscoverable);
  29.     }
  30.     else {
  31.         qDebug() << "Bluetooth IS NOT Available.";
  32.     }
  33.  
  34.     qDebug() << "Starting discovery agent...";
  35.     _device_Discovery_Agent = new QBluetoothDeviceDiscoveryAgent();
  36.     _device_Discovery_Agent->setLowEnergyDiscoveryTimeout(10000);
  37.  
  38.     connect(_device_Discovery_Agent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &bluetooth_Bridge::S_add_Device);
  39.     connect(_device_Discovery_Agent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error), this, &bluetooth_Bridge::S_device_Scan_Error);
  40.     connect(_device_Discovery_Agent, &QBluetoothDeviceDiscoveryAgent::finished, this, &bluetooth_Bridge::S_device_Scan_Finished);
  41.     connect(_device_Discovery_Agent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &bluetooth_Bridge::S_device_Scan_Finished);
  42.  
  43.     qDebug() << "Discovery agent started!";
  44.  
  45.     qDebug() << "Start scanning...";
  46.     _device_Discovery_Agent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
  47.     qDebug() << "Scanning started!";
  48. }
  49.  
  50.  
  51.  
  52. void bluetooth_Bridge::S_add_Device(const QBluetoothDeviceInfo &device) {
  53.     qDebug() << "Found new device:" << device.name() << "," << device.address().toString();
  54.     _BLE_Devices.append(new device_Info(device));
  55.  
  56.     emit devices_Changed();
  57. }
  58.  
  59.  
  60.  
  61. void bluetooth_Bridge::S_device_Scan_Finished() {
  62.     if (_BLE_Devices.isEmpty()) {
  63.         qDebug() << "No Low Energy devices found.";
  64.     }
  65.     else {
  66.         qDebug() << "Scanning done successfully." << _BLE_Devices.size() << "devices founded.";
  67.     }
  68.  
  69.     _device_Discovery_Agent->stop();
  70.     device_Info *current_Device = nullptr;
  71.     for (QObject *entry : qAsConst(_BLE_Devices)) {
  72.         auto device = qobject_cast<device_Info *>(entry);
  73.         if (device) {
  74.             if (device->get_Device_Address() == "3F:5B:7D:80:38:C5") {
  75.                 current_Device = device;
  76.                 qDebug() << "CURRENT DEVICE UPDATED.";
  77.                 break;
  78.             }
  79.         }
  80.     }
  81.  
  82.     if (current_Device) {
  83.         qDebug() << "CONNECT TO DEVICE...";
  84.         connect_Device(current_Device);
  85.     }
  86.  
  87.     emit devices_Changed();
  88. }
  89.  
  90.  
  91.  
  92. void bluetooth_Bridge::S_device_Scan_Error(QBluetoothDeviceDiscoveryAgent::Error error) {
  93.     if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
  94.         qDebug() << "The Bluetooth adaptor is powered off.";
  95.  
  96.     else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
  97.         qDebug() << "Writing or reading from the device resulted in an error.";
  98.  
  99.     else
  100.         qDebug() << "An unknown error has occurred.";
  101. }
  102.  
  103.  
  104.  
  105. void bluetooth_Bridge::connect_Device(device_Info *device) {
  106.     _current_Device = device;
  107.  
  108.     // Disconnect and delete old connection
  109.     if (_BLE_Controller) {
  110.         _BLE_Controller->disconnectFromDevice();
  111.         delete _BLE_Controller;
  112.         _BLE_Controller = nullptr;
  113.     }
  114.  
  115.     // Create new controller and connect it if device available
  116.     if (_current_Device) {
  117.         _BLE_Controller = QLowEnergyController::createCentral(_current_Device->get_Device());
  118.  
  119.         connect(_BLE_Controller, SIGNAL(connected()), this, SLOT(S_device_Connected()));
  120.         connect(_BLE_Controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), this, SLOT(S_service_Discovered(QBluetoothUuid)));
  121.         connect(_BLE_Controller, SIGNAL(discoveryFinished()), this, SLOT(S_service_Scan_Done()));
  122.         connect(_BLE_Controller, SIGNAL(disconnected()), this, SLOT(S_device_Disconnected()));
  123.         connect(_BLE_Controller, SIGNAL(error(QLowEnergyController::Error)), this, SLOT(S_BLE_Controller_Error(QLowEnergyController::Error)));
  124.  
  125.         _BLE_Controller->connectToDevice();
  126.     }
  127. }
  128.  
  129.  
  130.  
  131. void bluetooth_Bridge::S_device_Connected() {
  132.     qDebug() << "CONNECT TO DEVICE SUCCESSFUL.";
  133.     _BLE_Services_UUID.clear();
  134.     _BLE_Controller->discoverServices();
  135. }
  136.  
  137.  
  138.  
  139. void bluetooth_Bridge::S_device_Disconnected() {
  140.     qWarning() << "DEVICE DISCONNECTED.";
  141. }
  142.  
  143.  
  144.  
  145. void bluetooth_Bridge::S_service_Discovered(const QBluetoothUuid &gatt) {
  146.     qDebug() << "Service Discovered:" << gatt;
  147.  
  148.     if (gatt.toString() == "{ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6}") {
  149.         qDebug() << "Display Service Discovered:" << gatt;
  150.         _display_Service_Found = true;
  151.     }
  152. }
  153.  
  154.  
  155.  
  156. void bluetooth_Bridge::S_service_Scan_Done() {
  157.     qDebug() << "Services Scan Done.";
  158.  
  159.     _BLE_Services_UUID = _BLE_Controller->services();
  160.  
  161.     if (_BLE_Services_UUID.isEmpty()) {
  162.         qWarning() << "CAN'T FIND ANY SERVICES.";
  163.     }
  164.     else {
  165.         _BLE_Service = _BLE_Controller->createServiceObject(_BLE_Services_UUID.at(7), this);
  166.         qDebug() << "Selected Service:" << _BLE_Services_UUID.at(7);
  167.         qDebug() << "Display Service Created.";
  168.     }
  169.  
  170.     if (_BLE_Service) {
  171.         qDebug() << "Display Service Found.";
  172.  
  173.         connect(_BLE_Service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)), this, SLOT(S_service_State_Changed(QLowEnergyService::ServiceState)));
  174.         connect(_BLE_Service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic, QByteArray)), this, SLOT(S_service_Characteristic_Changed(QLowEnergyCharacteristic, QByteArray)));
  175.         connect(_BLE_Service, SIGNAL(characteristicRead(QLowEnergyCharacteristic, QByteArray)), this, SLOT(S_service_Characteristic_Read(QLowEnergyCharacteristic, QByteArray)));
  176.         connect(_BLE_Service, SIGNAL(error(QLowEnergyService::ServiceError)), this, SLOT(S_service_Error(QLowEnergyService::ServiceError)));
  177.  
  178.         if (_BLE_Service->state() == QLowEnergyService::DiscoveryRequired) {
  179.             qDebug() << "Discovering Service Details...";
  180.             _BLE_Service->discoverDetails();
  181.         }
  182.     }
  183.     else {
  184.         qWarning() << "Display Service NOT Found.";
  185.     }
  186. }
  187.  
  188.  
  189.  
  190. void bluetooth_Bridge::S_service_State_Changed(QLowEnergyService::ServiceState state) {
  191.     qDebug() << "Service State Changed, State:" << state;
  192. }
  193.  
  194.  
  195.  
  196. void bluetooth_Bridge::S_service_Characteristic_Changed(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) {
  197.     Q_UNUSED(characteristic);
  198.     qDebug() << "Characteristic Changed, Value:" << value;
  199. }
  200.  
  201.  
  202.  
  203. void bluetooth_Bridge::S_service_Characteristic_Read(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) {
  204.     Q_UNUSED(characteristic);
  205.     qDebug() << "Characteristic Read, Value:" << value;
  206. }
  207.  
  208.  
  209.  
  210. void bluetooth_Bridge::S_BLE_Controller_Error(QLowEnergyController::Error error) {
  211.     qWarning() << "Controller Error:" << error;
  212. }
  213.  
  214.  
  215.  
  216. void bluetooth_Bridge::S_service_Error(QLowEnergyService::ServiceError error) {
  217.     qWarning() << "Service Error:" << error;
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement