Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
366 changes: 320 additions & 46 deletions Makefile

Large diffs are not rendered by default.

52 changes: 30 additions & 22 deletions ancs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "ancs.h"

void ANCS::onANCSCharacteristicChanged(QLowEnergyCharacteristic characteristic, QByteArray data) {
Q_ASSERT(leController);
Q_ASSERT(leController.get());

if (characteristic.uuid() == ancsNotificationSourceCharUUid)
{
Expand Down Expand Up @@ -58,19 +58,21 @@ void ANCS::onANCSCharacteristicChanged(QLowEnergyCharacteristic characteristic,

ANCS::ANCS(QObject *parent) : QObject(parent), ancsService(nullptr)
{
leController = QLowEnergyController::createPeripheral(this);
leController = LEController();
leController.setPeripheral(QLowEnergyController::createPeripheral(this));
leController.selectPeripheral();

QObject::connect(
leController,
leController.get(),
static_cast<void(QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
this,
&ANCS::leError
);

QObject::connect(leController, &QLowEnergyController::disconnected, this, &ANCS::onDisconnected);
QObject::connect(leController, &QLowEnergyController::connected, this, &ANCS::onConnected);
QObject::connect(leController.get(), &QLowEnergyController::disconnected, this, &ANCS::onDisconnected);
QObject::connect(leController.get(), &QLowEnergyController::connected, this, &ANCS::onConnected);

QObject::connect(leController, &QLowEnergyController::serviceDiscovered, this, &ANCS::onServiceDiscovered);
QObject::connect(leController.get(), &QLowEnergyController::serviceDiscovered, this, &ANCS::onServiceDiscovered);
}

ANCS::~ANCS()
Expand All @@ -85,8 +87,8 @@ void ANCS::start()

void ANCS::stop()
{
leController->stopAdvertising();
leController->disconnectFromDevice();
leController.get()->stopAdvertising();
leController.get()->disconnectFromDevice();

if (ancsService)
{
Expand All @@ -98,7 +100,7 @@ void ANCS::stop()

void ANCS::startAdvertising()
{
Q_ASSERT(leController);
Q_ASSERT(leController.get());
QLowEnergyAdvertisingData advertisingData;
QLowEnergyAdvertisingParameters params;
// @TODO: Add solicited UUIDs in Qt Bluetooth library
Expand All @@ -108,13 +110,13 @@ void ANCS::startAdvertising()
// Set very small advertising interval to be discovered
params.setInterval(20,20);

leController->startAdvertising(params, advertisingData, advertisingData);
leController.get()->startAdvertising(params, advertisingData, advertisingData);
}

void ANCS::leError(QLowEnergyController::Error error)
{
Q_ASSERT(leController);
QTextStream(stderr) << "LEControllerError: " << leController->errorString() << endl;
Q_ASSERT(leController.get());
QTextStream(stderr) << "LEControllerError: " << leController.get()->errorString() << endl;
stop();
emit finished(-1);
}
Expand All @@ -125,7 +127,7 @@ void ANCS::onServiceDiscovered(const QBluetoothUuid &newService)
if (newService != ancsServiceUuid) return;

Q_ASSERT(ancsService == nullptr);
ancsService = leController->createServiceObject(newService);
ancsService = leController.get()->createServiceObject(newService);
QObject::connect(ancsService, &QLowEnergyService::stateChanged, this, &ANCS::onServiceStateChanged);
ancsService->discoverDetails();
}
Expand All @@ -140,7 +142,7 @@ void ANCS::onServiceStateChanged(QLowEnergyService::ServiceState newState)
return;
}

Q_ASSERT(leController);
Q_ASSERT(leController.get());

// @TODO: Deal with other states
if (newState == QLowEnergyService::ServiceDiscovered)
Expand All @@ -161,21 +163,27 @@ void ANCS::onServiceStateChanged(QLowEnergyService::ServiceState newState)

void ANCS::onDisconnected()
{
Q_ASSERT(leController);
Q_ASSERT(leController.get());
// If disconnect happened due to error, stop will have already occured
if (leController->error() != QLowEnergyController::NoError) return;
if (leController.get()->error() != QLowEnergyController::NoError) return;

stop();
leController->switchRole();
leController.switchRole();
start();
}

void ANCS::onConnected()
{
Q_ASSERT(leController);
Q_ASSERT(leController.get());

leController->stopAdvertising();
// @TODO: Fix this hack in QT Bluetooth lib
leController->switchRole();
leController->discoverServices();
leController.get()->stopAdvertising();

QBluetoothDeviceInfo remoteDevice(leController.get()->remoteDeviceUuid(),
leController.get()->remoteName(),
0);
leController.setCentral(QLowEnergyController::createCentral(remoteDevice, this));

leController.switchRole();
leController.get()->discoverServices();
QTextStream(stderr) << "Connected"<<endl;
}
3 changes: 2 additions & 1 deletion ancs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <vector>

#include "ancsnotification.h"
#include "le_controller.h"

const QBluetoothUuid ancsServiceUuid = QBluetoothUuid(QString("{7905F431-B5CE-4E99-A40F-4B1E122D00D0}"));
const QBluetoothUuid ancsNotificationSourceCharUUid = QBluetoothUuid(QString("9FBF120D-6301-42D9-8C58-25E699A21DBD"));
Expand All @@ -40,7 +41,7 @@ public slots:

private:
QLowEnergyService* ancsService;
QLowEnergyController* leController;
LEController leController;
std::vector<ANCSNotification> notifications;

void startAdvertising();
Expand Down
49 changes: 49 additions & 0 deletions le_controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "le_controller.h"
#include<iostream>
#include <QTextStream>

LEController::LEController(){
peripheral = QLowEnergyController::createPeripheral();
}

QLowEnergyController* LEController::get(){
return selected;
}

QLowEnergyController* LEController::switchRole(){
if (selected == peripheral){
selected = central;
QTextStream(stderr) << "selected: central"<<endl;
}
else{
selected = peripheral;
QTextStream(stderr) << "selected: central"<<endl;
}

if (selected != nullptr)
return selected;

QTextStream(stderr) << "LEControllerError: null value"<<Qt::endl;
return nullptr;
}

QLowEnergyController* LEController::setPeripheral(QLowEnergyController* leController){
peripheral = leController;
return leController;
}

QLowEnergyController* LEController::setCentral(QLowEnergyController* leController){
central = leController;
return leController;
}


QLowEnergyController* LEController::selectPeripheral(){
selected = peripheral;
return selected;
}

QLowEnergyController* LEController::selectCentral(){
selected = central;
return selected;
}
17 changes: 17 additions & 0 deletions le_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<vector>
#include <QLowEnergyController>

class LEController {
private:
QLowEnergyController *peripheral, *central, *selected;

public:
LEController();

QLowEnergyController* get();
QLowEnergyController* setPeripheral(QLowEnergyController*);
QLowEnergyController* setCentral(QLowEnergyController*);
QLowEnergyController* selectPeripheral();
QLowEnergyController* selectCentral();
QLowEnergyController* switchRole();
};
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ int main(int argc, char *argv[])
QObject::connect(&ancs, &ANCS::newNotification, [&](const ANCSNotification& notification) {
if (notification.getAppIdentifier().find("SMS") != std::string::npos)
{
QTextStream(stderr) << "Something happenend"<<Qt::endl;
QStringList args;
args.append(notification.getTitle().c_str());
args.append(notification.getMessage().c_str());
Expand All @@ -50,6 +51,7 @@ int main(int argc, char *argv[])

if (notification.getAppIdentifier().find("mobilephone") != std::string::npos)
{
QTextStream(stderr) << "Something happenend"<<Qt::endl;
QStringList args;
args.append(notification.getTitle().c_str());
args.append("Incoming call...");
Expand Down