99
1 Manikantan Krishnamurthy Technical Manager Nokia, SEAP NFC and Qt Mobility for NFC © 2011 Nokia Nokia NFC Development v. 1.1.6 NFC Forum and the NFC Forum logo are trademarks of the Near Field Communication Forum.

Nokia NFC 1.1.6 - Day1

Embed Size (px)

Citation preview

Page 1: Nokia NFC 1.1.6 - Day1

1

Manikantan Krishnamurthy Technical Manager

Nokia, SEAP

NFC and Qt Mobility for NFC

© 2011 Nokia Nokia NFC Development v. 1.1.6

NFC Forum and the NFC Forum logo are trademarks of the Near Field Communication Forum.

Page 2: Nokia NFC 1.1.6 - Day1

What is Near Field Communication (NFC)?

Simplifies local communication into an easy and intuitive gesture

Based on and extends RFID. Operates on 13.56 MHz frequency. Transmits up to 424Kbits/sec

Open-platform technology that is being standardized in the NFC Forum

Short-range radio technology that enables communication between devices that touch/momentarily held close together

Operates over a distance of just a few centimetres, up to 10cm (Nokia: best to touch)

Typical applications are in contactless transactions, social and local interactions

Page 3: Nokia NFC 1.1.6 - Day1

Extensive experience in NFC globally • First to bring commercial NFC devices to market

• More than 80 trials and pilots globally (AT&T, O2, Citibank, Maxis, Visa, MasterCard)

• First to deploy commercial services (Austria, Malaysia, India)

3

2005 2007 2008 P6/2011

Nokia 3220 Nokia 5140i Nokia 6131 NFC

Nokia 6212 classic

Nokia C7 Symbian Anna

Page 4: Nokia NFC 1.1.6 - Day1

4

Key NFC Experiences Sharing

Transfer content between any two NFC devices

Peer to peer mode

Pairing

Connect easily with other NFC devices

Reader mode

Service Initiation

Start a service or an application

Reader/writer mode

Secure NFC

Mobile device is a credit card and travel card

Card emulation mode

Page 5: Nokia NFC 1.1.6 - Day1

5

What can NFC do for your App? Add value (NFC enhanced apps) vs. Create new value (NFC centric apps)

Page 6: Nokia NFC 1.1.6 - Day1

NFC Application Types

• NFC Centric Apps

– The application will only work

on NFC devices.

– Publisher makes sure that

the application is only

available to NFC enabled

devices

• NFC Enhanced Apps

– Offer NFC feature when

available on device

– Check for presence of NFC

feature and adapt the user

experience or...

– Make 2 versions of the

application

6

Page 7: Nokia NFC 1.1.6 - Day1

7

NFC Enhanced App – Angry Birds Magic

Comes with 20 levels

• First 5 levels available for play

• Remaining 15 levels are locked

Unlock 5 levels at a time with NFC interaction

• Touch your C7 with another C7 or certain NFC tags

Page 8: Nokia NFC 1.1.6 - Day1

NFC Centric App – Poken • Poken is a social networking app that enables

exchanging social business cards with a touch

– Create your social identity with multiple profiles

– Auto-friending on social networks such as FB,

LinkedIn…

– Combines activity feeds

• Collect, classify and share NFC tag info

• View contacts and collection in a dynamic

timeline

8

Page 10: Nokia NFC 1.1.6 - Day1

Near Field Communication

• International standard

– Interface and protocol for simple wireless interconnection of

closely coupled devices operating at 13.56 MHz

• Spec overview

– Omits specific bit / byte encodings,

details and alternatives

– Gives high-level overview of the concepts!

10

The 'N' Logo is a trademark of the NFC Forum, Inc. in the United States and in other countries.

Page 11: Nokia NFC 1.1.6 - Day1

NFC Forum Standard Overview *

11

Applications

RF Layer – ISO 18092 + ISO 14443 A/B + FeliCa

Mode Switching

Card Emulation

Smart Card capability for mobile devices

Peer-2-Peer

– LLCP (Logical Link Control Protocol)

– NFCIP

Reader / Writer

– RTD (Record Type Definition)

– NDEF (NFC Data Exchange format)

Protocol bindings (IP, OBEX, etc)

Tag Type 1, 2, 3, 4

* Figure adapted from NFC Forum Specification (www.nfc-forum.org)

Page 12: Nokia NFC 1.1.6 - Day1

12

Operating Modes

• Reader/writer: when reader is polling for a

tag

• Peer to Peer: communicate with another

NFC device (share information)

• Card Emulation*: where the NFC device

acts as a card (e.g. credit card or gift card)

Reader /writer

Peer to Peer

Base Band

NFC Antenna

Services & adaptation

NFC Interfaces

NFC Applications

NFC Controller Card Emulation*

* not supported in Symbian Anna

Page 13: Nokia NFC 1.1.6 - Day1

13

Tag & Reader NFC-based communication between two devices is possible when one device acts as a reader/writer and the other as a tag.

Tag (Initiator)

• Simple, thin device containing antenna and

small amount of memory

• Passive, powered by magnetic field that can

be picked up by Target

• Can be Read-Only/Rewritable/Writable once

Reader (Target)

• Active device generating radio signals to

communicate with tags

• Powers the passive tag when the two are

engaged in the passive mode

Page 14: Nokia NFC 1.1.6 - Day1

14

Discover Services and Content

Touch a hot spot on a smart poster with your device

What is a smart poster or object?

• Any object which contains readable NFC tags

Poster Magazine page Gadgets

What can NFC tags contain? * • Business card

• An SMS message

• Website URL

• Bluetooth information

• Code to launch an app

* Additional content types are possible as well.

Interactive, immersive and immediate

Page 15: Nokia NFC 1.1.6 - Day1

15

NFC tags vs. Barcodes

– No NFC HW required – Easily recognizable

– Inconvenient (camera, focus, take picture) – Large visual impact on product – Very limited data storage – Limited use cases (no app launch, BT pairing) – Write-only

– Works by touching, instant – Design can be merged with product – Larger data storage possible – Multiple use cases (app launch, BT pairing) – Re-writable (if desired)

– Requires NFC HW – New to consumers

Page 16: Nokia NFC 1.1.6 - Day1

16

Developing NFC apps in Qt

Page 17: Nokia NFC 1.1.6 - Day1

Target NFC devices

17 © 2011 Nokia Nokia NFC Development v. 1.1.6

Nokia 600

Nokia 700

Nokia 701

N9

Nokia 603

Page 18: Nokia NFC 1.1.6 - Day1

Following slides are for Qt SDK 1.1.3

24 © 2011 Nokia Nokia NFC Development v. 1.1.6 August 16, 2011

Page 19: Nokia NFC 1.1.6 - Day1

Qt NFC Development • Requirements

– Qt SDK 1.1.3 – Contains: Qt 4.7.3, Qt Mobility 1.1.3 – Contains: Qt 4.7.4, Qt Mobility 1.2.0

– Nokia Ovi Suite 3.x

– Symbian – Windows 7 (or XP) – Nokia C7 with Symbian Anna (for development) – Simulator

– MeeGo – Nokia N9 – Install Qt SDK’s experimental MeeGo Harmattan target

25 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 20: Nokia NFC 1.1.6 - Day1

Device Setup • Install Symbian Belle (Windows Start menu)

– Qt 4.7.4

– WebKit for Qt 4.7.4

– CODA

– Qt mobility1.2.0

• One-click install from Windows requires Ovi Suite

– Alternative: copy files to device mass memory, install on

the device using Applications → Office → File mgr.

26 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 21: Nokia NFC 1.1.6 - Day1

27 © 2011 Nokia Nokia NFC Development v. 1.1.6

Qt Mobility

Mobility 1.0

Bearer Management API Contacts Location Messaging Multimedia Publish and Subscribe Service Framework Sensors System Information Versit

Mobility 1.1

Camera Document Gallery Feedback Landmarks Maps/Navigation Organizer Service Framework – Out of process

Mobility 1.2 – develop now, deploy later this year

Connectivity (Bluetooth, NFC)

Page 22: Nokia NFC 1.1.6 - Day1

Qt Mobility 1.2 NFC • Interact with

– NFC Forum tags

– Target detection

– NDEF message handlers

– Reading and writing NDEF messages

– Send tag specific commands

– Master / slave communication

– NFC Forum devices

– LLCP Sockets

– Peer to peer communication

28 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 23: Nokia NFC 1.1.6 - Day1

29 © 2011 Nokia Nokia NFC Development v. 1.1.6

API Overview* QNearFieldManager

Detects and filters targets

QLlcpServer Listen for new server socket connections.

QNearFieldTarget Query properties,

read/write messages, send commands or

establish a client socket

QNdefMessage Collection of 0 or more

records

QNdefRecord Access to NDEF data.

Subclasses for handling specific details.

QLlcpSocket Read / write data. For

server & client sockets.

* Not a class diagram, but explains sequential usage order

Generic NFC

Tag access

Peer to peer

Page 25: Nokia NFC 1.1.6 - Day1

31

NFC Corkboards

© 2011 Nokia Nokia NFC Development v. 1.1.6

Tasks

– Extend Qt SDK example with NFC:

Touch tag to create note

– Qt Quick based UI

– C++ based NFC

– Integrate C++ with QML

https://projects.developer.nokia.com/nfccorkboards

Page 26: Nokia NFC 1.1.6 - Day1

Importing the Example • From Qt Creator

– Open project:

C:\QtSDK\Examples\4.7\declarative\toys\corkboards\corkboards.pro

32 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 27: Nokia NFC 1.1.6 - Day1

Choosing Targets • If you are using SDK 1.1.2,

• Select our new Qt 4.7.3 for Symbian^3 with QtM1.2b target

33 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 28: Nokia NFC 1.1.6 - Day1

Choosing Targets • If you are using SDK 1.1.3,

• Select Qt Simulator

34 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 29: Nokia NFC 1.1.6 - Day1

Test! With Simulator

• Select Qt Simulator

• Play to compile and deploy!

35 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 30: Nokia NFC 1.1.6 - Day1

36 © 2011 Nokia Nokia NFC Development v. 1.1.6

App Overview

main.cpp

Application startup

Loads and shows corkboards.qml

corkboards.qml

UI definition

Defines data model and view

Day.qml

Delegate for drawing the UI for a single day

Interaction for notes

ndefmanager.cpp/.h

NFC Tag discovery and reading

We will add this:

Page 31: Nokia NFC 1.1.6 - Day1

in: corkboards.qml id: list

in: corkboards.qml id: flickable

in: Day.qml

37 © 2011 Nokia Nokia NFC Development v. 1.1.6

Model, View & Delegate

ListModel

Defines free-form list data source

Container for ListElement definitions

ListView

Shows data from a model in a list

Horizontal or vertical

Delegate

Template defining each item instantiated by the view

Page 32: Nokia NFC 1.1.6 - Day1

Add Connectivity

• Edit the Qt project file (corkboards.pro)

– Uncomment & add mobility component: connectivity

– Add LocalServices Capability

38 © 2011 Nokia Nokia NFC Development v. 1.1.6

CONFIG += mobility

MOBILITY += connectivity

symbian:TARGET.CAPABILITY += NetworkServices LocalServices

