/*! * \copyright * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file BluetoothInterface.h * \author (last) Behrouz NematiPour * \author (original) Behrouz NematiPour * \date (last) 22-Aug-2021 * \date (original) 22-Aug-2021 */ #pragma once // Qt #include #include #include #include #include // Project #include "main.h" // Doxygen : do not remove #include "MBluetooth.h" // Define #define _BluetoothInterface Bluetooth::BluetoothInterface::I() // forward declarations class tst_initializations; // namespace namespace Bluetooth { using namespace Model; /*! * \brief The MessageAcknowModel class * \details The wrapper class of the AcknowModel which does the thread handling. */ class BluetoothInterface : public QObject { Q_OBJECT // Singleton SINGLETON(BluetoothInterface) // friends friend class ::tst_initializations; typedef QMap< QBluetoothUuid::ServiceClassUuid, QLowEnergyService *> ServiceMap; QThread *_thread = nullptr; bool _init = false; QStringList _supportedDeviceKeywords { "BP7000", "BLEsmart", "BLESmart" }; // Regarding to Omron Documents. QBluetoothLocalDevice *_local = nullptr ; QBluetoothDeviceDiscoveryAgent *_agent = nullptr ; QLowEnergyController *_device = nullptr ; // QList _services ; QLowEnergyService *_serviceDeviceInformation = nullptr ; QLowEnergyService *_serviceCurrentTime = nullptr ; QLowEnergyService *_serviceBloodPressure = nullptr ; QLowEnergyService *_serviceBattery = nullptr ; QBluetoothDeviceInfo _temp ; bool _connectionActive = false ; const quint16 _interval = 1000 ; // the interface timer base interval in mSec public: // 19 total bytes struct MeasurementData { uint8_t flags; // 1 byte uint16_t systolic; // 2 bytes uint16_t diastolic; // 2 bytes uint16_t mean_arterial_pressure_value; // 2 bytes uint16_t year; // 2 bytes uint8_t month; // 1 byte uint8_t day; // 1 byte uint8_t hour; // 1 byte uint8_t minute; // 1 byte uint8_t second; // 1 byte uint16_t pulse_rate; // 2 bytes uint8_t user_id; // 1 byte uint16_t measurement_status; // 2 bytes }; protected: void timerEvent(QTimerEvent *event) override; public slots: bool init(); bool init(QThread &vThread); private slots: void quit(); // Local Device Slots void onLocalDeviceConnect (const QBluetoothAddress &vAddress ); void onLocalDeviceDisconnect (const QBluetoothAddress &vAddress ); void onLocalError ( QBluetoothLocalDevice::Error vError ); // Discovery Agent Slots void onAgentDiscoverDevice (const QBluetoothDeviceInfo & vDevice ); void onAgentDiscoverFinish (); void onAgentDiscoverCancel (); void onAgentDiscoverError ( QBluetoothDeviceDiscoveryAgent::Error vError ); // Device Slots void onDeviceConnect ( ); void onDeviceDisconnect ( ); void onDeviceDiscoverService (const QBluetoothUuid &vService); void onDeviceDiscoverFinish ( ); void onDeviceError ( QLowEnergyController::Error vError ); void onServiceCharacteristicChanged (const QLowEnergyCharacteristic &vCharacteristic , const QByteArray &vValue ); void onServiceCharacteristicRead (const QLowEnergyCharacteristic &vCharacteristic , const QByteArray &vValue ) { qDebug() << " ..... Service Charc R:" << vCharacteristic .name() << vValue; } void onServiceCharacteristicWritten (const QLowEnergyCharacteristic &vCharacteristic , const QByteArray &vValue ) { qDebug() << " ..... Service Charc W:" << vCharacteristic .name() << vValue; } void onServiceDescriptorRead (const QLowEnergyDescriptor &vDescriptor , const QByteArray &vValue ) { qDebug() << " ..... Service Descr R:" << vDescriptor .name() << vValue; } void onServiceDescriptorWritten (const QLowEnergyDescriptor &vDescriptor , const QByteArray &vValue ) { qDebug() << " ..... Service Descr W:" << vDescriptor .name() << vValue; } void onServiceError ( QLowEnergyService::ServiceError vError ); void onServiceStateChanged ( QLowEnergyService::ServiceState vState ); private: void initConnections (); void initConnectionsDevice (); void initConnectionsService (QLowEnergyService *vService); void initThread(QThread &vThread); void quitThread(); bool initDevice (); bool makeDevice (); void quitDevice (); void initServices (const QBluetoothUuid &vService); void makeServices (const QBluetoothUuid &vService); void quitServices (); bool isLocalValid (); bool isInfoValid (); bool isDeviceValid (); // bool isServiceValid (QLowEnergyService *vService); bool isDetailValid (const QLowEnergyCharacteristic &vDetail); bool isDeviceSupported ( const QString &vName ); bool connectToDevice (); void discoverServices (); void discoverServicesDetails(); void notifyStateChange(const BluetoothData &vData); void parseMeasurement (const QByteArray &byteArray); signals: void didStateChange(const BluetoothData &vData); SAFE_CALL(start ) SAFE_CALL(doScan) }; }