#include "VBluetooth.h" #include "BLEScanner.h" // Qt // Project using namespace View; VBluetooth::VBluetooth(QObject *parent) : QObject(parent) { connect(&_BLEScanner, SIGNAL(didReceiveScanForDevicesError(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(onScanForDevicesError(QBluetoothDeviceDiscoveryAgent::Error))); connect(&_BLEScanner, SIGNAL(didFinishScan()), this, SLOT(onScanForDevicesFinished())); connect(&_BLEScanner, SIGNAL(didDiscoverDevice(QBluetoothDeviceInfo)), this, SLOT(onDeviceDiscovered(const QBluetoothDeviceInfo))); connect(this, SIGNAL(didSelectDevice(const QString)), &_BLEScanner, SLOT(doSelectDevice(const QString))); connect(this, SIGNAL(didRequestScanForDevices()), &_BLEScanner, SLOT(doScanForDevices())); } /*! * \brief VBluetooth::onScanForDevices * Slot for when the user starts scanning for devices. * Updates the status and emits a signal to the BLEScanner to * begin a new scan. */ void VBluetooth::doScanForDevices() { bleDevices.clear(); emit didDevicesChanged(); emit didRequestScanForDevices(); status = "Scanning..."; emit didStatusChanged(); } /*! * \brief VBluetooth::onScanForDevicesError * Slot called when the BLEScanner enounters an error * while scanning for devices * \param error */ void VBluetooth::onScanForDevicesError(QBluetoothDeviceDiscoveryAgent::Error error) { switch (error) { case QBluetoothDeviceDiscoveryAgent::PoweredOffError: status = tr("Error: The bluetooth module is powered off."); break; default: status = tr("Error: Scan for devices error."); break; } emit didStatusChanged(); } /*! * \brief VBluetooth::onDeviceDiscovered * Slot for when a device is discovered by the BLEScanner. * \param device - the BLE Device info of the discovered device */ void VBluetooth::onDeviceDiscovered(const QBluetoothDeviceInfo &device) { VBluetoothDeviceInfo *info = new VBluetoothDeviceInfo(device); bleDevices.append(info); qDebug() << "VBluetooth: Discovered " << device.address(); emit didDevicesChanged(); } /*! * \brief VBluetooth::onScanForDevicesFinished * Slot called when the scan is finished. Updates the scanning status. */ void VBluetooth::onScanForDevicesFinished() { emit didScanFinished(); status = "Scan Finished."; emit didStatusChanged(); } /*! * \brief VBluetooth::getDevices * Gets the BLE modelData for QML * \return QVariant - the modelData */ QVariant VBluetooth::doGetDevices() { return QVariant::fromValue(bleDevices); } /*! * \brief VBluetooth::onSelectedDevice * Emits a signal that the device was selected * \param addr - the selected BLE mac address */ void VBluetooth::doSelectDevice(const QString &addr) { emit didSelectDevice(addr); }