Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bluetooth_bridge.h"
- #include <QtDebug>
- bluetooth_Bridge::bluetooth_Bridge(QObject *parent) : QObject(parent)
- {
- }
- void bluetooth_Bridge::clear_Devices() {
- qDeleteAll(_BLE_Devices);
- _BLE_Devices.clear();
- emit devices_Changed();
- }
- void bluetooth_Bridge::scan_Devices() {
- clear_Devices();
- QBluetoothLocalDevice *local_Device = new QBluetoothLocalDevice;
- if (local_Device->isValid()) {
- qDebug() << "Bluetooth Available.";
- local_Device->powerOn();
- local_Device->setHostMode(QBluetoothLocalDevice::HostDiscoverable);
- }
- else {
- qDebug() << "Bluetooth IS NOT Available.";
- }
- qDebug() << "Starting discovery agent...";
- _device_Discovery_Agent = new QBluetoothDeviceDiscoveryAgent();
- _device_Discovery_Agent->setLowEnergyDiscoveryTimeout(10000);
- connect(_device_Discovery_Agent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &bluetooth_Bridge::S_add_Device);
- connect(_device_Discovery_Agent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error), this, &bluetooth_Bridge::S_device_Scan_Error);
- connect(_device_Discovery_Agent, &QBluetoothDeviceDiscoveryAgent::finished, this, &bluetooth_Bridge::S_device_Scan_Finished);
- connect(_device_Discovery_Agent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &bluetooth_Bridge::S_device_Scan_Finished);
- qDebug() << "Discovery agent started!";
- qDebug() << "Start scanning...";
- _device_Discovery_Agent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
- qDebug() << "Scanning started!";
- }
- void bluetooth_Bridge::S_add_Device(const QBluetoothDeviceInfo &device) {
- qDebug() << "Found new device:" << device.name() << "," << device.address().toString();
- _BLE_Devices.append(new device_Info(device));
- emit devices_Changed();
- }
- void bluetooth_Bridge::S_device_Scan_Finished() {
- if (_BLE_Devices.isEmpty()) {
- qDebug() << "No Low Energy devices found.";
- }
- else {
- qDebug() << "Scanning done successfully." << _BLE_Devices.size() << "devices founded.";
- }
- _device_Discovery_Agent->stop();
- device_Info *current_Device = nullptr;
- for (QObject *entry : qAsConst(_BLE_Devices)) {
- auto device = qobject_cast<device_Info *>(entry);
- if (device) {
- if (device->get_Device_Address() == "3F:5B:7D:80:38:C5") {
- current_Device = device;
- qDebug() << "CURRENT DEVICE UPDATED.";
- break;
- }
- }
- }
- if (current_Device) {
- qDebug() << "CONNECT TO DEVICE...";
- connect_Device(current_Device);
- }
- emit devices_Changed();
- }
- void bluetooth_Bridge::S_device_Scan_Error(QBluetoothDeviceDiscoveryAgent::Error error) {
- if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
- qDebug() << "The Bluetooth adaptor is powered off.";
- else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
- qDebug() << "Writing or reading from the device resulted in an error.";
- else
- qDebug() << "An unknown error has occurred.";
- }
- void bluetooth_Bridge::connect_Device(device_Info *device) {
- _current_Device = device;
- // Disconnect and delete old connection
- if (_BLE_Controller) {
- _BLE_Controller->disconnectFromDevice();
- delete _BLE_Controller;
- _BLE_Controller = nullptr;
- }
- // Create new controller and connect it if device available
- if (_current_Device) {
- _BLE_Controller = QLowEnergyController::createCentral(_current_Device->get_Device());
- connect(_BLE_Controller, SIGNAL(connected()), this, SLOT(S_device_Connected()));
- connect(_BLE_Controller, SIGNAL(serviceDiscovered(QBluetoothUuid)), this, SLOT(S_service_Discovered(QBluetoothUuid)));
- connect(_BLE_Controller, SIGNAL(discoveryFinished()), this, SLOT(S_service_Scan_Done()));
- connect(_BLE_Controller, SIGNAL(disconnected()), this, SLOT(S_device_Disconnected()));
- connect(_BLE_Controller, SIGNAL(error(QLowEnergyController::Error)), this, SLOT(S_BLE_Controller_Error(QLowEnergyController::Error)));
- _BLE_Controller->connectToDevice();
- }
- }
- void bluetooth_Bridge::S_device_Connected() {
- qDebug() << "CONNECT TO DEVICE SUCCESSFUL.";
- _BLE_Services_UUID.clear();
- _BLE_Controller->discoverServices();
- }
- void bluetooth_Bridge::S_device_Disconnected() {
- qWarning() << "DEVICE DISCONNECTED.";
- }
- void bluetooth_Bridge::S_service_Discovered(const QBluetoothUuid &gatt) {
- qDebug() << "Service Discovered:" << gatt;
- if (gatt.toString() == "{ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6}") {
- qDebug() << "Display Service Discovered:" << gatt;
- _display_Service_Found = true;
- }
- }
- void bluetooth_Bridge::S_service_Scan_Done() {
- qDebug() << "Services Scan Done.";
- _BLE_Services_UUID = _BLE_Controller->services();
- if (_BLE_Services_UUID.isEmpty()) {
- qWarning() << "CAN'T FIND ANY SERVICES.";
- }
- else {
- _BLE_Service = _BLE_Controller->createServiceObject(_BLE_Services_UUID.at(7), this);
- qDebug() << "Selected Service:" << _BLE_Services_UUID.at(7);
- qDebug() << "Display Service Created.";
- }
- if (_BLE_Service) {
- qDebug() << "Display Service Found.";
- connect(_BLE_Service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)), this, SLOT(S_service_State_Changed(QLowEnergyService::ServiceState)));
- connect(_BLE_Service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic, QByteArray)), this, SLOT(S_service_Characteristic_Changed(QLowEnergyCharacteristic, QByteArray)));
- connect(_BLE_Service, SIGNAL(characteristicRead(QLowEnergyCharacteristic, QByteArray)), this, SLOT(S_service_Characteristic_Read(QLowEnergyCharacteristic, QByteArray)));
- connect(_BLE_Service, SIGNAL(error(QLowEnergyService::ServiceError)), this, SLOT(S_service_Error(QLowEnergyService::ServiceError)));
- if (_BLE_Service->state() == QLowEnergyService::DiscoveryRequired) {
- qDebug() << "Discovering Service Details...";
- _BLE_Service->discoverDetails();
- }
- }
- else {
- qWarning() << "Display Service NOT Found.";
- }
- }
- void bluetooth_Bridge::S_service_State_Changed(QLowEnergyService::ServiceState state) {
- qDebug() << "Service State Changed, State:" << state;
- }
- void bluetooth_Bridge::S_service_Characteristic_Changed(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) {
- Q_UNUSED(characteristic);
- qDebug() << "Characteristic Changed, Value:" << value;
- }
- void bluetooth_Bridge::S_service_Characteristic_Read(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) {
- Q_UNUSED(characteristic);
- qDebug() << "Characteristic Read, Value:" << value;
- }
- void bluetooth_Bridge::S_BLE_Controller_Error(QLowEnergyController::Error error) {
- qWarning() << "Controller Error:" << error;
- }
- void bluetooth_Bridge::S_service_Error(QLowEnergyService::ServiceError error) {
- qWarning() << "Service Error:" << error;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement