summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/bluetooth/lowenergyscanner/assets/Dialog.qml2
-rw-r--r--examples/bluetooth/lowenergyscanner/device.cpp23
-rw-r--r--examples/bluetooth/lowenergyscanner/device.h1
-rw-r--r--examples/bluetooth/lowenergyscanner/deviceinfo.cpp8
-rw-r--r--examples/nfc/annotatedurl/annotatedurl.cpp48
-rw-r--r--examples/nfc/annotatedurl/annotatedurl.h13
-rw-r--r--examples/nfc/annotatedurl/main.cpp10
-rw-r--r--examples/nfc/corkboard/Mode.qml149
-rw-r--r--examples/nfc/corkboard/corkboards.qml39
-rw-r--r--examples/nfc/poster/poster.qml46
10 files changed, 230 insertions, 109 deletions
diff --git a/examples/bluetooth/lowenergyscanner/assets/Dialog.qml b/examples/bluetooth/lowenergyscanner/assets/Dialog.qml
index 2b8580f3..190e6b90 100644
--- a/examples/bluetooth/lowenergyscanner/assets/Dialog.qml
+++ b/examples/bluetooth/lowenergyscanner/assets/Dialog.qml
@@ -42,7 +42,7 @@ import QtQuick 2.0
Rectangle {
width: parent.width/3*2
- height: 62
+ height: dialogTextId.height + background.height + 20
z: 50
property string dialogText: ""
property bool busyImage: true
diff --git a/examples/bluetooth/lowenergyscanner/device.cpp b/examples/bluetooth/lowenergyscanner/device.cpp
index 9175095c..7ba27ad1 100644
--- a/examples/bluetooth/lowenergyscanner/device.cpp
+++ b/examples/bluetooth/lowenergyscanner/device.cpp
@@ -138,6 +138,7 @@ QString Device::getUpdate()
void Device::scanServices(const QString &address)
{
// We need the current device for service discovery.
+
for (int i = 0; i < devices.size(); i++) {
if (((DeviceInfo*)devices.at(i))->getAddress() == address )
currentDevice.setDevice(((DeviceInfo*)devices.at(i))->getDevice());
@@ -157,7 +158,12 @@ void Device::scanServices(const QString &address)
setUpdate("Back\n(Connecting to device...)");
+#ifdef Q_OS_MAC
+ if (controller && m_previousAddress != currentDevice.getAddress()) {
+ m_previousAddress = currentDevice.getAddress();
+#else
if (controller && controller->remoteAddress() != currentDevice.getDevice().address()) {
+#endif
controller->disconnectFromDevice();
delete controller;
controller = 0;
@@ -166,7 +172,11 @@ void Device::scanServices(const QString &address)
//! [les-controller-1]
if (!controller) {
// Connecting signals and slots for connecting to LE services.
+#ifdef Q_OS_MAC
+ controller = new QLowEnergyController(currentDevice.getDevice());
+#else
controller = new QLowEnergyController(currentDevice.getDevice().address());
+#endif
connect(controller, SIGNAL(connected()),
this, SLOT(deviceConnected()));
connect(controller, SIGNAL(error(QLowEnergyController::Error)),
@@ -291,13 +301,24 @@ void Device::deviceDisconnected()
void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState)
{
- if (newState != QLowEnergyService::ServiceDiscovered)
+ if (newState != QLowEnergyService::ServiceDiscovered) {
+ // do not hang in "Scanning for characteristics" mode forever
+ // in case the service discovery failed
+ // We have to queue the signal up to give UI time to even enter
+ // the above mode
+ if (newState != QLowEnergyService::DiscoveringServices) {
+ QMetaObject::invokeMethod(this, "characteristicsUpdated",
+ Qt::QueuedConnection);
+ }
return;
+ }
QLowEnergyService *service = qobject_cast<QLowEnergyService *>(sender());
if (!service)
return;
+
+
//! [les-chars]
const QList<QLowEnergyCharacteristic> chars = service->characteristics();
foreach (const QLowEnergyCharacteristic &ch, chars) {
diff --git a/examples/bluetooth/lowenergyscanner/device.h b/examples/bluetooth/lowenergyscanner/device.h
index a12f9e0e..756209da 100644
--- a/examples/bluetooth/lowenergyscanner/device.h
+++ b/examples/bluetooth/lowenergyscanner/device.h
@@ -119,6 +119,7 @@ private:
QList<QObject*> devices;
QList<QObject*> m_services;
QList<QObject*> m_characteristics;
+ QString m_previousAddress;
QString m_message;
bool connected;
QLowEnergyController *controller;
diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp
index d6b7a734..d774cb22 100644
--- a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp
+++ b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp
@@ -39,6 +39,8 @@
**
****************************************************************************/
+#include <qbluetoothuuid.h>
+
#include "deviceinfo.h"
DeviceInfo::DeviceInfo()
@@ -52,7 +54,13 @@ DeviceInfo::DeviceInfo(const QBluetoothDeviceInfo &d)
QString DeviceInfo::getAddress() const
{
+#ifdef Q_OS_MAC
+ // On OS X and iOS we do not have addresses,
+ // only unique UUIDs generated by Core Bluetooth.
+ return device.deviceUuid().toString();
+#else
return device.address().toString();
+#endif
}
QString DeviceInfo::getName() const
diff --git a/examples/nfc/annotatedurl/annotatedurl.cpp b/examples/nfc/annotatedurl/annotatedurl.cpp
index f29045b6..909f199b 100644
--- a/examples/nfc/annotatedurl/annotatedurl.cpp
+++ b/examples/nfc/annotatedurl/annotatedurl.cpp
@@ -40,6 +40,7 @@
#include "annotatedurl.h"
+#include <qnearfieldmanager.h>
#include <qnearfieldtarget.h>
#include <qndefmessage.h>
#include <qndefrecord.h>
@@ -53,14 +54,61 @@
#include <QLabel>
#include <QMouseEvent>
#include <QDesktopServices>
+#include <QDebug>
AnnotatedUrl::AnnotatedUrl(QObject *parent)
: QObject(parent)
{
+ //! [QNearFieldManager register handler]
+ manager = new QNearFieldManager(this);
+ if (!manager->isAvailable()) {
+ qWarning() << "NFC not available";
+ return;
+ }
+
+ QNdefFilter filter;
+ filter.setOrderMatch(false);
+ filter.appendRecord<QNdefNfcTextRecord>(1, UINT_MAX);
+ filter.appendRecord<QNdefNfcUriRecord>();
+ int result = manager->registerNdefMessageHandler(filter, this,
+ SLOT(handleMessage(QNdefMessage,QNearFieldTarget*)));
+ //! [QNearFieldManager register handler]
+
+ if (result != -1)
+ return;
+
+ manager->startTargetDetection();
+ connect(manager, SIGNAL(targetDetected(QNearFieldTarget*)),
+ this, SLOT(targetDetected(QNearFieldTarget*)));
+ connect(manager, SIGNAL(targetLost(QNearFieldTarget*)),
+ this, SLOT(targetLost(QNearFieldTarget*)));
}
AnnotatedUrl::~AnnotatedUrl()
{
+
+}
+
+void AnnotatedUrl::targetDetected(QNearFieldTarget *target)
+{
+ if (!target)
+ return;
+
+ connect(target, SIGNAL(ndefMessageRead(QNdefMessage)),
+ this, SLOT(handlePolledNdefMessage(QNdefMessage)));
+ target->readNdefMessages();
+}
+
+void AnnotatedUrl::targetLost(QNearFieldTarget *target)
+{
+ if (target)
+ target->deleteLater();
+}
+
+void AnnotatedUrl::handlePolledNdefMessage(QNdefMessage message)
+{
+ QNearFieldTarget *target = qobject_cast<QNearFieldTarget *>(sender());
+ handleMessage(message, target);
}
//! [handleMessage 1]
diff --git a/examples/nfc/annotatedurl/annotatedurl.h b/examples/nfc/annotatedurl/annotatedurl.h
index 863f0976..b0c69a62 100644
--- a/examples/nfc/annotatedurl/annotatedurl.h
+++ b/examples/nfc/annotatedurl/annotatedurl.h
@@ -42,16 +42,14 @@
#define ANNOTATEDURL_H
#include <qnfcglobal.h>
+#include <QNdefMessage>
#include <QtCore/QObject>
QT_FORWARD_DECLARE_CLASS(QUrl)
QT_FORWARD_DECLARE_CLASS(QPixmap)
-
-QT_BEGIN_NAMESPACE
-class QNearFieldTarget;
-class QNdefMessage;
-QT_END_NAMESPACE
+QT_FORWARD_DECLARE_CLASS(QNearFieldManager)
+QT_FORWARD_DECLARE_CLASS(QNearFieldTarget)
QT_USE_NAMESPACE
@@ -67,7 +65,12 @@ signals:
void annotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap);
public slots:
+ void targetDetected(QNearFieldTarget *target);
+ void targetLost(QNearFieldTarget *target);
void handleMessage(const QNdefMessage &message, QNearFieldTarget *target);
+ void handlePolledNdefMessage(QNdefMessage message);
+private:
+ QNearFieldManager *manager;
};
#endif // ANNOTATEDURL_H
diff --git a/examples/nfc/annotatedurl/main.cpp b/examples/nfc/annotatedurl/main.cpp
index 351f33bf..659aeb15 100644
--- a/examples/nfc/annotatedurl/main.cpp
+++ b/examples/nfc/annotatedurl/main.cpp
@@ -57,18 +57,8 @@ int main(int argc, char *argv[])
QApplication a(argc, argv);
MainWindow mainWindow;
- //! [QNearFieldManager register handler]
- QNearFieldManager manager;
AnnotatedUrl annotatedUrl;
- QNdefFilter filter;
- filter.setOrderMatch(false);
- filter.appendRecord<QNdefNfcTextRecord>(1, UINT_MAX);
- filter.appendRecord<QNdefNfcUriRecord>();
- manager.registerNdefMessageHandler(filter, &annotatedUrl,
- SLOT(handleMessage(QNdefMessage,QNearFieldTarget*)));
- //! [QNearFieldManager register handler]
-
QObject::connect(&annotatedUrl, SIGNAL(annotatedUrl(QUrl,QString,QPixmap)),
&mainWindow, SLOT(displayAnnotatedUrl(QUrl,QString,QPixmap)));
diff --git a/examples/nfc/corkboard/Mode.qml b/examples/nfc/corkboard/Mode.qml
index a3df4448..36b5afd7 100644
--- a/examples/nfc/corkboard/Mode.qml
+++ b/examples/nfc/corkboard/Mode.qml
@@ -39,110 +39,101 @@
**
****************************************************************************/
-import QtQuick 2.1
+import QtQuick 2.4
Item {
id: page
width: ListView.view.width;
height: ListView.view.height
- Rectangle {
- id: frame
- anchors.fill: parent
- z: 1
- color: "transparent"
- border.color: "darkred"
- border.width: 10
- }
Image {
source: "qrc:/cork.jpg"
- width: listView.width
- height: listView.height
+ anchors.centerIn: parent
+ width: parent.width - 20
+ height: parent.height - 20
fillMode: Image.PreserveAspectCrop
- }
-
- Text {
- anchors { horizontalCenter: parent.horizontalCenter; top: parent.top; topMargin: 10}
- text: name;
- font.pixelSize: 30; font.bold: true; color: "white"
- style: Text.Outline; styleColor: "black"
- }
-
- Repeater {
- model: notes
- Item {
- id: stickyPage
- z: 2
-
- x: Math.random() * (listView.width-listView.width*0.30) + listView.width*0.10
- y: Math.random() * (listView.height-listView.height*0.30) + listView.height*0.10
- rotation: -listView.horizontalVelocity / 200;
- Behavior on rotation {
- SpringAnimation { spring: 2.0; damping: 0.15 }
- }
+ Text {
+ anchors { horizontalCenter: parent.horizontalCenter; top: parent.top; topMargin: 10}
+ text: name;
+ font.pixelSize: 30; font.bold: true; color: "white"
+ style: Text.Outline; styleColor: "black"
+ }
+ Repeater {
+ model: notes
Item {
- id: sticky
- scale: mouse.pressed ? 1 : 0.7
- rotation: mouse.pressed ? 8 : 0
- Behavior on rotation{
- NumberAnimation {duration: 200 }
- }
- Behavior on scale{
- NumberAnimation { duration: 200 }
- }
+ id: stickyPage
- Image {
- id: stickyImage
- x: 8 + -width * 0.6 / 2; y: -20
- source: "qrc:/note-yellow.png"
- scale: 0.6; transformOrigin: Item.TopLeft
- smooth: true
- }
+ x: Math.random() * (listView.width-listView.width*0.30) + listView.width*0.10
+ y: Math.random() * (listView.height-listView.height*0.30) + listView.height*0.10
- TextEdit {
- id: myText
- text: noteText
- x: -104; y: 36; width: 215; height: 200
- smooth: true
- font.pixelSize: 24
- readOnly: false
- rotation: -8
- wrapMode: TextEdit.Wrap
+ rotation: -listView.horizontalVelocity / 200;
+ Behavior on rotation {
+ SpringAnimation { spring: 2.0; damping: 0.15 }
}
Item {
- id: interactionItem
- x: stickyImage.x; y: -20
- width: stickyImage.width * stickyImage.scale
- height: stickyImage.height * stickyImage.scale
-
- MouseArea {
- id: mouse
- anchors.fill: parent
- drag.target: stickyPage
- drag.axis: Drag.XandYAxis
+ id: sticky
+ scale: mouse.pressed ? 1 : 0.7
+ rotation: mouse.pressed ? 8 : 0
+ Behavior on rotation{
+ NumberAnimation {duration: 200 }
}
+ Behavior on scale{
+ NumberAnimation { duration: 200 }
+ }
+
Image {
- id: writeButton
- source: "qrc:/NfcFlag.png"
- rotation: -8 // Note image itself is rotated
- anchors { bottom: parent.bottom; right:parent.right }
- scale: flagMouse.pressed ? 1.3 : 1
+ id: stickyImage
+ x: 8 + -width * 0.6 / 2; y: -20
+ source: "qrc:/note-yellow.png"
+ scale: 0.6; transformOrigin: Item.TopLeft
+ smooth: true
+ }
+
+ TextEdit {
+ id: myText
+ text: noteText
+ x: -104; y: 36; width: 215; height: 200
+ smooth: true
+ font.pixelSize: 24
+ readOnly: false
+ rotation: -8
+ wrapMode: TextEdit.Wrap
+ }
+
+ Item {
+ id: interactionItem
+ x: stickyImage.x; y: -20
+ width: stickyImage.width * stickyImage.scale
+ height: stickyImage.height * stickyImage.scale
+
MouseArea {
- id: flagMouse
+ id: mouse
anchors.fill: parent
- //onClicked: { }
+ drag.target: stickyPage
+ drag.axis: Drag.XandYAxis
+ }
+ Image {
+ id: writeButton
+ source: "qrc:/NfcFlag.png"
+ rotation: -8 // Note image itself is rotated
+ anchors { bottom: parent.bottom; right:parent.right }
+ scale: flagMouse.pressed ? 1.3 : 1
+ MouseArea {
+ id: flagMouse
+ anchors.fill: parent
+ }
}
}
}
- }
- Image {
- x: -width / 2; y: -height * 0.5 / 2
- source: "qrc:/tack.png"
- scale: 0.7; transformOrigin: Item.TopLeft
+ Image {
+ x: -width / 2; y: -height * 0.5 / 2
+ source: "qrc:/tack.png"
+ scale: 0.7; transformOrigin: Item.TopLeft
+ }
}
}
}
diff --git a/examples/nfc/corkboard/corkboards.qml b/examples/nfc/corkboard/corkboards.qml
index 55c83723..22804c06 100644
--- a/examples/nfc/corkboard/corkboards.qml
+++ b/examples/nfc/corkboard/corkboards.qml
@@ -39,18 +39,50 @@
**
****************************************************************************/
-import QtQuick 2.1
+import QtQuick 2.3
import QtNfc 5.2
Rectangle {
width: 800; height: 480
- color: "black"
+ color: "darkred"
NearField {
+ property bool requiresManualPolling: false
orderMatch: false
onMessageRecordsChanged: {
- list.get(listView.currentIndex).notes.append({"noteText":messageRecords[0].text})
+ var i;
+ for (i = 0; i < messageRecords.length; ++i) {
+ var data = "";
+ if (messageRecords[i].typeNameFormat === NdefRecord.NfcRtd) {
+ if (messageRecords[i].type === "T") {
+ data = messageRecords[i].text;
+ } else if (messageRecords[i].type === "U") {
+ data = messageRecords[i].uri;
+ }
+ }
+ if (!data)
+ data = "Unknown content";
+
+ list.get(listView.currentIndex).notes.append( {
+ "noteText":data
+ })
+ }
+ }
+
+ onPollingChanged: {
+ if (!polling && requiresManualPolling)
+ polling = true; //restart polling
+ }
+
+ Component.onCompleted: {
+ // Polling should be true if
+ // QNearFieldManager::registerNdefMessageHandler() was successful;
+ // otherwise the platform requires manual polling mode.
+ if (!polling) {
+ requiresManualPolling = true;
+ polling = true;
+ }
}
}
@@ -60,7 +92,6 @@ Rectangle {
ListElement {
name: "Personal"
notes: [
- ListElement { noteText: "https://developer.blackberry.com" },
ListElement { noteText: "Near Field Communication" },
ListElement { noteText: "Touch a tag and its contents will appear as a new note" }
]
diff --git a/examples/nfc/poster/poster.qml b/examples/nfc/poster/poster.qml
index e2c67dd9..34181017 100644
--- a/examples/nfc/poster/poster.qml
+++ b/examples/nfc/poster/poster.qml
@@ -38,7 +38,7 @@
**
****************************************************************************/
-import QtQuick 2.0
+import QtQuick 2.3
import QtNfc 5.2
Rectangle {
@@ -48,6 +48,19 @@ Rectangle {
NearField {
id: nearfield
+ property bool requiresManualPolling: false
+
+ onPollingChanged: {
+ if (!polling && requiresManualPolling)
+ polling = true; //restart polling
+ }
+
+ Component.onCompleted: {
+ if (!polling) {
+ requiresManualPolling = true;
+ polling = true;
+ }
+ }
filter: [
NdefFilter { type: "U"; typeNameFormat: NdefRecord.NfcRtd; minimum: 1; maximum: 1 },
@@ -61,17 +74,32 @@ Rectangle {
var currentLocaleMatch = NdefTextRecord.LocaleMatchedNone;
var i;
+ var found = false;
for (i = 0; i < messageRecords.length; ++i) {
- if (messageRecords[i].recordType == "urn:nfc:wkt:T") {
- if (messageRecords[i].localeMatch > currentLocaleMatch) {
- currentLocaleMatch = messageRecords[i].localeMatch;
- posterText.text = messageRecords[i].text;
+ switch (messageRecords[i].typeNameFormat) {
+ case NdefRecord.NfcRtd:
+ if (messageRecords[i].type === "T") {
+ if (messageRecords[i].localeMatch > currentLocaleMatch) {
+ currentLocaleMatch = messageRecords[i].localeMatch;
+ posterText.text = messageRecords[i].text;
+ found = true;
+ }
+
+ } else if (messageRecords[i].type === "U") {
+ posterUrl.text = messageRecords[i].uri;
+ found = true;
+ }
+ break;
+ case NdefRecord.Mime:
+ if (messageRecords[i].type.startsWith("image/")) {
+ posterImage.source = messageRecords[i].uri;
+ found = true;
}
- } else if (messageRecords[i].recordType == "urn:nfc:wkt:U") {
- posterUrl.text = messageRecords[i].uri;
- } else if (messageRecords[i].recordType.substr(0, 19) == "urn:nfc:mime:image/") {
- posterImage.source = messageRecords[i].uri;
+ break;
}
+
+ if (!found)
+ console.warn("Unknown NFC tag detected. Cannot display content.")
}
root.state = "show";