From 0913e486548c712bdca46cfef091dae501c8fec8 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Mon, 27 Aug 2018 22:36:53 -0700 Subject: [PATCH 1/5] implementing device discovery --- CMakeLists.txt | 9 ++++-- common/BaseDiscoveryAgent.cpp | 14 ++++++++ common/BaseDiscoveryAgent.h | 26 +++++++++++++++ common/BluetoothAdress.cpp | 26 +++++++++++++++ common/BluetoothAdress.h | 25 +++++++++++++++ forms/deviceselectdialog.cpp | 2 +- platform/windows/common/DiscoveryAgent.cpp | 37 ++++++++++++++++++++++ platform/windows/common/DiscoveryAgent.h | 31 ++++++++++++++++++ 8 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 common/BaseDiscoveryAgent.cpp create mode 100644 common/BaseDiscoveryAgent.h create mode 100644 common/BluetoothAdress.cpp create mode 100644 common/BluetoothAdress.h create mode 100644 platform/windows/common/DiscoveryAgent.cpp create mode 100644 platform/windows/common/DiscoveryAgent.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 07bcc19..ec85a24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,8 @@ set (SOURCES common/main.cpp common/metawearwrapperbase.cpp common/util.cpp -) + common/BluetoothAdress.cpp + common/BaseDiscoveryAgent.cpp) set(HEADERS qcustomplot.h @@ -43,8 +44,8 @@ set(HEADERS common/metawearwrapperbase.h common/util.h - -) + common/BaseDiscoveryAgent.h + common/BluetoothAdress.h) set ( UIS @@ -78,10 +79,12 @@ if (MSVC) set (PLATFORM_SOURCE platform/windows/common/metawearwrapper.cpp + platform/windows/common/DiscoveryAgent.cpp ) set(PLATFORM_HEADERS platform/windows/common/metawearwrapper.h + platform/windows/common/DiscoveryAgent.h ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /ZW") diff --git a/common/BaseDiscoveryAgent.cpp b/common/BaseDiscoveryAgent.cpp new file mode 100644 index 0000000..d5a7476 --- /dev/null +++ b/common/BaseDiscoveryAgent.cpp @@ -0,0 +1,14 @@ +// +// Created by Michael on 8/27/2018. +// + +#include "common/BaseDiscoveryAgent.h" + + +BaseDiscoveryAgent::BaseDiscoveryAgent() { + +} + +BaseDiscoveryAgent::~BaseDiscoveryAgent() { + +} \ No newline at end of file diff --git a/common/BaseDiscoveryAgent.h b/common/BaseDiscoveryAgent.h new file mode 100644 index 0000000..8f26168 --- /dev/null +++ b/common/BaseDiscoveryAgent.h @@ -0,0 +1,26 @@ +// +// Created by Michael on 8/27/2018. +// + +#ifndef SMART_BASEDISCOVERYAGENT_H +#define SMART_BASEDISCOVERYAGENT_H + +#include +#include "BluetoothAdress.h" + +class BaseDiscoveryAgent : public QObject{ + Q_OBJECT +private: + +public: + BaseDiscoveryAgent(); + ~BaseDiscoveryAgent(); + + virtual void start() = 0; +signals: + void deviceDiscovered(const BluetoothAdress& address); + void finished(); + +}; + +#endif //SMART_BASEDISCOVERYAGENT_H diff --git a/common/BluetoothAdress.cpp b/common/BluetoothAdress.cpp new file mode 100644 index 0000000..38687a0 --- /dev/null +++ b/common/BluetoothAdress.cpp @@ -0,0 +1,26 @@ +// +// Created by Michael on 8/27/2018. +// + +#include "common/BluetoothAdress.h" + +BluetoothAdress::BluetoothAdress(const QString& mac, const QString& title) : + m_mac(mac),m_title(title) {} + +BluetoothAdress::BluetoothAdress(const BluetoothAdress& address): + m_title(address.m_title),m_mac(address.m_mac){ + +} + + +BluetoothAdress::~BluetoothAdress() { +} + +const QString& BluetoothAdress::getTitle() const{ + return m_title; +} + +const QString& BluetoothAdress::getMac() const{ + return m_mac; +} + diff --git a/common/BluetoothAdress.h b/common/BluetoothAdress.h new file mode 100644 index 0000000..a04b1f2 --- /dev/null +++ b/common/BluetoothAdress.h @@ -0,0 +1,25 @@ +// +// Created by Michael on 8/27/2018. +// + +#ifndef SMART_BLUETOOTHADRESS_H +#define SMART_BLUETOOTHADRESS_H + +#include + +class BluetoothAdress{ +private: + QString m_mac; + QString m_title; +public: + BluetoothAdress(const QString& mac, const QString& title); + BluetoothAdress(const BluetoothAdress& address); + + ~BluetoothAdress(); + + const QString& getTitle() const; + const QString& getMac() const; + +}; + +#endif //SMART_BLUETOOTHADRESS_H diff --git a/forms/deviceselectdialog.cpp b/forms/deviceselectdialog.cpp index 26332bf..63e79aa 100644 --- a/forms/deviceselectdialog.cpp +++ b/forms/deviceselectdialog.cpp @@ -85,7 +85,7 @@ void DeviceSelectDialog::accept() { void DeviceSelectDialog::addDevice(const QBluetoothDeviceInfo &info) { - if(info.name() != "MetaWear") + if(info.name() != "MetaWear" || info.name() != "MetaBoot") return; if(m_deviceBlackList.contains(info.address().toString())) return; diff --git a/platform/windows/common/DiscoveryAgent.cpp b/platform/windows/common/DiscoveryAgent.cpp new file mode 100644 index 0000000..004c6c7 --- /dev/null +++ b/platform/windows/common/DiscoveryAgent.cpp @@ -0,0 +1,37 @@ +// +// Created by Michael on 8/27/2018. +// + +#include "common/DiscoveryAgent.h" + + +DiscoveryAgent::DiscoveryAgent() : BaseDiscoveryAgent(){ + Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); + + CoInitializeSecurity( + nullptr, // TODO: "O:BAG:BAD:(A;;0x7;;;PS)(A;;0x3;;;SY)(A;;0x7;;;BA)(A;;0x3;;;AC)(A;;0x3;;;LS)(A;;0x3;;;NS)" + -1, + nullptr, + nullptr, + RPC_C_AUTHN_LEVEL_DEFAULT, + RPC_C_IMP_LEVEL_IDENTIFY, + NULL, + EOAC_NONE, + nullptr); + + m_bleAdvertisementWatcher = ref new Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher(); + m_bleAdvertisementWatcher->ScanningMode = Bluetooth::Advertisement::BluetoothLEScanningMode::Active; + m_bleAdvertisementWatcher->Received += ref new Windows::Foundation::TypedEventHandler( + [=](Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher ^watcher, Bluetooth::Advertisement::BluetoothLEAdvertisementReceivedEventArgs^ eventArgs){ + + } +} + +void DiscoveryAgent::start() { + +} + +DiscoveryAgent::~DiscoveryAgent() { + +} + diff --git a/platform/windows/common/DiscoveryAgent.h b/platform/windows/common/DiscoveryAgent.h new file mode 100644 index 0000000..8897909 --- /dev/null +++ b/platform/windows/common/DiscoveryAgent.h @@ -0,0 +1,31 @@ +// +// Created by Michael on 8/27/2018. +// +#ifndef SMART_DISCOVERYAGENT_H +#define SMART_DISCOVERYAGENT_H + + +#include "common/BaseDiscoveryAgent.h" +#include +#include +#include +#include +#include +#include + +using namespace Platform; +using namespace Windows::Devices; + +class DiscoveryAgent : public BaseDiscoveryAgent{ +private: + Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher^ m_bleAdvertisementWatcher; + +public: + DiscoveryAgent(); + ~DiscoveryAgent(); + + void start(); + +}; + +#endif //SMART_DISCOVERYAGENT_H From 408aac460cf1c09387000e6f0419ae3d60fda46e Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Tue, 28 Aug 2018 17:55:59 -0700 Subject: [PATCH 2/5] implemented discovery wrapper --- CMakeLists.txt | 5 +- common/BaseDiscoveryAgent.cpp | 14 ----- common/BaseDiscoveryAgent.h | 14 +++-- common/BluetoothAddress.cpp | 28 +++++++++ .../{BluetoothAdress.h => BluetoothAddress.h} | 10 ++-- common/BluetoothAdress.cpp | 26 -------- common/main.cpp | 19 ++++++ common/metawearwrapperbase.h | 7 ++- common/util.h | 5 +- forms/deviceselectdialog.cpp | 59 ++++++++++--------- forms/deviceselectdialog.h | 14 ++--- forms/mainwindow.cpp | 23 ++++---- forms/mainwindow.h | 5 +- forms/sensorpanel.cpp | 16 ++--- forms/sensorpanel.h | 14 ++--- platform/windows/common/DiscoveryAgent.cpp | 58 ++++++++++-------- platform/windows/common/DiscoveryAgent.h | 6 +- platform/windows/common/metawearwrapper.cpp | 55 +++++++---------- platform/windows/common/metawearwrapper.h | 14 ++--- 19 files changed, 200 insertions(+), 192 deletions(-) delete mode 100644 common/BaseDiscoveryAgent.cpp create mode 100644 common/BluetoothAddress.cpp rename common/{BluetoothAdress.h => BluetoothAddress.h} (52%) delete mode 100644 common/BluetoothAdress.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ec85a24..70c5a62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,7 @@ set (SOURCES common/main.cpp common/metawearwrapperbase.cpp common/util.cpp - common/BluetoothAdress.cpp - common/BaseDiscoveryAgent.cpp) + common/BluetoothAddress.cpp) set(HEADERS qcustomplot.h @@ -45,7 +44,7 @@ set(HEADERS common/metawearwrapperbase.h common/util.h common/BaseDiscoveryAgent.h - common/BluetoothAdress.h) + common/BluetoothAddress.h) set ( UIS diff --git a/common/BaseDiscoveryAgent.cpp b/common/BaseDiscoveryAgent.cpp deleted file mode 100644 index d5a7476..0000000 --- a/common/BaseDiscoveryAgent.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// -// Created by Michael on 8/27/2018. -// - -#include "common/BaseDiscoveryAgent.h" - - -BaseDiscoveryAgent::BaseDiscoveryAgent() { - -} - -BaseDiscoveryAgent::~BaseDiscoveryAgent() { - -} \ No newline at end of file diff --git a/common/BaseDiscoveryAgent.h b/common/BaseDiscoveryAgent.h index 8f26168..48949be 100644 --- a/common/BaseDiscoveryAgent.h +++ b/common/BaseDiscoveryAgent.h @@ -6,19 +6,23 @@ #define SMART_BASEDISCOVERYAGENT_H #include -#include "BluetoothAdress.h" +#include "BluetoothAddress.h" class BaseDiscoveryAgent : public QObject{ Q_OBJECT private: - public: - BaseDiscoveryAgent(); - ~BaseDiscoveryAgent(); + BaseDiscoveryAgent(){ + + } + ~BaseDiscoveryAgent(){ + + } virtual void start() = 0; + virtual void stop() = 0; signals: - void deviceDiscovered(const BluetoothAdress& address); + void deviceDiscovered(BluetoothAddress address); void finished(); }; diff --git a/common/BluetoothAddress.cpp b/common/BluetoothAddress.cpp new file mode 100644 index 0000000..1f81bfe --- /dev/null +++ b/common/BluetoothAddress.cpp @@ -0,0 +1,28 @@ +// +// Created by Michael on 8/27/2018. +// + +#include "common/BluetoothAddress.h" + +BluetoothAddress::BluetoothAddress(){ + +} +BluetoothAddress::BluetoothAddress(const QString& mac, const QString& title) : + m_mac(mac),m_title(title) {} + +BluetoothAddress::BluetoothAddress(const BluetoothAddress& address): + m_title(address.m_title),m_mac(address.m_mac){ +} + + +BluetoothAddress::~BluetoothAddress() { +} + +const QString& BluetoothAddress::getTitle() const{ + return m_title; +} + +const QString& BluetoothAddress::getMac() const{ + return m_mac; +} + diff --git a/common/BluetoothAdress.h b/common/BluetoothAddress.h similarity index 52% rename from common/BluetoothAdress.h rename to common/BluetoothAddress.h index a04b1f2..e8c834f 100644 --- a/common/BluetoothAdress.h +++ b/common/BluetoothAddress.h @@ -7,15 +7,17 @@ #include -class BluetoothAdress{ +class BluetoothAddress{ private: QString m_mac; QString m_title; public: - BluetoothAdress(const QString& mac, const QString& title); - BluetoothAdress(const BluetoothAdress& address); + BluetoothAddress(); + BluetoothAddress(const QString& mac, const QString& title); + BluetoothAddress(const BluetoothAddress& address); + BluetoothAddress(unsigned long long address, const QString& title); - ~BluetoothAdress(); + ~BluetoothAddress(); const QString& getTitle() const; const QString& getMac() const; diff --git a/common/BluetoothAdress.cpp b/common/BluetoothAdress.cpp deleted file mode 100644 index 38687a0..0000000 --- a/common/BluetoothAdress.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// -// Created by Michael on 8/27/2018. -// - -#include "common/BluetoothAdress.h" - -BluetoothAdress::BluetoothAdress(const QString& mac, const QString& title) : - m_mac(mac),m_title(title) {} - -BluetoothAdress::BluetoothAdress(const BluetoothAdress& address): - m_title(address.m_title),m_mac(address.m_mac){ - -} - - -BluetoothAdress::~BluetoothAdress() { -} - -const QString& BluetoothAdress::getTitle() const{ - return m_title; -} - -const QString& BluetoothAdress::getMac() const{ - return m_mac; -} - diff --git a/common/main.cpp b/common/main.cpp index 455df2f..38958f5 100644 --- a/common/main.cpp +++ b/common/main.cpp @@ -19,10 +19,29 @@ #include #include + +#if(_WIN64) +#include +static Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); +#endif + int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); +#if(_WIN64) + CoInitializeSecurity( + nullptr, // TODO: "O:BAG:BAD:(A;;0x7;;;PS)(A;;0x3;;;SY)(A;;0x7;;;BA)(A;;0x3;;;AC)(A;;0x3;;;LS)(A;;0x3;;;NS)" + -1, + nullptr, + nullptr, + RPC_C_AUTHN_LEVEL_DEFAULT, + RPC_C_IMP_LEVEL_IDENTIFY, + NULL, + EOAC_NONE, + nullptr); +#endif + QApplication app(argc, argv); MainWindow window; window.show(); diff --git a/common/metawearwrapperbase.h b/common/metawearwrapperbase.h index 7b0635f..779acbe 100644 --- a/common/metawearwrapperbase.h +++ b/common/metawearwrapperbase.h @@ -14,6 +14,10 @@ * limitations under the License. */ + +#ifndef METAWEARWRAPPERBASE_H +#define METAWEARWRAPPERBASE_H + #include #include @@ -23,9 +27,6 @@ #include "metawear/sensor/magnetometer_bmm150.h" #include "metawear/core/datasignal.h" -#ifndef METAWEARWRAPPERBASE_H -#define METAWEARWRAPPERBASE_H - class MetawearWrapperBase : public QObject { Q_OBJECT diff --git a/common/util.h b/common/util.h index ad30dae..6bf3ff6 100644 --- a/common/util.h +++ b/common/util.h @@ -16,14 +16,15 @@ #ifndef UTIL_H #define UTIL_H - - #include +#include + class Util { public: uint64_t stringToMac(std::string const& address); + QString formatLongtoMac(unsigned long long BluetoothAddress); private: Util(); }; diff --git a/forms/deviceselectdialog.cpp b/forms/deviceselectdialog.cpp index 63e79aa..5eeb823 100644 --- a/forms/deviceselectdialog.cpp +++ b/forms/deviceselectdialog.cpp @@ -21,45 +21,46 @@ #include #include #include +#include "common/DiscoveryAgent.h" DeviceSelectDialog::DeviceSelectDialog(QWidget *parent) : QDialog(parent), ui(new Ui::DeviceSelectDialog), - m_deviceInfo(QMap()), + m_deviceInfo(), localDevice(new QBluetoothLocalDevice), - m_deviceBlackList(){ + m_deviceBlackList(), + m_discoveryAgent(new DiscoveryAgent()){ ui->setupUi(this); - discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); - discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry); - connect(ui->scan, &QPushButton::clicked, this,[=](){ ui->scan->setEnabled(false); - discoveryAgent->start(); + m_discoveryAgent->start(); }); connect(ui->clear, &QPushButton::clicked,this,[=](){ ui->deviceList->clear(); }); - connect(discoveryAgent,&QBluetoothDeviceDiscoveryAgent::finished, this, [=](){ui->scan->setEnabled(true); }); + // connect(m_discoveryAgent,&QBluetoothDeviceDiscoveryAgent::finished, this, [=](){ui->scan->setEnabled(true); }); // An entry is added for every device connected - connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this,[=](const QBluetoothDeviceInfo& info){ + qRegisterMetaType("BluetoothAddress"); + connect(m_discoveryAgent, &DiscoveryAgent::deviceDiscovered, this,[=](BluetoothAddress info){ //if(info.name() != "MetaWear") // return; - if(m_deviceBlackList.contains(info.address().toString())) + if(m_deviceBlackList.contains(info.getMac())) return; - QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name()); + + QString label = QString("%1 %2").arg(info.getMac()).arg(info.getTitle()); m_deviceInfo.insert(label, info); QList items = ui->deviceList->findItems(label, Qt::MatchExactly); if (items.empty()) { QListWidgetItem *item = new QListWidgetItem(label); - QBluetoothLocalDevice::Pairing lpairingStatus = localDevice->pairingStatus(info.address()); - if (lpairingStatus == QBluetoothLocalDevice::Paired || lpairingStatus == QBluetoothLocalDevice::AuthorizedPaired) { - item->setTextColor(QColor(Qt::green)); - } else { + //QBluetoothLocalDevice::Pairing lpairingStatus = localDevice->pairingStatus(info.address()); + //if (lpairingStatus == QBluetoothLocalDevice::Paired || lpairingStatus == QBluetoothLocalDevice::AuthorizedPaired) { + // item->setTextColor(QColor(Qt::green)); + //} else { item->setTextColor(QColor(Qt::black)); - } + //} ui->deviceList->addItem(item); } }); @@ -83,40 +84,40 @@ void DeviceSelectDialog::accept() { } -void DeviceSelectDialog::addDevice(const QBluetoothDeviceInfo &info) { +void DeviceSelectDialog::addDevice(const BluetoothAddress &info) { - if(info.name() != "MetaWear" || info.name() != "MetaBoot") - return; - if(m_deviceBlackList.contains(info.address().toString())) +// if(info.name() != "MetaWear" || info.name() != "MetaBoot") + // return; + if(m_deviceBlackList.contains(info.getMac())) return; - QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name()); + QString label = QString("%1 %2").arg(info.getMac()).arg(info.getTitle()); m_deviceInfo.insert(label, info); QList items = ui->deviceList->findItems(label, Qt::MatchExactly); if (items.empty()) { QListWidgetItem *item = new QListWidgetItem(label); - QBluetoothLocalDevice::Pairing lpairingStatus = localDevice->pairingStatus(info.address()); - if (lpairingStatus == QBluetoothLocalDevice::Paired || lpairingStatus == QBluetoothLocalDevice::AuthorizedPaired) { - item->setTextColor(QColor(Qt::green)); - } else { + //QBluetoothLocalDevice::Pairing lpairingStatus = localDevice->pairingStatus(info.getMac()); + //if (lpairingStatus == QBluetoothLocalDevice::Paired || lpairingStatus == QBluetoothLocalDevice::AuthorizedPaired) { + // item->setTextColor(QColor(Qt::green)); + //} else { item->setTextColor(QColor(Qt::black)); - } + //} ui->deviceList->addItem(item); } } -void DeviceSelectDialog::updateDeviceBlackList(const QList &info) +void DeviceSelectDialog::updateDeviceBlackList(const QList &info) { m_deviceBlackList.clear(); for(int i = 0; i < info.length(); ++i){ - m_deviceBlackList.append(info[i].address().toString()); + m_deviceBlackList.append(info[i].getMac()); } - QMap temp = m_deviceInfo; + QMap temp = m_deviceInfo; ui->deviceList->clear(); m_deviceInfo.clear(); - QMap::iterator i; + QMap::iterator i; for (auto i = temp.begin(); i != temp.end(); ++i){ addDevice(i.value()); } diff --git a/forms/deviceselectdialog.h b/forms/deviceselectdialog.h index c89402b..52e9da8 100644 --- a/forms/deviceselectdialog.h +++ b/forms/deviceselectdialog.h @@ -22,11 +22,11 @@ #include #include #include +#include "common/BluetoothAddress.h" -class QBluetoothDeviceDiscoveryAgent; class QBluetoothLocalDevice; class QListWidgetItem; - +class DiscoveryAgent; namespace Ui { class DeviceSelectDialog; } @@ -35,21 +35,21 @@ class DeviceSelectDialog : public QDialog { Q_OBJECT private: Ui::DeviceSelectDialog *ui; - QBluetoothDeviceDiscoveryAgent *discoveryAgent; + DiscoveryAgent* m_discoveryAgent; QBluetoothLocalDevice *localDevice; - QMap m_deviceInfo; + QMap m_deviceInfo; QList m_deviceBlackList; public: explicit DeviceSelectDialog(QWidget *parent = nullptr); ~DeviceSelectDialog(); - void addDevice(const QBluetoothDeviceInfo&); - void updateDeviceBlackList(const QList&); + void addDevice(const BluetoothAddress&); + void updateDeviceBlackList(const QList&); void accept(); signals: - void onBluetoothDeviceAccepted(const QBluetoothDeviceInfo &info); + void onBluetoothDeviceAccepted(const BluetoothAddress &info); }; #endif // DEVICESELECTDIALOG_H diff --git a/forms/mainwindow.cpp b/forms/mainwindow.cpp index 603c6b0..7220cac 100644 --- a/forms/mainwindow.cpp +++ b/forms/mainwindow.cpp @@ -38,9 +38,9 @@ MainWindow::MainWindow(QWidget *parent) DeviceSelectDialog dialog; connect(&dialog, &DeviceSelectDialog::onBluetoothDeviceAccepted, this, &MainWindow::registerDevice); - QList devices; - connectedDevices(devices); - dialog.updateDeviceBlackList(devices); + //QList devices; + //connectedDevices(devices); + //dialog.updateDeviceBlackList(devices); dialog.exec(); //m_deviceSelectDialog->show(); @@ -66,16 +66,17 @@ MainWindow::MainWindow(QWidget *parent) -SensorPanel* MainWindow::registerDevice(const QBluetoothDeviceInfo &info) { +SensorPanel* MainWindow::registerDevice(const BluetoothAddress &info) { m_deviceIndex++; - QBluetoothHostInfo host; - if(QBluetoothLocalDevice::allDevices().length() > 0 ){ - QList hosts = QBluetoothLocalDevice::allDevices(); - host = hosts[(m_deviceIndex % hosts.length())]; - } +// ignored for the moment +// QBluetoothHostInfo host; +// if(QBluetoothLocalDevice::allDevices().length() > 0 ){ +// QList hosts = QBluetoothLocalDevice::allDevices(); +// host = hosts[(m_deviceIndex % hosts.length())]; +// } - SensorPanel* panel = new SensorPanel(host,info,this); + SensorPanel* panel = new SensorPanel(info,this); connect(panel->getMetwareWrapper(),&MetawearWrapperBase::latestEpoch,this,[=](qint64 epoch){ if(panel->getOffset() == 0){ for(int x = 0; x < this->ui->sensorContainer->count();x++){ @@ -146,7 +147,7 @@ void MainWindow::stopCapture() MainWindow::~MainWindow() { delete ui; } -void MainWindow::connectedDevices(QList& devices) { +void MainWindow::connectedDevices(QList& devices) { QObjectList children = ui->sensorContainer->children(); for (int i = 0; i < ui->sensorContainer->count(); ++i) { SensorPanel* panel = dynamic_cast(ui->sensorContainer->itemAt(i)->widget()); diff --git a/forms/mainwindow.h b/forms/mainwindow.h index 08e0434..2c8279a 100644 --- a/forms/mainwindow.h +++ b/forms/mainwindow.h @@ -22,6 +22,7 @@ #include #include #include +#include "common/BluetoothAddress.h" namespace Ui { class MainWindow; @@ -43,8 +44,8 @@ class MainWindow : public QMainWindow { MainWindow(QWidget *parent = nullptr); virtual ~MainWindow(); - void connectedDevices(QList&); - SensorPanel *registerDevice(const QBluetoothDeviceInfo &info); + void connectedDevices(QList&); + SensorPanel *registerDevice(const BluetoothAddress &info); void startCapture(); void stopCapture(); diff --git a/forms/sensorpanel.cpp b/forms/sensorpanel.cpp index 21572e6..bc1f84c 100644 --- a/forms/sensorpanel.cpp +++ b/forms/sensorpanel.cpp @@ -30,12 +30,12 @@ #include #include -SensorPanel::SensorPanel(const QBluetoothHostInfo &local,const QBluetoothDeviceInfo &target, QWidget *parent) +SensorPanel::SensorPanel(const BluetoothAddress &target, QWidget *parent) : QWidget(parent), ui(new Ui::SensorPanel), m_settingUpdateTimer(this), m_currentDevice(target), m_plotUpdatetimer(), - m_wrapper(new MetawearWrapper(local,target)), + m_wrapper(new MetawearWrapper(target)), m_plotoffset(0), m_temporaryDir(nullptr), m_reconnectTimer(), @@ -49,12 +49,12 @@ SensorPanel::SensorPanel(const QBluetoothHostInfo &local,const QBluetoothDeviceI if(m_reconnectTimer.interval() > 20000){ this->deleteLater(); QMessageBox messageBox; - messageBox.critical(0,"Error",QString("Failed to connect to device: %0").arg(m_currentDevice.address().toString()) ); + messageBox.critical(0,"Error",QString("Failed to connect to device: %0").arg(m_currentDevice.getMac()) ); messageBox.setFixedSize(500,200); return; } m_reconnectTimer.setInterval(m_reconnectTimer.interval() * 2); - qDebug() << "trying to reconnect to " << m_currentDevice.address().toString() << " with timeout " << m_reconnectTimer.interval(); + qDebug() << "trying to reconnect to " << m_currentDevice.getMac() << " with timeout " << m_reconnectTimer.interval(); m_reconnectTimer.start(); }); @@ -125,8 +125,8 @@ SensorPanel::SensorPanel(const QBluetoothHostInfo &local,const QBluetoothDeviceI m_isReadyToCapture = true; }); - ui->deviceAddress->setText(m_currentDevice.address().toString()); - ui->sensorName->setText(m_currentDevice.address().toString()); + ui->deviceAddress->setText(m_currentDevice.getMac()); + ui->sensorName->setText(m_currentDevice.getMac()); QSharedPointer timeTicker(new QCPAxisTickerTime); timeTicker->setTimeFormat("%h:%m:%s"); @@ -214,7 +214,7 @@ void SensorPanel::setOffset(qint64 offset) { m_plotoffset = offset; } -qint64 SensorPanel::getOffset() +qint64 SensorPanel::getOffset() const { return m_plotoffset; } @@ -265,7 +265,7 @@ MetawearWrapperBase* SensorPanel::getMetwareWrapper() { return this->m_wrapper; } -QBluetoothDeviceInfo SensorPanel::getDeviceInfo() +const BluetoothAddress& SensorPanel::getDeviceInfo() const { return m_currentDevice; } diff --git a/forms/sensorpanel.h b/forms/sensorpanel.h index 755866d..c6f952e 100644 --- a/forms/sensorpanel.h +++ b/forms/sensorpanel.h @@ -24,16 +24,14 @@ #include #include #include - #include #include - +#include "common/BluetoothAddress.h" class MetawearWrapperBase; class MblMwMetaWearBoard; class QTimer; class DataBundle; - namespace Ui { class SensorPanel; } @@ -44,7 +42,7 @@ class SensorPanel : public QWidget { Ui::SensorPanel *ui; MetawearWrapperBase *m_wrapper; - QBluetoothDeviceInfo m_currentDevice; + BluetoothAddress m_currentDevice; qint64 m_plotoffset; @@ -63,18 +61,18 @@ class SensorPanel : public QWidget { void registerPlotHandlers(); void registerDataHandlers(); - void configureWrapper(QBluetoothDeviceInfo device); + void configureWrapper(BluetoothAddress device); public: - explicit SensorPanel(const QBluetoothHostInfo &local,const QBluetoothDeviceInfo &target, QWidget *parent = nullptr); + explicit SensorPanel(const BluetoothAddress &target, QWidget *parent = nullptr); virtual ~SensorPanel(); bool isReadyToCapture(); MetawearWrapperBase* getMetwareWrapper(); - QBluetoothDeviceInfo getDeviceInfo(); + const BluetoothAddress& getDeviceInfo() const; void setName(QString); void setOffset(qint64 offset); - qint64 getOffset(); + qint64 getOffset() const; void startCapture(QTemporaryDir* dir); void stopCapture(); void clearPlots(); diff --git a/platform/windows/common/DiscoveryAgent.cpp b/platform/windows/common/DiscoveryAgent.cpp index 004c6c7..14fb55c 100644 --- a/platform/windows/common/DiscoveryAgent.cpp +++ b/platform/windows/common/DiscoveryAgent.cpp @@ -3,35 +3,47 @@ // #include "common/DiscoveryAgent.h" +#include "common/util.h" +#include +#include +#include + +DiscoveryAgent::DiscoveryAgent() : BaseDiscoveryAgent() { + m_bleAdvertisementWatcher = ref new Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher(); + m_bleAdvertisementWatcher->ScanningMode = Bluetooth::Advertisement::BluetoothLEScanningMode::Active; + m_bleAdvertisementWatcher->Received += ref new Windows::Foundation::TypedEventHandler([=](Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher^ watcher,Bluetooth::Advertisement::BluetoothLEAdvertisementReceivedEventArgs^ eventArgs) { + + std::wostringstream ret; + ret << std::hex << std::setfill(L'0') + << std::setw(2) << ((eventArgs->BluetoothAddress >> (5 * 8)) & 0xff) << ":" + << std::setw(2) << ((eventArgs->BluetoothAddress >> (4 * 8)) & 0xff) << ":" + << std::setw(2) << ((eventArgs->BluetoothAddress >> (3 * 8)) & 0xff) << ":" + << std::setw(2) << ((eventArgs->BluetoothAddress >> (2 * 8)) & 0xff) << ":" + << std::setw(2) << ((eventArgs->BluetoothAddress >> (1 * 8)) & 0xff) << ":" + << std::setw(2) << ((eventArgs->BluetoothAddress >> (0 * 8)) & 0xff); + + std::wstring localname(eventArgs->Advertisement->LocalName->Begin()); + + QString mac = QString::fromWCharArray(ret.str().c_str()); + QString name = QString::fromWCharArray(localname.c_str()); + BluetoothAddress address(mac,name ); + qDebug() << "mac:" << mac << " name: " << name; + emit deviceDiscovered(address); + }); +} +DiscoveryAgent::~DiscoveryAgent() { -DiscoveryAgent::DiscoveryAgent() : BaseDiscoveryAgent(){ - Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); - - CoInitializeSecurity( - nullptr, // TODO: "O:BAG:BAD:(A;;0x7;;;PS)(A;;0x3;;;SY)(A;;0x7;;;BA)(A;;0x3;;;AC)(A;;0x3;;;LS)(A;;0x3;;;NS)" - -1, - nullptr, - nullptr, - RPC_C_AUTHN_LEVEL_DEFAULT, - RPC_C_IMP_LEVEL_IDENTIFY, - NULL, - EOAC_NONE, - nullptr); - - m_bleAdvertisementWatcher = ref new Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher(); - m_bleAdvertisementWatcher->ScanningMode = Bluetooth::Advertisement::BluetoothLEScanningMode::Active; - m_bleAdvertisementWatcher->Received += ref new Windows::Foundation::TypedEventHandler( - [=](Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher ^watcher, Bluetooth::Advertisement::BluetoothLEAdvertisementReceivedEventArgs^ eventArgs){ - - } } - void DiscoveryAgent::start() { - + qDebug() << "started advertisement"; + m_bleAdvertisementWatcher->Start(); } -DiscoveryAgent::~DiscoveryAgent() { +void DiscoveryAgent::stop() { + qDebug() << "stop advertisement"; + m_bleAdvertisementWatcher->Stop(); } + diff --git a/platform/windows/common/DiscoveryAgent.h b/platform/windows/common/DiscoveryAgent.h index 8897909..ab2a859 100644 --- a/platform/windows/common/DiscoveryAgent.h +++ b/platform/windows/common/DiscoveryAgent.h @@ -17,15 +17,15 @@ using namespace Platform; using namespace Windows::Devices; class DiscoveryAgent : public BaseDiscoveryAgent{ +Q_OBJECT private: Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher^ m_bleAdvertisementWatcher; - + public: DiscoveryAgent(); ~DiscoveryAgent(); - void start(); - + void stop(); }; #endif //SMART_DISCOVERYAGENT_H diff --git a/platform/windows/common/metawearwrapper.cpp b/platform/windows/common/metawearwrapper.cpp index 506d074..62d109e 100644 --- a/platform/windows/common/metawearwrapper.cpp +++ b/platform/windows/common/metawearwrapper.cpp @@ -120,26 +120,13 @@ void MetawearWrapper::on_disconnect(void *context, const void *caller, MblMwFnVo -MetawearWrapper::MetawearWrapper(const QBluetoothHostInfo &local,const QBluetoothDeviceInfo &target): - MetawearWrapperBase::MetawearWrapperBase(), m_discover_device_event() ,m_event_set(m_discover_device_event){ +MetawearWrapper::MetawearWrapper(const BluetoothAddress &target): + MetawearWrapperBase::MetawearWrapperBase(){ - connect(this, &MetawearWrapper::onSensorConfigured, &MetawearWrapper::configureHandlers); + connect(this, &MetawearWrapper::onSensorConfigured,this, &MetawearWrapper::configureHandlers,Qt::QueuedConnection); - Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); - - CoInitializeSecurity( - nullptr, // TODO: "O:BAG:BAD:(A;;0x7;;;PS)(A;;0x3;;;SY)(A;;0x7;;;BA)(A;;0x3;;;AC)(A;;0x3;;;LS)(A;;0x3;;;NS)" - -1, - nullptr, - nullptr, - RPC_C_AUTHN_LEVEL_DEFAULT, - RPC_C_IMP_LEVEL_IDENTIFY, - NULL, - EOAC_NONE, - nullptr); - - std::string mac_copy(target.address().toString().toStdString()); + std::string mac_copy(target.getMac().toStdString()); mac_copy.erase(2, 1); mac_copy.erase(4, 1); mac_copy.erase(6, 1); @@ -153,7 +140,6 @@ MetawearWrapper::MetawearWrapper(const QBluetoothHostInfo &local,const QBluetoot create_task(BluetoothLEDevice::FromBluetoothAddressAsync(mac_ulong)).then([=](BluetoothLEDevice^ leDevice) { if (leDevice == nullptr) { qWarning() << "Failed to discover device"; - } else { leDevice->ConnectionStatusChanged += ref new TypedEventHandler([=](BluetoothLEDevice^ sender, Platform::Object^ args) { @@ -166,14 +152,25 @@ MetawearWrapper::MetawearWrapper(const QBluetoothHostInfo &local,const QBluetoot } }); this->m_device = leDevice; - m_discover_device_event.set(); + this->startDiscovery(); } }); - m_event_set.then([=]() { - qDebug() << "Started Gatt service Async"; - return create_task(this->m_device->GetGattServicesAsync(BluetoothCacheMode::Uncached)); - }).then([=](GattDeviceServicesResult^ result) { + /* .then([](task previous) { + try { + previous.wait(); + } catch (const exception& e) { + qWarning() << QString(e->Message->Data()); + } catch (Exception^ e) { + qWarning() << QString(e->Message->Data()); + } + });*/ + +} + +void MetawearWrapper::startDiscovery(){ + qDebug() << "Started Discovery"; + create_task(this->m_device->GetGattServicesAsync(BluetoothCacheMode::Uncached)).then([=](GattDeviceServicesResult^ result) { if (result->Status == GattCommunicationStatus::Success) { std::vector> find_gattchar_tasks; for (uint x = 0; x < result->Services->Size; ++x) { @@ -206,19 +203,9 @@ MetawearWrapper::MetawearWrapper(const QBluetoothHostInfo &local,const QBluetoot btleConnection.enable_notifications = enable_char_notify; btleConnection.on_disconnect = on_disconnect; this->m_metaWearBoard = mbl_mw_metawearboard_create(&btleConnection); - QMetaObject::invokeMethod(this, "configureHandlers", Qt::QueuedConnection); emit this->onSensorConfigured(); //this->configureHandlers(); - }); /* .then([](task previous) { - try { - previous.wait(); - } catch (const exception& e) { - qWarning() << QString(e->Message->Data()); - } catch (Exception^ e) { - qWarning() << QString(e->Message->Data()); - } - });*/ - + }).wait(); } MetawearWrapper::~MetawearWrapper() { diff --git a/platform/windows/common/metawearwrapper.h b/platform/windows/common/metawearwrapper.h index 617d8f0..105f0a8 100644 --- a/platform/windows/common/metawearwrapper.h +++ b/platform/windows/common/metawearwrapper.h @@ -16,6 +16,7 @@ #include #include #include +#include "common/BluetoothAddress.h" using namespace concurrency; using namespace Windows::Devices::Bluetooth; @@ -23,8 +24,6 @@ using namespace Windows::Devices::Bluetooth::Advertisement; using namespace Windows::Devices::Bluetooth::GenericAttributeProfile; using namespace Windows::Security::Cryptography; -static Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); - class MetawearWrapper : public MetawearWrapperBase { Q_OBJECT private: @@ -43,9 +42,7 @@ Q_OBJECT std::unordered_map m_services; std::unordered_map m_characterstics; - - BluetoothLEDevice^ m_device; - int m_readyCharacteristicCount; + BluetoothLEDevice^ m_device; MblMwFnIntVoidPtrArray m_readGattHandler; MblMwFnVoidVoidPtrInt m_disconnectedHandler; @@ -53,8 +50,7 @@ Q_OBJECT GattCharacteristic^ findCharacterstic( uint64_t low, uint64_t high); - task_completion_event m_discover_device_event; - task m_event_set; + void startDiscovery(); protected: @@ -72,10 +68,8 @@ Q_OBJECT static void on_disconnect(void *context, const void *caller, MblMwFnVoidVoidPtrInt handler); - - public: - MetawearWrapper(const QBluetoothHostInfo &local, const QBluetoothDeviceInfo &target); + MetawearWrapper( const BluetoothAddress &target); ~MetawearWrapper(); signals: void onSensorConfigured(); From e4b685c8577c1e4e292635bb4c4409ddc769d79a Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Tue, 28 Aug 2018 23:02:06 -0700 Subject: [PATCH 3/5] updated wrapper and discovery agent --- forms/deviceselectdialog.cpp | 6 ++ platform/windows/common/DiscoveryAgent.cpp | 2 +- platform/windows/common/metawearwrapper.cpp | 77 +++++++++++---------- 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/forms/deviceselectdialog.cpp b/forms/deviceselectdialog.cpp index 5eeb823..8d238e9 100644 --- a/forms/deviceselectdialog.cpp +++ b/forms/deviceselectdialog.cpp @@ -37,6 +37,7 @@ DeviceSelectDialog::DeviceSelectDialog(QWidget *parent) }); connect(ui->clear, &QPushButton::clicked,this,[=](){ ui->deviceList->clear(); + m_discoveryAgent->stop(); }); // connect(m_discoveryAgent,&QBluetoothDeviceDiscoveryAgent::finished, this, [=](){ui->scan->setEnabled(true); }); @@ -77,6 +78,11 @@ DeviceSelectDialog::~DeviceSelectDialog() { delete ui; } void DeviceSelectDialog::accept() { if (ui->deviceList->count() == 0) return; + + + m_discoveryAgent->stop(); + ui->scan->setEnabled(true); + QListWidgetItem *currentItem = ui->deviceList->currentItem(); QString text = currentItem->text(); emit onBluetoothDeviceAccepted(m_deviceInfo.value(text)); diff --git a/platform/windows/common/DiscoveryAgent.cpp b/platform/windows/common/DiscoveryAgent.cpp index 14fb55c..cc3236b 100644 --- a/platform/windows/common/DiscoveryAgent.cpp +++ b/platform/windows/common/DiscoveryAgent.cpp @@ -27,7 +27,7 @@ DiscoveryAgent::DiscoveryAgent() : BaseDiscoveryAgent() { QString mac = QString::fromWCharArray(ret.str().c_str()); QString name = QString::fromWCharArray(localname.c_str()); BluetoothAddress address(mac,name ); - qDebug() << "mac:" << mac << " name: " << name; +// qDebug() << "mac:" << mac << " name: " << name; emit deviceDiscovered(address); }); } diff --git a/platform/windows/common/metawearwrapper.cpp b/platform/windows/common/metawearwrapper.cpp index 62d109e..1d60dcc 100644 --- a/platform/windows/common/metawearwrapper.cpp +++ b/platform/windows/common/metawearwrapper.cpp @@ -123,9 +123,6 @@ void MetawearWrapper::on_disconnect(void *context, const void *caller, MblMwFnVo MetawearWrapper::MetawearWrapper(const BluetoothAddress &target): MetawearWrapperBase::MetawearWrapperBase(){ - connect(this, &MetawearWrapper::onSensorConfigured,this, &MetawearWrapper::configureHandlers,Qt::QueuedConnection); - - std::string mac_copy(target.getMac().toStdString()); mac_copy.erase(2, 1); mac_copy.erase(4, 1); @@ -170,42 +167,46 @@ MetawearWrapper::MetawearWrapper(const BluetoothAddress &target): void MetawearWrapper::startDiscovery(){ qDebug() << "Started Discovery"; - create_task(this->m_device->GetGattServicesAsync(BluetoothCacheMode::Uncached)).then([=](GattDeviceServicesResult^ result) { - if (result->Status == GattCommunicationStatus::Success) { - std::vector> find_gattchar_tasks; - for (uint x = 0; x < result->Services->Size; ++x) { - auto service = result->Services->GetAt(x); - m_services.emplace(service->Uuid, service); - find_gattchar_tasks.push_back(create_task(service->GetCharacteristicsAsync(BluetoothCacheMode::Uncached))); - } - return when_all(std::begin(find_gattchar_tasks), std::end(find_gattchar_tasks)); - } - qWarning() << "Failed fo discover Gatt service"; - - }).then([=](std::vector results) { - for (auto it : results) { - if (it->Status == GattCommunicationStatus::Success) { - for (uint x = 0; x < it->Characteristics->Size; ++x) { - auto chr = it->Characteristics->GetAt(x); - m_characterstics.emplace(chr->Uuid, chr); - } - } - else { - qWarning() << "Failed to discover gatt charactersitic (status = " << static_cast(it->Status) << ")"; - } - } - qDebug() << "Configuring Mbientsensor"; - MblMwBtleConnection btleConnection; - btleConnection.context = this; - btleConnection.write_gatt_char = write_gatt_char; - btleConnection.read_gatt_char = read_gatt_char; - btleConnection.enable_notifications = enable_char_notify; - btleConnection.on_disconnect = on_disconnect; - this->m_metaWearBoard = mbl_mw_metawearboard_create(&btleConnection); - emit this->onSensorConfigured(); - //this->configureHandlers(); - }).wait(); + create_task(this->m_device->GetGattServicesAsync(BluetoothCacheMode::Uncached)).then( + [=](GattDeviceServicesResult^result) { + if (result->Status == GattCommunicationStatus::Success) { + std::vector> + find_gattchar_tasks; + for (uint x = 0; x < result->Services->Size; ++x) { + auto service = result->Services->GetAt(x); + m_services.emplace(service->Uuid, service); + find_gattchar_tasks.push_back( + create_task(service->GetCharacteristicsAsync(BluetoothCacheMode::Uncached))); + } + return when_all(std::begin(find_gattchar_tasks), std::end(find_gattchar_tasks)); + } + qWarning() << "Failed fo discover Gatt service"; + + }).then([=](std::vector results) { + for (auto it : results) { + if (it->Status == GattCommunicationStatus::Success) { + for (uint x = 0; x < it->Characteristics->Size; ++x) { + auto chr = it->Characteristics->GetAt(x); + m_characterstics.emplace(chr->Uuid, chr); + } + } else { + qWarning() << "Failed to discover gatt charactersitic (status = " << static_cast(it->Status) + << ")"; + } + } + + qDebug() << "Configuring Mbientsensor"; + MblMwBtleConnection btleConnection; + btleConnection.context = this; + btleConnection.write_gatt_char = write_gatt_char; + btleConnection.read_gatt_char = read_gatt_char; + btleConnection.enable_notifications = enable_char_notify; + btleConnection.on_disconnect = on_disconnect; + this->m_metaWearBoard = mbl_mw_metawearboard_create(&btleConnection); + this->configureHandlers(); + }).wait(); + } MetawearWrapper::~MetawearWrapper() { From 53980fc2ec8652a13d23bd66d9dc540588b0242d Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 29 Aug 2018 10:32:34 -0700 Subject: [PATCH 4/5] configured connection setup --- CMakeLists.txt | 5 ++--- forms/deviceselectdialog.cpp | 5 ----- forms/deviceselectdialog.h | 4 ---- forms/mainwindow.h | 3 +-- forms/sensorpanel.cpp | 12 ++++++------ forms/sensorpanel.h | 5 +---- platform/windows/common/metawearwrapper.cpp | 21 +++++++++++---------- platform/windows/common/metawearwrapper.h | 4 ++-- 8 files changed, 23 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70c5a62..4fcbba9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ if(WIN32) endif() include_directories(./) -find_package(Qt5 COMPONENTS Core Widgets Quick PrintSupport Bluetooth REQUIRED) +find_package(Qt5 COMPONENTS Core Widgets Quick PrintSupport REQUIRED) set (SOURCES qcustomplot.cpp @@ -114,7 +114,6 @@ if (MSVC) $ $ $ - $ $ $ ) @@ -123,6 +122,6 @@ if (MSVC) endif(MSVC) # Add the Qt5 Widgets for linking -target_link_libraries(SMART Qt5::Gui Qt5::Widgets Qt5::Core Qt5::Quick Qt5::Bluetooth Qt5::PrintSupport ) +target_link_libraries(SMART Qt5::Gui Qt5::Widgets Qt5::Core Qt5::Quick Qt5::PrintSupport ) target_link_libraries(SMART ${QUAZIP_LIB_TARGET_NAME}) target_link_libraries(SMART extern_metawear) \ No newline at end of file diff --git a/forms/deviceselectdialog.cpp b/forms/deviceselectdialog.cpp index 8d238e9..8d06d3a 100644 --- a/forms/deviceselectdialog.cpp +++ b/forms/deviceselectdialog.cpp @@ -17,16 +17,11 @@ #include "deviceselectdialog.h" #include "ui_deviceselectdialog.h" #include -#include -#include -#include -#include #include "common/DiscoveryAgent.h" DeviceSelectDialog::DeviceSelectDialog(QWidget *parent) : QDialog(parent), ui(new Ui::DeviceSelectDialog), m_deviceInfo(), - localDevice(new QBluetoothLocalDevice), m_deviceBlackList(), m_discoveryAgent(new DiscoveryAgent()){ ui->setupUi(this); diff --git a/forms/deviceselectdialog.h b/forms/deviceselectdialog.h index 52e9da8..0bf23e5 100644 --- a/forms/deviceselectdialog.h +++ b/forms/deviceselectdialog.h @@ -17,14 +17,11 @@ #ifndef DEVICESELECTDIALOG_H #define DEVICESELECTDIALOG_H -#include -#include #include #include #include #include "common/BluetoothAddress.h" -class QBluetoothLocalDevice; class QListWidgetItem; class DiscoveryAgent; namespace Ui { @@ -36,7 +33,6 @@ class DeviceSelectDialog : public QDialog { private: Ui::DeviceSelectDialog *ui; DiscoveryAgent* m_discoveryAgent; - QBluetoothLocalDevice *localDevice; QMap m_deviceInfo; QList m_deviceBlackList; diff --git a/forms/mainwindow.h b/forms/mainwindow.h index 2c8279a..1263b1f 100644 --- a/forms/mainwindow.h +++ b/forms/mainwindow.h @@ -17,8 +17,7 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include -#include + #include #include #include diff --git a/forms/sensorpanel.cpp b/forms/sensorpanel.cpp index bc1f84c..d62e4af 100644 --- a/forms/sensorpanel.cpp +++ b/forms/sensorpanel.cpp @@ -134,14 +134,14 @@ SensorPanel::SensorPanel(const BluetoothAddress &target, QWidget *parent) ui->plot->axisRect()->setupFullAxesBox(); ui->plot->yAxis->setRange(-2, 2); - connect(this->m_wrapper, &MetawearWrapper::batteryPercentage, - [=](qint8 amount) { this->ui->battery->setValue(amount); }); + connect(this->m_wrapper, &MetawearWrapper::batteryPercentage,this, + [=](qint8 amount) { this->ui->battery->setValue(amount); },Qt::QueuedConnection); // read the battery status every minute m_settingUpdateTimer.setInterval(60000); - connect(&m_settingUpdateTimer, &QTimer::timeout, - [=]() { this->m_wrapper->readBatteryStatus(); }); + connect(&m_settingUpdateTimer, &QTimer::timeout,this, + [=]() { this->m_wrapper->readBatteryStatus(); },Qt::QueuedConnection); connect(&m_plotUpdatetimer,&QTimer::timeout,this,[=](){ if(this->m_plotLock.tryLock(100)){ @@ -155,7 +155,7 @@ SensorPanel::SensorPanel(const BluetoothAddress &target, QWidget *parent) this->m_plotLock.unlock(); this->ui->plot->replot(); } - }); + },Qt::QueuedConnection); m_plotUpdatetimer.setInterval(50); m_plotUpdatetimer.start(); } @@ -179,7 +179,7 @@ void SensorPanel::registerPlotHandlers() ygraphAcc->addData(epoch - m_plotoffset, static_cast(y)); zgraphAcc->addData(epoch - m_plotoffset, static_cast(z)); this->m_plotLock.unlock(); - }); + },Qt::QueuedConnection); } diff --git a/forms/sensorpanel.h b/forms/sensorpanel.h index c6f952e..95478f8 100644 --- a/forms/sensorpanel.h +++ b/forms/sensorpanel.h @@ -17,9 +17,6 @@ #ifndef SENSORPANEL_H #define SENSORPANEL_H -#include -#include -#include #include #include #include @@ -80,7 +77,7 @@ class SensorPanel : public QWidget { signals: void connected(); void disconnect(); - void bluetoothError(QLowEnergyController::Error e); + //void bluetoothError(QLowEnergyController::Error e); void metawearInitilized(); }; diff --git a/platform/windows/common/metawearwrapper.cpp b/platform/windows/common/metawearwrapper.cpp index 1d60dcc..7f80fb5 100644 --- a/platform/windows/common/metawearwrapper.cpp +++ b/platform/windows/common/metawearwrapper.cpp @@ -141,15 +141,17 @@ MetawearWrapper::MetawearWrapper(const BluetoothAddress &target): else { leDevice->ConnectionStatusChanged += ref new TypedEventHandler([=](BluetoothLEDevice^ sender, Platform::Object^ args) { switch (sender->ConnectionStatus) { - case BluetoothConnectionStatus::Disconnected: - qWarning() << "Failed to connect to device"; - this->cleanup(); - - break; - } + case BluetoothConnectionStatus::Disconnected: + qWarning() << "Failed to connect to device"; + this->cleanup(); + break; + case BluetoothConnectionStatus::Connected: + qDebug() << "Device Connected"; + this->m_device = leDevice; + this->startDiscovery(); + break; + } }); - this->m_device = leDevice; - this->startDiscovery(); } }); @@ -171,8 +173,7 @@ void MetawearWrapper::startDiscovery(){ create_task(this->m_device->GetGattServicesAsync(BluetoothCacheMode::Uncached)).then( [=](GattDeviceServicesResult^result) { if (result->Status == GattCommunicationStatus::Success) { - std::vector> - find_gattchar_tasks; + std::vector> find_gattchar_tasks; for (uint x = 0; x < result->Services->Size; ++x) { auto service = result->Services->GetAt(x); m_services.emplace(service->Uuid, service); diff --git a/platform/windows/common/metawearwrapper.h b/platform/windows/common/metawearwrapper.h index 105f0a8..6b166ac 100644 --- a/platform/windows/common/metawearwrapper.h +++ b/platform/windows/common/metawearwrapper.h @@ -74,9 +74,9 @@ Q_OBJECT signals: void onSensorConfigured(); - void controllerError(QLowEnergyController::Error); + // void controllerError(QLowEnergyController::Error); - void characteristicError(QLowEnergyService::ServiceError); + // void characteristicError(QLowEnergyService::ServiceError); }; From f3b18b22df0674db3ceac04d0fed0251ec175f82 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 29 Aug 2018 11:16:57 -0700 Subject: [PATCH 5/5] updated wrapper and fixed null exception where device is deleted --- forms/sensorpanel.cpp | 4 +- platform/windows/common/metawearwrapper.cpp | 94 ++++++++++----------- 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/forms/sensorpanel.cpp b/forms/sensorpanel.cpp index d62e4af..a365368 100644 --- a/forms/sensorpanel.cpp +++ b/forms/sensorpanel.cpp @@ -270,7 +270,9 @@ const BluetoothAddress& SensorPanel::getDeviceInfo() const return m_currentDevice; } -SensorPanel::~SensorPanel() { delete ui; } +SensorPanel::~SensorPanel() { + delete m_wrapper; + delete ui; } bool SensorPanel::isReadyToCapture() { diff --git a/platform/windows/common/metawearwrapper.cpp b/platform/windows/common/metawearwrapper.cpp index 7f80fb5..437da9c 100644 --- a/platform/windows/common/metawearwrapper.cpp +++ b/platform/windows/common/metawearwrapper.cpp @@ -143,19 +143,14 @@ MetawearWrapper::MetawearWrapper(const BluetoothAddress &target): switch (sender->ConnectionStatus) { case BluetoothConnectionStatus::Disconnected: qWarning() << "Failed to connect to device"; - this->cleanup(); - break; - case BluetoothConnectionStatus::Connected: - qDebug() << "Device Connected"; - this->m_device = leDevice; - this->startDiscovery(); + // this->cleanup(); break; } }); + this->m_device = leDevice; + this->startDiscovery(); } - }); - - /* .then([](task previous) { + });/* .then([](task previous) { try { previous.wait(); } catch (const exception& e) { @@ -167,56 +162,61 @@ MetawearWrapper::MetawearWrapper(const BluetoothAddress &target): } -void MetawearWrapper::startDiscovery(){ - qDebug() << "Started Discovery"; - - create_task(this->m_device->GetGattServicesAsync(BluetoothCacheMode::Uncached)).then( - [=](GattDeviceServicesResult^result) { - if (result->Status == GattCommunicationStatus::Success) { - std::vector> find_gattchar_tasks; - for (uint x = 0; x < result->Services->Size; ++x) { - auto service = result->Services->GetAt(x); - m_services.emplace(service->Uuid, service); - find_gattchar_tasks.push_back( - create_task(service->GetCharacteristicsAsync(BluetoothCacheMode::Uncached))); +void MetawearWrapper::startDiscovery() { + qDebug() << "Started Discovery"; + try { + create_task(this->m_device->GetGattServicesAsync(BluetoothCacheMode::Uncached)).then( + [=](GattDeviceServicesResult^result) { + if (result->Status == GattCommunicationStatus::Success) { + std::vector> find_gattchar_tasks; + for (uint x = 0; x < result->Services->Size; ++x) { + auto service = result->Services->GetAt(x); + m_services.emplace(service->Uuid, service); + find_gattchar_tasks.push_back( + create_task(service->GetCharacteristicsAsync(BluetoothCacheMode::Uncached))); + } + return when_all(std::begin(find_gattchar_tasks), std::end(find_gattchar_tasks)); } - return when_all(std::begin(find_gattchar_tasks), std::end(find_gattchar_tasks)); - } - qWarning() << "Failed fo discover Gatt service"; - - }).then([=](std::vector results) { - for (auto it : results) { - if (it->Status == GattCommunicationStatus::Success) { - for (uint x = 0; x < it->Characteristics->Size; ++x) { - auto chr = it->Characteristics->GetAt(x); - m_characterstics.emplace(chr->Uuid, chr); + qWarning() << "Failed fo discover Gatt service"; + throw ref new Platform::Exception(-1, "Failed fo discover Gatt service"); + }).then([=](std::vector results) { + for (auto it : results) { + if (it->Status == GattCommunicationStatus::Success) { + for (uint x = 0; x < it->Characteristics->Size; ++x) { + auto chr = it->Characteristics->GetAt(x); + m_characterstics.emplace(chr->Uuid, chr); + } + } else { + qWarning() << "Failed to discover gatt charactersitic (status = " << static_cast(it->Status) << ")"; + throw ref new Platform::Exception(-1, "Failed to discover gatt charactersitic"); } - } else { - qWarning() << "Failed to discover gatt charactersitic (status = " << static_cast(it->Status) - << ")"; } - } - - qDebug() << "Configuring Mbientsensor"; - MblMwBtleConnection btleConnection; - btleConnection.context = this; - btleConnection.write_gatt_char = write_gatt_char; - btleConnection.read_gatt_char = read_gatt_char; - btleConnection.enable_notifications = enable_char_notify; - btleConnection.on_disconnect = on_disconnect; - this->m_metaWearBoard = mbl_mw_metawearboard_create(&btleConnection); - this->configureHandlers(); - }).wait(); + qDebug() << "Configuring Mbientsensor"; + MblMwBtleConnection btleConnection; + btleConnection.context = this; + btleConnection.write_gatt_char = write_gatt_char; + btleConnection.read_gatt_char = read_gatt_char; + btleConnection.enable_notifications = enable_char_notify; + btleConnection.on_disconnect = on_disconnect; + this->m_metaWearBoard = mbl_mw_metawearboard_create(&btleConnection); + this->configureHandlers(); + }).wait(); + } + catch (Platform::Exception^e) { + qWarning() << QString::fromWCharArray(e->Message->Data()); + this->cleanup(); + } } MetawearWrapper::~MetawearWrapper() { cleanup(); - } void MetawearWrapper::cleanup() { + qDebug() << "Cleanup"; + for (auto it : m_characterstics) delete it.second; for (auto it : m_services)