Allows Internet access Allows NFC

Page 33: Nokia NFC 1.1.6 - Day1

Good to Know: Symbian Security *

1. Determine the required security / privacy related features

– Most common:

2. Define the capabilities in the Qt project file (.pro)

39 © 2011 Nokia Nokia NFC Development v. 1.1.6

* Qt apps are native apps; therefore, the security model of the target operating system applies.

Feature Capability

Internet access, telephony, messaging NetworkServices

Access location (GPS, etc.) Location

Camera, record audio UserEnvironment

Contacts, Calendar ReadUserData / WriteUserData

Bluetooth, NFC LocalServices

IMEI, model name, battery status ReadDeviceData

(See: developer.nokia.com/Community/Wiki/Capabilities)

symbian:TARGET.CAPABILITY += Location LocalServices

Basic Capabilities

Extended Capabilities

Page 34: Nokia NFC 1.1.6 - Day1

V#-Filename.ppt / 10/25/2011 © 2009 Nokia 40 Access

Capability

Certified Signed

Symbian Signed for

Nokia

Sa

les v

ersio

n

Sa

les v

ersio

n

1 week 1 week

LocalServices

ReadUserData

WriteUserData

NetworkServices

UserEnvironment

Location

SwEvent

ProtServ

TrustedUI

PowerMgmt

SurroundingsDD

ReadDeviceData

WriteDeviceData

CommDD

DiskAdmin

MultimediaDD

NetworkControl

AllFiles

DRM

TCB

Lead-time

Note Test house

Tested Test house

Tested

Express Signed

Sa

les v

ersio

n

Immediate Developer

tested

Open Signed

with PublisherID

For te

sting

Immediate

Certify on PC

Open Signed

without PublisherID

For te

sting

Immediate

Upload SIS

For

testin

g &

sa

les

ve

rsion

User Grantable

Immediate Developer

Tested

Device

Manufacturer

approval

Page 35: Nokia NFC 1.1.6 - Day1

Good to Know: Symbian Security

3. Get the right certificate during development

41 © 2011 Nokia Nokia NFC Development v. 1.1.6

No / Basic Capabilities Extended Capabilities

Sign up for Ovi Publish account (€1) publish.ovi.com

Request development certificate (free) developer.nokia.com/Distribute/Packaging_and_signing.xhtml

Sign your app (Qt Creator build settings)

Test on your develoment device(s)

Use auto-generated self signed certificate (default, no action required)

Page 36: Nokia NFC 1.1.6 - Day1

Good to Know: Symbian Security

4. Publish

42 © 2011 Nokia Nokia NFC Development v. 1.1.6

No / Basic Capabilities Extended Capabilities

Sign up for Ovi Publish account (€1) publish.ovi.com

Request development certificate (free) developer.nokia.com/Distribute/Packaging_and_signing.xhtml

Sign & package your Qt app with the Smart Installer (Qt Creator build settings)

Publish to the Nokia Store Need Certified Signed capabilities? developer.nokia.com/Community/Wiki/Capabilities -> Purchase a Publisher ID ($200 / year) Submit to Certified Signed process @ www.symbiansigned.com

Page 37: Nokia NFC 1.1.6 - Day1

NdefManager

• New C++ class

– Right-click the project

→ Add New ...

→ C++ Class

– Class name: NdefManager

– Base class: QObject

43 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 38: Nokia NFC 1.1.6 - Day1

Qt Meta-Object System

• C++ extended with QObject

– Introspection: meta-information at runtime

– Inter-object communication: signals & slots

– Parent / Child hierarchy: easier memory

management

44 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 39: Nokia NFC 1.1.6 - Day1

Include

• Headers, define member variable

45 © 2011 Nokia Nokia NFC Development v. 1.1.6

#include <qnearfieldmanager.h>

#include <qndeffilter.h>

#include <qnearfieldtarget.h>

#include <qndefmessage.h>

#include <qndefrecord.h>

#include <qndefnfcurirecord.h>

#include <QUrl>

#include <QDebug> // we will test first!

QTM_USE_NAMESPACE // Use Qt Mobility namespace

[...]

private:

QNearFieldManager *nfcManager;

ndefmanager.h

Page 40: Nokia NFC 1.1.6 - Day1

46 © 2011 Nokia Nokia NFC Development v. 1.1.6

Start Waiting NdefManager::NdefManager(QObject *parent) : QObject(parent)

{

// NdefManager (this) is the parent; will automatically delete nfcManager

nfcManager = new QNearFieldManager(this);

nfcManager->setTargetAccessModes(QNearFieldManager::NdefReadTargetAccess);

// React only to Uri records (NfcRtd, "U")

QNdefFilter filter;

filter.appendRecord<QNdefNfcUriRecord>();

// Registers message handler with criteria - Uri

nfcManager->registerNdefMessageHandler(filter,

this, SLOT(targetDetected(QNdefMessage,QNearFieldTarget*)));

// Get notified when the tag gets out of range

connect(nfcManager, SIGNAL(targetLost(QNearFieldTarget*)),

this, SLOT(targetLost(QNearFieldTarget*)));

}

ndefmanager.cpp

Page 41: Nokia NFC 1.1.6 - Day1

Signals & Slots • Signal

– Emitted when a particular event occurs (e.g., clicked())

– Qt objects: predefined signals

– Also create your own signals

• Slot

– Function called in response to a signal

– Qt objects: predefined slots (e.g., quit())

– Also create your own slots

• Connection signals slots established by developer,

handled by Qt framework

47 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 42: Nokia NFC 1.1.6 - Day1

Signals & Slots • Type safe

– Signal signature must match signature of receiving slot

– (Slot might also have shorter signature and ignore the rest of the

arguments)

• Loosely coupled

– Emitter doesn’t know or care which slots receive signal

– Information encapsulation

• Integrated, independent components

– Slots are normal C++ member functions

– Don’t know if signals are connected to them

48 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 43: Nokia NFC 1.1.6 - Day1

Lost & Found

• Methods to handle lost & found tags = Slots

• Tip

– Alt + Enter, and Qt Creator writes the .cpp method definition!

49 © 2011 Nokia Nokia NFC Development v. 1.1.6

private slots:

void targetDetected(const QNdefMessage &message,

QNearFieldTarget *target);

void targetLost(QNearFieldTarget *target);

ndefmanager.h

Page 44: Nokia NFC 1.1.6 - Day1

React

• New target: write debug output to console for now

• Target out of range: delayed object delete

50 © 2011 Nokia Nokia NFC Development v. 1.1.6

void NdefManager::targetDetected(const QNdefMessage &message,

QNearFieldTarget *target)

{

qDebug() << "Uri Target detected!";

}

ndefmanager.cpp

void NdefManager::targetLost(QNearFieldTarget *target)

{

target->deleteLater();

}

ndefmanager.cpp

Page 45: Nokia NFC 1.1.6 - Day1

Register C++ with QML

• Make your C++ class available in QML

– QML Name: NdefManager

– QML Library: Nfc 1.0

51 © 2011 Nokia Nokia NFC Development v. 1.1.6

#include <QtDeclarative>

#include "ndefmanager.h"

int main(int argc, char *argv[]) {

QApplication app(argc, argv);

qmlRegisterType<NdefManager>("Nfc", 1, 0, "NdefManager");

[...]

}

main.cpp

Page 46: Nokia NFC 1.1.6 - Day1

NdefManager in QML

• Import module

• Create Instance

52 © 2011 Nokia Nokia NFC Development v. 1.1.6

import Nfc 1.0

corkboards.qml

NdefManager {

id: ndefManager

}

corkboards.qml

Page 47: Nokia NFC 1.1.6 - Day1

Test! #2

• Run the app

• Touch a URI formatted NDEF tag

• Observe the Application Output

53 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 48: Nokia NFC 1.1.6 - Day1

Parse Tag & Emit Contents

• Revisit and extend the target detection slot

54 © 2011 Nokia Nokia NFC Development v. 1.1.6

void NdefManager::targetDetected(const QNdefMessage &message,

QNearFieldTarget *target) {

qDebug() << "Uri Target detected!";

// Go through all records in the message

foreach (const QNdefRecord &record, message) {

// Check type again, just to make sure

if (record.isRecordType<QNdefNfcUriRecord>()) {

// Convert to the specialized URI record class

QNdefNfcUriRecord uriRecord(record);

// Emit a signal with the URI

emit nfcReadTagUri(uriRecord.uri());

}

}

}

ndefmanager.cpp

Page 49: Nokia NFC 1.1.6 - Day1

Define the Signal

• Signals have no implementation

– They represent information, not a method to execute

55 © 2011 Nokia Nokia NFC Development v. 1.1.6

signals:

void nfcReadTagUri(const QUrl& nfcTagUri);

ndefmanager.h

Page 50: Nokia NFC 1.1.6 - Day1

Create new Notes

• Handle our new signal

– on<signal> : <javascript code>

– Get current corkboard page → append new notes element →

key: “noteText”, value: URI from NDEF message

56 © 2011 Nokia Nokia NFC Development v. 1.1.6

NdefManager {

id: ndefManager

onNfcReadTagUri:

list.get(flickable.currentIndex).notes.append({"noteText":nfcTagUri})

}

corkboards.qml

Page 51: Nokia NFC 1.1.6 - Day1

Test! #3

• Touch Uri-formatted NDEF NFC tags to create notes on the

current page!

57 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 52: Nokia NFC 1.1.6 - Day1

58

Interactive NFC Corkboards

© 2011 Nokia Nokia NFC Development v. 1.1.6

Tasks

– Extend previous example

– React to any kind of tags

– Write tags by pressing the red flag

while touching a tag

https://projects.developer.nokia.com/nfccorkboards

Page 53: Nokia NFC 1.1.6 - Day1

59 © 2011 Nokia Nokia NFC Development v. 1.1.6

Overview – Before QNdefFilter filter;

filter.appendRecord<QNdefNfcUriRecord>();

nfcManager->registerNdefMessageHandler(filter, this,

SLOT(targetDetected(QNdefMessage,QNearFieldTarget*)));

void targetDetected(const QNdefMessage &message,

QNearFieldTarget *target);

Signal is only emitted when a tag containing all records defined in the filter are found

Register NDEF filters and detect targets

The slot directly gets the NDEF message

Page 54: Nokia NFC 1.1.6 - Day1

60 © 2011 Nokia Nokia NFC Development v. 1.1.6

Overview – After connect(nfcManager, SIGNAL(targetDetected(QNearFieldTarget*)),

this, SLOT(targetDetected(QNearFieldTarget*)));

nfcManager->startTargetDetection();

void targetDetected(QNearFieldTarget *target) {

const bool hasNdefMessage = target->hasNdefMessage();

if (hasNdefMessage) {

connect(target, SIGNAL(ndefMessageRead(QNdefMessage)),

this, SLOT(ndefMessageRead(QNdefMessage)));

target->readNdefMessages();

}

}

Signals is emitted for every target that is detected

Detect any kind of targets

void NdefManager::ndefMessageRead(const QNdefMessage &message) ;

Another signal is emitted when reading the message from the target is finished

Page 55: Nokia NFC 1.1.6 - Day1

React to all targets

• Generic target detection instead of listening to specific tag

message(s)

– Remove previous targetDetected() with two parameters

61 © 2011 Nokia Nokia NFC Development v. 1.1.6

public slots:

void nfcWriteTag(const QString& nfcTagText); // New

private slots:

void targetDetected(QNearFieldTarget *target); // Modified from prev.

void ndefMessageRead(const QNdefMessage &message); // New

private:

QNearFieldTarget *cachedTarget; // New

ndefmanager.h

Page 56: Nokia NFC 1.1.6 - Day1

62 © 2011 Nokia Nokia NFC Development v. 1.1.6

Start Generic Target Detection NdefManager::NdefManager(QObject *parent) : QObject(parent)

{

// [...]

// React only to Uri records (NfcRtd, "U")

QNdefFilter filter;

filter.appendRecord<QNdefNfcUriRecord>();

nfcManager->registerNdefMessageHandler(filter,

this, SLOT(targetDetected(QNdefMessage,QNearFieldTarget*)));

// Get notified when the tag gets out of range

connect(nfcManager, SIGNAL(targetLost(QNearFieldTarget*)),

this, SLOT(targetLost(QNearFieldTarget*)));

connect(nfcManager, SIGNAL(targetDetected(QNearFieldTarget*)),

this, SLOT(targetDetected(QNearFieldTarget*)));

nfcManager->startTargetDetection();

}

ndefmanager.cpp

Page 57: Nokia NFC 1.1.6 - Day1

Read NDEF Message

• Detected a target? Read its message.

63 © 2011 Nokia Nokia NFC Development v. 1.1.6

void NdefManager::targetDetected(QNearFieldTarget *target)

{

qDebug() << "Target detected!";

const bool hasNdefMessage = target->hasNdefMessage();

if (hasNdefMessage) {

connect(target, SIGNAL(ndefMessageRead(QNdefMessage)),

this, SLOT(ndefMessageRead(QNdefMessage)));

target->readNdefMessages();

}

// Cache target to member variable for writing

cachedTarget = target;

}

ndefmanager.cpp

Page 58: Nokia NFC 1.1.6 - Day1

Target Lost

• Set cached target to NULL

64 © 2011 Nokia Nokia NFC Development v. 1.1.6

void NdefManager::targetLost(QNearFieldTarget *target) {

cachedTarget = NULL; // New

target->deleteLater();

}

ndefmanager.cpp

Page 59: Nokia NFC 1.1.6 - Day1

Parse NDEF Message

• Same code as before, different method

65 © 2011 Nokia Nokia NFC Development v. 1.1.6

void NdefManager::ndefMessageRead(const QNdefMessage &message) {

// Go through all records in the message

foreach (const QNdefRecord &record, message) {

// Check type

if (record.isRecordType<QNdefNfcUriRecord>()) {

// Convert to the specialized URI record class

QNdefNfcUriRecord uriRecord(record);

// Emit a signal with the URI

emit nfcReadTagUri(uriRecord.uri());

}

}

}

ndefmanager.cpp

Page 60: Nokia NFC 1.1.6 - Day1

66 © 2011 Nokia Nokia NFC Development v. 1.1.6 August 16, 2011 Andreas Jakl

Write URI Ndef Message void NdefManager::nfcWriteTag(const QString &nfcTagText) {

// The device currently has a target in reach

if (cachedTarget) {

// Convert text to a URI, adding missing http:// if necessary

QUrl convertedUrl = QUrl::fromUserInput(nfcTagText);

// Check if it is a valid URL

if (convertedUrl.isValid() && nfcTagText.contains('.')) {

QNdefMessage message;

// Create a URI NDEF record

QNdefNfcUriRecord uriRecord;

uriRecord.setUri(convertedUrl);

message.append(uriRecord);

qDebug() << "Writing URI ...";

// Write NDEF message to the tag

cachedTarget->writeNdefMessages(QList<QNdefMessage>() << message);

} else {

qDebug() << "No valid URI";

}

} else {

qDebug() << "No cached target available";

}

}

ndefmanager.cpp

Page 61: Nokia NFC 1.1.6 - Day1

67 © 2011 Nokia Nokia NFC Development v. 1.1.6

Extend UI with Write Button Image {

id: writeButton

source: "NfcFlag.png"

rotation: -8 // Note image itself is rotated

anchors { bottom: parent.bottom; right:parent.right }

MouseArea {

anchors.fill: parent

onClicked: ndefManager.nfcWriteTag(myText.text)

}

}

Day.qml, between lines 122 / 123 (below MouseArea with id: mouse)

NfcFlag.png <project dir>\qml\

Page 62: Nokia NFC 1.1.6 - Day1

Test! • Touch a tag, at the same time press the NFC flag of a note

– Writes the note text to the tag

– Remember to enter a valid URI!

68 © 2011 Nokia Nokia NFC Development v. 1.1.6

The extended solution on Nokia Developer Projects additionally handles text tags and potential

errors.

https://projects.developer.nokia.com/nfccorkboards

Page 63: Nokia NFC 1.1.6 - Day1

69

NFC Corkboards

© 2011 Nokia Nokia NFC Development v. 1.1.6

Task

– Adding autostart feature

https://projects.developer.nokia.com/nfccorkboards

Page 64: Nokia NFC 1.1.6 - Day1

Auto start feature • Create an xml file with the format given below ( for Symbian^3 )

70 © 2011 Nokia Nokia NFC Development v. 1.1.6

<?xml version="1.0" encoding="UTF-8"?>

<SFW version="1.1">

<service> <name>%APPNAME%</name>

<ipcaddress>%APPNAME%</ipcaddress>

<description>NFC NDEF message handler for %APPNAME%</description>

<interface>

<name>com.nokia.qtmobility.nfc.NdefMessageHandler</name>

<version>1.0</version>

<description>NFC NDEF message handler for %APPNAME%</description>

<capabilities></capabilities>

<customproperty key="datatype">%DATATYPE%</customproperty>

</interface>

</service>

</SFW>

Page 65: Nokia NFC 1.1.6 - Day1

Auto start feature • Recommended to name the xml file after the application package name

• XML file needs to be installed in the correct location. The following needs to be

added into the .pro file

71 © 2011 Nokia Nokia NFC Development v. 1.1.6

symbian {

ndefhandler.sources = %APPNAME%.xml

ndefhandler.path = /private/2002AC7F/import/

DEPLOYMENT += ndefhandler

}

Page 66: Nokia NFC 1.1.6 - Day1

Auto start feature • Make sure that the application calls registerNdefMessageHandler()

72 © 2011 Nokia Nokia NFC Development v. 1.1.6

NdefManager::NdefManager(QObject *parent) : QObject(parent)

{

// Get notified when the tag gets out of range

connect(nfcManager, SIGNAL(targetLost(QNearFieldTarget*)),

this, SLOT(targetLost(QNearFieldTarget*)));

connect(nfcManager, SIGNAL(targetDetected(QNearFieldTarget*)),

this, SLOT(targetDetected(QNearFieldTarget*)));

// Add this line below

nfcManager->registerNdefMessageHandler(this,

SLOT(targetMessageDetected(QNdefMessage,QNearFieldTarget*)));

nfcManager->startTargetDetection();

}

ndefmanager.cpp

Page 67: Nokia NFC 1.1.6 - Day1

Auto start feature

73 © 2011 Nokia Nokia NFC Development v. 1.1.6

private slots:

void targetMessageDetected(const QNdefMessage &message,

QNearFieldTarget *target );

ndefmanager.h

• Remember to add the targetMessageDetected() in the header/cpp files

void NdefManager::targetMessageDetected(const QNdefMessage &message,

QNearFieldTarget *target)

{

qDebug() << "Target detected!";

ndefMessageRead(message);

}

ndefmanager.cpp

Page 68: Nokia NFC 1.1.6 - Day1

74

NFC Chat

© 2011 Nokia Nokia NFC Development v. 1.1.6

Tasks

– Develop an NFC chat from scratch

– NFC Peer to Peer mode

– LLCP Protocol (server & client sockets)

– Qt Quick UI

https://projects.developer.nokia.com/nfcchat

Page 69: Nokia NFC 1.1.6 - Day1

75 © 2011 Nokia Nokia NFC Development v. 1.1.6

App Overview

main.cpp

Application startup

Loads and shows main.qml

main.qml

UI definition

Defines data model and view

nfcpeertopeer.cpp/.h

Target discovery

NFC Peer to peer sockets (client & server)

Page 70: Nokia NFC 1.1.6 - Day1

New Project (SDK 1.1.2)

• Qt Quick Application

– Name: NfcChat

– Targets: only

Qt 4.7.3 w/ QtM 1.2

(uncheck Qt 4.7.3 for Desktop)

– Accept defaults for the rest

76 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 71: Nokia NFC 1.1.6 - Day1

New Project (SDK 1.1.3) • Qt Quick Application

– Name: NfcChat

– Application Type: Qt Quick

Application Components

for Symbian

– Targets: only

Qt 4.7.4 for Symbian Belle

– Accept defaults for the rest 77 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 72: Nokia NFC 1.1.6 - Day1

Add Connectivity

• Edit the Qt project file (nfcchat.pro)

– Uncomment & add mobility component: connectivity

– Add LocalServices Capability

78 © 2011 Nokia Nokia NFC Development v. 1.1.6

CONFIG += mobility

MOBILITY += connectivity

symbian:TARGET.CAPABILITY += NetworkServices LocalServices

Allows Internet access Allows NFC

Page 73: Nokia NFC 1.1.6 - Day1

UI Design • Delete existing main.qml and MainPage.qml

• Add a new QML page main.qml

• create the followings:

• Button (Image + mousearea) – set id as “sendButton”

• TextEdit – set id as “sendTextEdit”

• List View – set id as “messageView”

• List Model – set id as “messageModel”

79 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 74: Nokia NFC 1.1.6 - Day1

Solution: (main.qml) import QtQuick 1.0

Rectangle {

id: rectangle1

width: 360

height: 360

Image {

id: sendButton

x: 291

anchors.right: parent.right

anchors.rightMargin: 0

anchors.top: parent.top

anchors.topMargin: 0

source: "NfcButton.png"

MouseArea {

id: mouse_area1

anchors.fill: parent

} }

// continue next page

}

80 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 75: Nokia NFC 1.1.6 - Day1

Solution: (main.qml) TextEdit {

id: sendTextEdit

width: 80

height: 20

text: "Hello NFC"

anchors.left: parent.left

anchors.leftMargin: 0

anchors.top: parent.top

anchors.topMargin: 0

font.pixelSize: 10

}

ListModel { id:messageModel }

// continue next page

81 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 76: Nokia NFC 1.1.6 - Day1

Solution: (main.qml) ListView {

id: messageView

model: messageModel

delegate: Text {text: chatMessage }

anchors.right: parent.right

anchors.rightMargin: 0

anchors.left: parent.left

anchors.leftMargin: 0

anchors.bottom: parent.bottom

anchors.bottomMargin: 0

anchors.top: sendButton.bottom

anchors.topMargin: 0

}

82 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 77: Nokia NFC 1.1.6 - Day1

NfcPeerToPeer

• Go to the “Edit” view

• New C++ class

– Right-click the project

→ Add New ...

→ C++ Class

– Class name: NfcPeerToPeer

– Base class: QObject

86 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 78: Nokia NFC 1.1.6 - Day1

Include

• Headers, define member variable

87 © 2011 Nokia Nokia NFC Development v. 1.1.6

#include <qnearfieldmanager.h>

#include <qllcpserver.h>

#include <qllcpsocket.h>

QTM_USE_NAMESPACE // Use Qt Mobility namespace

[...]

private:

QLatin1String nfcUri;

QNearFieldManager *nfcManager;

QLlcpServer *nfcServer;

QLlcpSocket *nfcClientSocket;

QLlcpSocket *nfcServerSocket;

nfcpeertopeer.h

Page 79: Nokia NFC 1.1.6 - Day1

Search for Targets

• Save own unique service Uri and start target detection

88 © 2011 Nokia Nokia NFC Development v. 1.1.6

NfcPeerToPeer::NfcPeerToPeer(QObject *parent) :

QObject(parent), nfcUri("urn:nfc:sn:com.nokia.nfcchat")

{

nfcManager = new QNearFieldManager(this);

connect(nfcManager, SIGNAL(targetDetected(QNearFieldTarget*)),

this, SLOT(targetDetected(QNearFieldTarget*)));

connect(nfcManager, SIGNAL(targetLost(QNearFieldTarget*)),

this, SLOT(targetLost(QNearFieldTarget*)));

// Only detect other NFC devices. Leave the phone to handle NFC tags.

nfcManager->startTargetDetection(QNearFieldTarget::NfcForumDevice);

}

nfcpeertopeer.cpp

Page 80: Nokia NFC 1.1.6 - Day1

Signals & Slots

• Handle targets

• Emit chat messages to the UI

89 © 2011 Nokia Nokia NFC Development v. 1.1.6

private slots:

void targetDetected(QNearFieldTarget *target);

void targetLost(QNearFieldTarget *target);

nfcpeertopeer.h

signals:

void chatMessage(const QString& nfcClientMessage);

nfcpeertopeer.h

Page 81: Nokia NFC 1.1.6 - Day1

React

• New target: emit info message

• Target out of range: emit message & delayed object delete

90 © 2011 Nokia Nokia NFC Development v. 1.1.6

void NfcPeerToPeer::targetDetected(QNearFieldTarget *target)

{

emit chatMessage("Target detected");

}

nfcpeertopeer.cpp

void NfcPeerToPeer::targetLost(QNearFieldTarget *target)

{

target->deleteLater();

emit chatMessage("Target lost");

}

nfcpeertopeer.cpp

Page 82: Nokia NFC 1.1.6 - Day1

Register C++ with QML

• Make your C++ class available in QML

– QML Name: NfcPeerToPeer

– QML Library: NfcPeerToPeer 1.0

91 © 2011 Nokia Nokia NFC Development v. 1.1.6

#include <QtDeclarative>

#include "nfcpeertopeer.h"

int main(int argc, char *argv[]) {

QApplication app(argc, argv);

qmlRegisterType<NfcPeerToPeer>("NfcPeerToPeer", 1, 0, "NfcPeerToPeer");

[...]

}

main.cpp

Page 83: Nokia NFC 1.1.6 - Day1

NfcPeerToPeer in QML • Import module

• Update the list model when a NFC chat message arrives

92 © 2011 Nokia Nokia NFC Development v. 1.1.6

import NfcPeerToPeer 1.0

main.qml

NfcPeerToPeer {

id: nfcPeerToPeer

onChatMessage: {

messageModel.append( {"chatMessage": nfcClientMessage} )

}

}

ListModel { id: messageModel }

ListView { model: messageModel

delegate: Text { text: chatMessage }

// [...]

}

main.qml

Page 85: Nokia NFC 1.1.6 - Day1

Test in simulator!

94 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 86: Nokia NFC 1.1.6 - Day1

NFC LLCP Server Socket

• Define slots for new server connections & sending text

• Listen for new server connections

95 © 2011 Nokia Nokia NFC Development v. 1.1.6

NfcPeerToPeer::NfcPeerToPeer(QObject *parent)

// [...]

nfcServer = new QLlcpServer(this);

connect(nfcServer, SIGNAL(newConnection()),

this, SLOT(handleNewConnection()));

nfcServer->listen(nfcUri);

}

nfcpeertopeer.cpp

public slots:

void sendText(const QString& text);

private slots:

void handleNewConnection();

nfcpeertopeer.h

Page 87: Nokia NFC 1.1.6 - Day1

Connections & Sending

• Create a socket for a new server connection

• Slot to send chat messages over the server socket

96 © 2011 Nokia Nokia NFC Development v. 1.1.6

void NfcPeerToPeer::sendText(const QString& text) {

if (nfcServerSocket && nfcServerSocket->isOpen()) {

nfcServerSocket->write(text.toUtf8());

emit chatMessage("Message sent");

}

}

nfcpeertopeer.cpp

void NfcPeerToPeer::handleNewConnection() {

if (nfcServerSocket) { nfcServerSocket->deleteLater(); }

nfcServerSocket = nfcServer->nextPendingConnection();

}

nfcpeertopeer.cpp

Page 88: Nokia NFC 1.1.6 - Day1

Sending Chat Messages

• Call the sendText() slot in MouseArea’s onClicked handler

97 © 2011 Nokia Nokia NFC Development v. 1.1.6

onClicked: nfcPeerToPeer.sendText(sendTextEdit.text)

main.qml

Page 89: Nokia NFC 1.1.6 - Day1

NFC LLCP Client Socket

• Define slot to read incoming text

