Skip to content

Commit

Permalink
Merge branch 'feature-discover-connection-wrapper'
Browse files Browse the repository at this point in the history
- remove qtbluetooth
- updated wrapper and cleaned up crashing issues
  • Loading branch information
pollend committed Aug 29, 2018
2 parents 4371e34 + f3b18b2 commit 0e2f28a
Show file tree
Hide file tree
Showing 17 changed files with 346 additions and 180 deletions.
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +33,7 @@ set (SOURCES
common/main.cpp
common/metawearwrapperbase.cpp
common/util.cpp
)
common/BluetoothAddress.cpp)

set(HEADERS
qcustomplot.h
Expand All @@ -43,8 +43,8 @@ set(HEADERS

common/metawearwrapperbase.h
common/util.h

)
common/BaseDiscoveryAgent.h
common/BluetoothAddress.h)


set ( UIS
Expand Down Expand Up @@ -78,10 +78,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")

Expand Down Expand Up @@ -112,7 +114,6 @@ if (MSVC)
$<TARGET_FILE:Qt5::Core>
$<TARGET_FILE:Qt5::Gui>
$<TARGET_FILE:Qt5::Quick>
$<TARGET_FILE:Qt5::Bluetooth>
$<TARGET_FILE:Qt5::PrintSupport>
$<TARGET_FILE_DIR:SMART>
)
Expand All @@ -121,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)
30 changes: 30 additions & 0 deletions common/BaseDiscoveryAgent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Created by Michael on 8/27/2018.
//

#ifndef SMART_BASEDISCOVERYAGENT_H
#define SMART_BASEDISCOVERYAGENT_H

#include <QtCore/QObject>
#include "BluetoothAddress.h"

class BaseDiscoveryAgent : public QObject{
Q_OBJECT
private:
public:
BaseDiscoveryAgent(){

}
~BaseDiscoveryAgent(){

}

virtual void start() = 0;
virtual void stop() = 0;
signals:
void deviceDiscovered(BluetoothAddress address);
void finished();

};

#endif //SMART_BASEDISCOVERYAGENT_H
28 changes: 28 additions & 0 deletions common/BluetoothAddress.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

27 changes: 27 additions & 0 deletions common/BluetoothAddress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by Michael on 8/27/2018.
//

#ifndef SMART_BLUETOOTHADRESS_H
#define SMART_BLUETOOTHADRESS_H

#include <QString>

class BluetoothAddress{
private:
QString m_mac;
QString m_title;
public:
BluetoothAddress();
BluetoothAddress(const QString& mac, const QString& title);
BluetoothAddress(const BluetoothAddress& address);
BluetoothAddress(unsigned long long address, const QString& title);

~BluetoothAddress();

const QString& getTitle() const;
const QString& getMac() const;

};

#endif //SMART_BLUETOOTHADRESS_H
19 changes: 19 additions & 0 deletions common/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,29 @@
#include <QQmlApplicationEngine>
#include <QQmlContext>


#if(_WIN64)
#include <wrl/wrappers/corewrappers.h>
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();
Expand Down
7 changes: 4 additions & 3 deletions common/metawearwrapperbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* limitations under the License.
*/


#ifndef METAWEARWRAPPERBASE_H
#define METAWEARWRAPPERBASE_H

#include <QObject>

#include <metawear/core/cpp/metawearboard_def.h>
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

#ifndef UTIL_H
#define UTIL_H


#include <string>
#include <QtCore/QString>


class Util
{
public:
uint64_t stringToMac(std::string const& address);
QString formatLongtoMac(unsigned long long BluetoothAddress);
private:
Util();
};
Expand Down
70 changes: 36 additions & 34 deletions forms/deviceselectdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,46 @@
#include "deviceselectdialog.h"
#include "ui_deviceselectdialog.h"
#include <QListView>
#include <qbluetoothlocaldevice.h>
#include <qbluetoothservicediscoveryagent.h>
#include <qbluetoothserviceinfo.h>
#include <qbluetoothuuid.h>
#include "common/DiscoveryAgent.h"

DeviceSelectDialog::DeviceSelectDialog(QWidget *parent)
: QDialog(parent), ui(new Ui::DeviceSelectDialog),
m_deviceInfo(QMap<QString, QBluetoothDeviceInfo>()),
localDevice(new QBluetoothLocalDevice),
m_deviceBlackList(){
m_deviceInfo(),
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();
m_discoveryAgent->stop();
});
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>("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<QListWidgetItem *> 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);
}
});
Expand All @@ -76,47 +73,52 @@ 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));
QDialog::accept();
}


void DeviceSelectDialog::addDevice(const QBluetoothDeviceInfo &info) {
void DeviceSelectDialog::addDevice(const BluetoothAddress &info) {

if(info.name() != "MetaWear")
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<QListWidgetItem *> 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<QBluetoothDeviceInfo> &info)
void DeviceSelectDialog::updateDeviceBlackList(const QList<BluetoothAddress> &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<QString, QBluetoothDeviceInfo> temp = m_deviceInfo;
QMap<QString, BluetoothAddress> temp = m_deviceInfo;
ui->deviceList->clear();
m_deviceInfo.clear();

QMap<QString, QBluetoothDeviceInfo>::iterator i;
QMap<QString, BluetoothAddress>::iterator i;
for (auto i = temp.begin(); i != temp.end(); ++i){
addDevice(i.value());
}
Expand Down
18 changes: 7 additions & 11 deletions forms/deviceselectdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
#ifndef DEVICESELECTDIALOG_H
#define DEVICESELECTDIALOG_H

#include <QBluetoothDeviceInfo>
#include <QBluetoothLocalDevice>
#include <QDialog>
#include <QMap>
#include <QWizardPage>
#include "common/BluetoothAddress.h"

class QBluetoothDeviceDiscoveryAgent;
class QBluetoothLocalDevice;
class QListWidgetItem;

class DiscoveryAgent;
namespace Ui {
class DeviceSelectDialog;
}
Expand All @@ -35,21 +32,20 @@ class DeviceSelectDialog : public QDialog {
Q_OBJECT
private:
Ui::DeviceSelectDialog *ui;
QBluetoothDeviceDiscoveryAgent *discoveryAgent;
QBluetoothLocalDevice *localDevice;
QMap<QString, QBluetoothDeviceInfo> m_deviceInfo;
DiscoveryAgent* m_discoveryAgent;
QMap<QString, BluetoothAddress> m_deviceInfo;
QList<QString> m_deviceBlackList;

public:
explicit DeviceSelectDialog(QWidget *parent = nullptr);
~DeviceSelectDialog();

void addDevice(const QBluetoothDeviceInfo&);
void updateDeviceBlackList(const QList<QBluetoothDeviceInfo>&);
void addDevice(const BluetoothAddress&);
void updateDeviceBlackList(const QList<BluetoothAddress>&);
void accept();

signals:
void onBluetoothDeviceAccepted(const QBluetoothDeviceInfo &info);
void onBluetoothDeviceAccepted(const BluetoothAddress &info);
};

#endif // DEVICESELECTDIALOG_H
Loading

0 comments on commit 0e2f28a

Please sign in to comment.