[bluemonkey] - more awesome ble code
authorKevron Rees <tripzero.kev@gmail.com>
Thu, 26 Feb 2015 18:08:44 +0000 (10:08 -0800)
committerKevron Rees <tripzero.kev@gmail.com>
Thu, 26 Feb 2015 18:08:44 +0000 (10:08 -0800)
README.md
plugins/bluemonkey/ble.cpp
plugins/bluemonkey/ble.h
plugins/bluemonkey/bmconfig.js

index f3373a6..5f35649 100644 (file)
--- a/README.md
+++ b/README.md
@@ -60,7 +60,7 @@ You will also need to edit your config to enable the Qt-based mainloop:
 
 ~~~~~~~~~~~~~{.json}
 {
-       "mainloop" : "/usr/lib/i386-linux-gnu/automotive-message-broker/qtmainloopplugin.so",
+       "mainloop" : "/usr/lib/x86_64-linux-gnu/automotive-message-broker/qtmainloopplugin.so",
        "plugins" : "/etc/ambd/plugins.d"
 }
 ~~~~~~~~~~~~~
index b6b836c..4f50982 100644 (file)
@@ -78,10 +78,16 @@ bool Ble::scanning()
        return mDeviceDiscoveryAgent->isActive();
 }
 
+void Ble::errorHandle(QLowEnergyController::Error err)
+{
+       error((int)err);
+}
+
 void Ble::deviceDiscovered(const QBluetoothDeviceInfo &device)
 {
        if(device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration)
        {
+               leDeviceFound(device.name(), device.address().toString());
                //qDebug() << "BLE device found: " << device.address().toString();
 
                ///We have a device.  Let's scan to see if it supports the service Uuid's we want
@@ -89,7 +95,7 @@ void Ble::deviceDiscovered(const QBluetoothDeviceInfo &device)
 
                connect(control, &QLowEnergyController::stateChanged, [control, this](QLowEnergyController::ControllerState state)
                {
-                       //qDebug() << "Controller state changed for device: " << control->remoteAddress().toString() << state;
+                       qDebug() << "Controller state changed for device: " << control->remoteAddress().toString() << state;
                        if(state == QLowEnergyController::DiscoveredState)
                        {
                                devicesChanged();
@@ -98,14 +104,15 @@ void Ble::deviceDiscovered(const QBluetoothDeviceInfo &device)
                connect(control, &QLowEnergyController::disconnected, control, &QLowEnergyController::deleteLater); // if we disconnect, clean up.
                connect(control, &QLowEnergyController::connected, [&control]()
                {
-                       //qDebug() << "Device connected. Discovering services...";
+                       qDebug() << "Device connected. Discovering services...";
                        control->discoverServices();
                });
+               connect(control, SIGNAL(error(QLowEnergyController::Error)), this, SLOT(errorHandle(QLowEnergyController::Error)));
                connect(control, &QLowEnergyController::serviceDiscovered, [&control, this, device](const QBluetoothUuid & uuid)
                {
                        Q_FOREACH(ServiceIdentifier * service, services)
                        {
-                               //qDebug() << "checking if service uuid (" << uuid.toString() << ") matches the one we want (" << service->serviceUuid << ")";
+                               qDebug() << "checking if service uuid (" << uuid.toString() << ") matches the one we want (" << service->serviceUuid << ")";
                                if(service->serviceUuid == uuid.toString())
                                {
                                        service->service = control->createServiceObject(uuid, service);
@@ -150,6 +157,7 @@ void Ble::deviceDiscovered(const QBluetoothDeviceInfo &device)
                                }
                        }
                });
+               qDebug() << "Attempting to connect...";
                control->connectToDevice();
        }
 }
index 70bcaa8..b855c53 100644 (file)
@@ -8,6 +8,7 @@
 #include <QJSValue>
 
 #include <QBluetoothDeviceInfo>
+#include <QLowEnergyController>
 
 class QBluetoothDeviceDiscoveryAgent;
 class QLowEnergyService;
@@ -16,9 +17,16 @@ class QLowEnergyCharacteristic;
 class ServiceIdentifier : public QObject
 {
        Q_OBJECT
+       Q_PROPERTY(QString deviceName READ getDeviceName)
+       Q_PROPERTY(QString deviceName READ getDeviceAddress)
+
 public:
        ServiceIdentifier(const QString & s, const QString & r, const QString & t, QObject *parent = nullptr)
                :QObject(parent), serviceUuid(s), rxUuid(r), txUuid(t), service(nullptr), isValid(false) {}
+
+       QString getDeviceName() { return deviceName; }
+       QString getDeviceAddress() { return deviceAddress; }
+
        QString serviceUuid;
        QString rxUuid;
        QString txUuid;
@@ -63,9 +71,11 @@ public Q_SLOTS:
 Q_SIGNALS:
        void scanningChanged();
        void devicesChanged();
+       void leDeviceFound(QString name, QString address);
+       void error(int);
 
 private Q_SLOTS:
-
+       void errorHandle(QLowEnergyController::Error err);
        void deviceDiscovered(const QBluetoothDeviceInfo & device);
 
 private:
index 32f52fd..c1328c9 100755 (executable)
@@ -40,7 +40,7 @@ app.main = function()
        if(ble !== undefined)
        {
                serviceUuid = "5faaf494-d4c6-483e-b592-d1a6ffd436c9";
-               device = ble.addService(serviceUuid, "5faaf495-d4c6-483e-b592-d1a6ffd436c9", "5faaf496-d4c6-483e-b592-d1a6ffd436c9");
+               ble.addService(serviceUuid, "5faaf495-d4c6-483e-b592-d1a6ffd436c9", "5faaf496-d4c6-483e-b592-d1a6ffd436c9");
                ble.scanningChanged.connect(function ()
                {
                        if(!ble.scan)