Created
March 9, 2018 21:15
-
-
Save neolit123/30c3e930ab295d63a260157cffb19932 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QDebug> | |
#include <QLowEnergyService> | |
#include <QLowEnergyController> | |
QLowEnergyService *service; | |
static void stateChanged(QLowEnergyService::ServiceState newState) | |
{ | |
qDebug() << "stateChanged:" << newState; | |
if (newState != QLowEnergyService::ServiceDiscovered) | |
return; | |
QList<QLowEnergyCharacteristic> chars = service->characteristics(); | |
qDebug() << "service->characteristics().length():" << chars.length(); | |
foreach(QLowEnergyCharacteristic c, chars) { | |
qDebug() << "char: " << c.name() << c.value() << c.properties(); | |
if (c.properties() & QLowEnergyCharacteristic::Read) { | |
qDebug() << "read"; | |
service->readCharacteristic(c); | |
} | |
if (c.properties() & QLowEnergyCharacteristic::Write || c.properties() & QLowEnergyCharacteristic::WriteNoResponse) { | |
qDebug() << "write"; | |
const char data[1] = { 1 }; | |
service->writeCharacteristic(c, QByteArray(data)); | |
} | |
} | |
} | |
static void charRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) | |
{ | |
qDebug() << "char read------------"; | |
qDebug() << characteristic.name(); | |
qDebug() << value; | |
} | |
static void charWrite(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) | |
{ | |
qDebug() << "char write------------"; | |
qDebug() << characteristic.name(); | |
qDebug() << value; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
qDebug() << "main start"; | |
QBluetoothAddress addr(QString("00:80:25:34:CE:0E")); | |
QBluetoothDeviceInfo currentDevice(addr, QString("test device"), 0); | |
QLowEnergyController *control = new QLowEnergyController(currentDevice); | |
control->connectToDevice(); | |
control->discoverServices(); | |
QList<QBluetoothUuid> list = control->services(); | |
int idx = 0; | |
foreach (QBluetoothUuid uuid, list) { | |
qDebug() << idx << uuid; | |
idx++; | |
} | |
// NOTE!!! pick the UUID here | |
QBluetoothUuid uuid = list.at(0); | |
qDebug() << "creating service for UUID:" << uuid; | |
service = control->createServiceObject(uuid); | |
if (!service) { | |
qWarning() << "cannot create service"; | |
return 1; | |
} | |
// control->disconnectFromDevice(); | |
QObject::connect(service, &QLowEnergyService::stateChanged, stateChanged); | |
QObject::connect(service, &QLowEnergyService::characteristicRead, charRead); | |
QObject::connect(service, &QLowEnergyService::characteristicWritten, charWrite); | |
qDebug() << "state:" << service->state(); | |
qDebug() << "here"; | |
service->discoverDetails(); | |
qDebug() << "main end"; | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment