diff options
author | Alex Blasche <alexander.blasche@digia.com> | 2014-08-27 16:47:27 +0200 |
---|---|---|
committer | Alex Blasche <alexander.blasche@digia.com> | 2014-09-02 20:18:36 +0200 |
commit | da7ccfcd08c587283f4656b30ca15011ebdbeb5c (patch) | |
tree | a17e33fb1459f932b0333e7c34b0f66c48777d24 /src/bluetooth/doc | |
parent | 695afdf1a50340018db68f08be4aed6978e89724 (diff) |
Improve documentation on how to enable characteristic notifications
Change-Id: I87575fb7b5115a536e12c0a00374cc0e67428706
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Diffstat (limited to 'src/bluetooth/doc')
-rw-r--r-- | src/bluetooth/doc/snippets/doc_src_qtbluetooth.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/bluetooth/doc/snippets/doc_src_qtbluetooth.cpp b/src/bluetooth/doc/snippets/doc_src_qtbluetooth.cpp index abcf9dbb..70e1f629 100644 --- a/src/bluetooth/doc/snippets/doc_src_qtbluetooth.cpp +++ b/src/bluetooth/doc/snippets/doc_src_qtbluetooth.cpp @@ -69,11 +69,13 @@ public: void startServiceDiscovery(); void objectPush(); void btleSharedData(); + void enableCharNotifications(); public slots: void deviceDiscovered(const QBluetoothDeviceInfo &device); void serviceDiscovered(const QBluetoothServiceInfo &service); void transferFinished(QBluetoothTransferReply* reply); + void characteristicChanged(const QLowEnergyCharacteristic& ,const QByteArray&); }; void MyClass::localDevice() { @@ -171,6 +173,10 @@ void MyClass::transferFinished(QBluetoothTransferReply* /*reply*/) { } +void MyClass::characteristicChanged(const QLowEnergyCharacteristic &, const QByteArray &) +{ +} + void MyClass::btleSharedData() { QBluetoothAddress remoteDevice; @@ -194,6 +200,51 @@ void MyClass::btleSharedData() //! [data_share_qlowenergyservice] } +void MyClass::enableCharNotifications() +{ + QBluetoothAddress remoteDevice; + QLowEnergyService *service; + QLowEnergyController *control = new QLowEnergyController(remoteDevice, this); + control->connectToDevice(); + + + service = control->createServiceObject(QBluetoothUuid::BatteryService, this); + if (!service) + return; + + service->discoverDetails(); + + //... wait until discovered + +//! [enable_btle_notifications] + //PreCondition: service details already discovered + QLowEnergyCharacteristic batteryLevel = service->characteristic( + QBluetoothUuid::BatteryLevel); + if (!batteryLevel.isValid()) + return; + + QLowEnergyDescriptor notification = batteryLevel.descriptor( + QBluetoothUuid::ClientCharacteristicConfiguration); + if (!notification.isValid()) + return; + + // establish hook into notifications + connect(service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic,QByteArray)), + this, SLOT(characteristicChanged(QLowEnergyCharacteristic,QByteArray))); + + // enable notification + service->writeDescriptor(notification, QByteArray::fromHex("0100")); + + // disable notification + //service->writeDescriptor(notification, QByteArray::fromHex("0000")); + + // wait until descriptorWritten() signal is emitted + // to confirm successful write +//! [enable_btle_notifications] +} + + + int main(int argc, char** argv) { QCoreApplication app(argc, argv); |