• Create the client socket (to be connected to NFC targets)

98 © 2011 Nokia Nokia NFC Development v. 1.1.6

NfcPeerToPeer::NfcPeerToPeer(QObject *parent)

// [...]

nfcClientSocket = new QLlcpSocket(this);

connect(nfcClientSocket, SIGNAL(readyRead()),

this, SLOT(readText()));

}

nfcpeertopeer.cpp

private slots:

void readText();

nfcpeertopeer.h

Page 90: Nokia NFC 1.1.6 - Day1

Connections & Reading • Connect the socket to a target in targetDetected()

• Read text from the socket and emit a signal

• Cleanup in targetLost()

99 © 2011 Nokia Nokia NFC Development v. 1.1.6

void NfcPeerToPeer::readText() {

QByteArray rawData = nfcClientSocket->readAll();

QString data = QString::fromUtf8(rawData.constData(), rawData.size());

emit chatMessage(data);

}

nfcpeertopeer.cpp

nfcClientSocket->connectToService(target, nfcUri);

nfcpeertopeer.cpp

nfcClientSocket->disconnectFromService();

nfcpeertopeer.cpp

Page 91: Nokia NFC 1.1.6 - Day1

Test! #2

• Touch upper back of two phones

– While touching, press the send button

100 © 2011 Nokia Nokia NFC Development v. 1.1.6

The extended solution on Nokia Developer Projects includes cached text sending and

error handling and is based on Qt Quick Components.

Note: if installing the solution, uninstall your own app first to avoid a name conflict..

https://projects.developer.nokia.com/nfcchat

Note: NFC app cannot be deploy on C7 with Symbian Anna at this moment With SDK 1.1.3 . Click on the link to view details

Page 92: Nokia NFC 1.1.6 - Day1

Nokia Store intake for Qt apps • 27 Models

• Compatible with Qt 4.7 / QML

• Symbian^3: N8, E7, C7, C6-01, C7 Astound, E6, X7 and Oro

• S60 5th Edition: X6, C6-00, C5-03, N97, N97 Mini, 5800, 5530, 5250, 5235, 5233, 5230, 5228

(does not support Qt Quick Components)

• Nokia N9

• Nokia N900 with PR 1.3 later

• Compatible with Qt 4.6 (Qt mobility 1.0)

• All of the above

• S60 3rd Edition: E72, E71, E66, E63 and E52

• Check here for the updated list: http://www.developer.nokia.com/Distribute/Packaging_and_signing.xhtml

101 © 2011 Nokia Nokia NFC Development v. 1.1.6

Page 93: Nokia NFC 1.1.6 - Day1

Where to find help? • http://www.developer.nokia.com/Community/Wiki/About_NFC

• Qt SDK – Help, examples

• Discussion boards, wikis – Nokia Developer: http://developer.nokia.com/

– Qt Developer Network: http://developer.qt.nokia.com/

• e-Learning materials – http://qt.nokia.com/developer/learning/online/training/

– http://qt.nokia.com/services-partners/qt-in-education/qt-in-education-course-material

• Community – http://www.qtcentre.org/

102

Page 94: Nokia NFC 1.1.6 - Day1

103

Qt Books Beginning Nokia Apps Development Ray Rischpater, Daniel Zucker. Apress. Complete end-to-end story of developing for Symbian and MeeGo with Qt (Quick) and HTML 5. Status: Qt 4.7, December 2010

C++ GUI Programming with Qt 4 (2nd edition) Jasmin Blanchette, Mark Summerfield. Prentice Hall. Official book for generic Qt development (no mobile). Good for looking things up or for enhancing your knowledge, average for learning from scratch. Status: Qt 4.3, 2008

Foundations of Qt Development Johan Thelin. Apress. More in-depth and technically oriented explanation of Qt – different approach to many other books. Generic Qt, no mobile. Status: 2007

(some recommendations)

Page 95: Nokia NFC 1.1.6 - Day1

Nokia Certified Qt Developer • Official accreditation for individual developers

– Confirms your development skills in Qt

– Become more attractive in the job market!

– Qt Essentials exam: 50 multiple choice questions, 60 minutes

– Qt Advanced exams: widgets and C++

104

Page 96: Nokia NFC 1.1.6 - Day1

Nokia Developer Champion & Qt Ambassador

• Active community member?

– Be even more involved, improve your network and get rewarded!

– Nokia Developer Champion

http://www.developer.nokia.com/Community/Champions/

– Qt Ambassador

http://qt.nokia.com/qt-in-use/ambassadors/qtambassador/

105

Page 97: Nokia NFC 1.1.6 - Day1

Universities • Qt is a good way to teach software development

– Instant results to motivate attendees

– Graphical tools make entry easier

– Flexible system, extensible with own libraries

– … and still standard C++!

• University support

– Free Qt course materials under Creative Commons (PPT and OpenOffice)

http://qt.nokia.com/learning/education/course-materials

– Nokia Developer University support

http://www.developer.nokia.com/Developer_Programs/Nokia_Developer_for_uni

versities/

106

Page 98: Nokia NFC 1.1.6 - Day1

Your Feedback Counts! • Not happy with the way something works? Found a bug?

– Submit your Qt bug report or suggestion to:

http://bugreports.qt.nokia.com/

– Unsure about what to include? The guide is here:

http://developer.qt.nokia.com/wiki/ReportingBugsInQt

– Found a bug somewhere outside of Qt?

Report it here:

http://www.developer.nokia.com/Support/

– Also go to the above URL to buy a

commercial support ticket if needed!

107

Page 99: Nokia NFC 1.1.6 - Day1

108

Thank You.

© 2011 Nokia Nokia NFC Development v. 1.1.6