Ich versuche den Qt 5.7 Beta QBluetooth GATT Server Beispiel (https://doc-snapshots.qt.io/qt5-dev/qtbluetooth-heartrate-server-example.html) auf einem Raspberry Pi 3 mit seinem integrierten Broadcom Bluetooth Chip zu betreiben.Qt 5.7 QBluetooth LE GATT Server Beispiel mit Raspberry Pi 3 und BlueZ 5.39
Bluetooth funktioniert gut auf meinem Pi 3 und die hci0 Schnittstelle ist „UP LÄUFT“ nach einer frischen boot:
[email protected]:~/bluez-5.39# hciconfig -a
hci0: Type: BR/EDR Bus: UART
BD Address: B8:27:EB:6F:71:A7 ACL MTU: 1021:8 SCO MTU: 64:1
UP RUNNING PSCAN
RX bytes:2316 acl:0 sco:0 events:99 errors:0
TX bytes:2676 acl:0 sco:0 commands:99 errors:0
Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH SNIFF
Link mode: SLAVE ACCEPT
Name: 'raspberrypi'
Class: 0x000000
Service Classes: Unspecified
Device Class: Miscellaneous,
HCI Version: 4.1 (0x7) Revision: 0xb6
LMP Version: 4.1 (0x7) Subversion: 0x2209
Manufacturer: Broadcom Corporation (15)
ich erfolgreich heruntergeladen, kompiliert und installiert die neueste BlueZ 5,39, die eine vollständige DBus enthält Schnittstelle zur BlueZ BLE Funktionalität:
http://www.kernel.org/pub/linux/bluetooth/bluez-5.39.tar.xz
ich habe bestätigt, dass die Versionen auf dem Weg abgeholt sind die richtige Version:
[bluetooth]# version
Version 5.39
Ich habe buildroot verwendet, um Qt 5.7 zu kompilieren und seine Bibliotheken auf den Pi zu verteilen. Ich kann erfolgreich auf meinem Hostcomputer aufbauen und die resultierende ausführbare Datei auf dem Pi ausführen.
Das GATT-Beispiel von Qt ist sehr einfach und ich fügte nur 2 "qDebug" Zeilen hinzu, um einige Konsolenausgaben zu sehen. Ich werde es der Vollständigkeit halber hier einfügen:
#include <QtBluetooth/qlowenergyadvertisingdata.h>
#include <QtBluetooth/qlowenergyadvertisingparameters.h>
#include <QtBluetooth/qlowenergycharacteristic.h>
#include <QtBluetooth/qlowenergycharacteristicdata.h>
#include <QtBluetooth/qlowenergydescriptordata.h>
#include <QtBluetooth/qlowenergycontroller.h>
#include <QtBluetooth/qlowenergyservice.h>
#include <QtBluetooth/qlowenergyservicedata.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qlist.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/qtimer.h>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QLowEnergyAdvertisingData advertisingData;
advertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
advertisingData.setIncludePowerLevel(true);
advertisingData.setLocalName("BlueZ 5 GATT Server");
advertisingData.setServices(QList<QBluetoothUuid>() << QBluetoothUuid::HeartRate);
QLowEnergyCharacteristicData charData;
charData.setUuid(QBluetoothUuid::HeartRateMeasurement);
charData.setValue(QByteArray(2, 0));
charData.setProperties(QLowEnergyCharacteristic::Notify);
const QLowEnergyDescriptorData clientConfig(QBluetoothUuid::ClientCharacteristicConfiguration,
QByteArray(2, 0));
charData.addDescriptor(clientConfig);
QLowEnergyServiceData serviceData;
serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
serviceData.setUuid(QBluetoothUuid::HeartRate);
serviceData.addCharacteristic(charData);
const QScopedPointer<QLowEnergyController> leController(QLowEnergyController::createPeripheral());
const QScopedPointer<QLowEnergyService> service(leController->addService(serviceData));
qDebug() << "Beginning to advertise...";
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,
advertisingData);
QTimer heartbeatTimer;
quint8 currentHeartRate = 60;
enum ValueChange { ValueUp, ValueDown } valueChange = ValueUp;
const auto heartbeatProvider = [&service, ¤tHeartRate, &valueChange]() {
QByteArray value;
value.append(char(0)); // Flags that specify the format of the value.
value.append(char(currentHeartRate)); // Actual value.
QLowEnergyCharacteristic characteristic
= service->characteristic(QBluetoothUuid::HeartRateMeasurement);
Q_ASSERT(characteristic.isValid());
qDebug() << "Changing characteristic to: " << value;
service->writeCharacteristic(characteristic, value); // Potentially causes notification.
if (currentHeartRate == 60)
valueChange = ValueUp;
else if (currentHeartRate == 100)
valueChange = ValueDown;
if (valueChange == ValueUp)
++currentHeartRate;
else
--currentHeartRate;
};
QObject::connect(&heartbeatTimer, &QTimer::timeout, heartbeatProvider);
heartbeatTimer.start(1000);
auto reconnect = [&leController, advertisingData]() {
leController->startAdvertising(QLowEnergyAdvertisingParameters(), advertisingData,
advertisingData);
};
QObject::connect(leController.data(), &QLowEnergyController::disconnected, reconnect);
return app.exec();
}
Pro-Datei:
TEMPLATE = app
TARGET = qt-gatt
QT += bluetooth
CONFIG += c++11
SOURCES += main.cpp
## Install directory
target.path = /home/pi
INSTALLS += target
Wenn ich die App auf dem Pi als root nach einem frischen Start ausgeführt - es läuft und gibt meine Drucknachrichten:
[email protected]:/home/pi# ./qt-gatt
Beginning to advertise...
Changing characteristic to: "\x00<"
Changing characteristic to: "\x00="
Changing characteristic to: "\x00>"
Changing characteristic to: "\x00?"
Es werden keine Fehler oder Warnungen angezeigt - aber keines meiner Geräte kann es sehen. Mein iPhone und Mac können andere BLE-Geräte sehen, aber nicht das Pi. Der Code legt das Pi als "erkennbar" fest.
Wie kann ich das machen/Was mache ich falsch?
Seitennotiz: Ihre Include-Namen sind völlig falsch. Das Voranstellen der Include-Namen mit 'QtModule /' ist unnötig und verbirgt die falsche qmake-Projektkonfiguration. Die Include-Namen haben das Format 'QType', nicht' qtype.h'. Letztere * passiert * im Moment zu arbeiten, aber es ist nicht dokumentiert und garantiert nicht weiter zu arbeiten. –
Danke Kuba, das werde ich mir überlegen. Diese Include-Namen stammen direkt aus dem Qt-Beispiel, also habe ich sie nicht geändert. – PhilBot
Leider brauchen einige Qt-Beispiele etwas Liebe, bevor sie als gute Praxis angesehen werden können :( –