Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 01:17, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

NFC enabling a Bluetooth Headset using Qt

From Wiki
Jump to: navigation, search

This code example demonstrates how to write to a NFC tag so it can be touched to connect the phone to a Bluetooth headset.

Featured Article
08 Jan
Article Metadata
Code ExampleTested with
Devices(s): Nokia 6131 NFC, Nokia C7-00, Nokia BH-900 headset
Device(s): All NFC Enabled with Qt Mobility 1.2 or later
Keywords: QNdefMessage, QNdefRecord, QNdefNfcHandOverRecord
Created: mahbub_s60 (15 Dec 2011)
Last edited: hamishwillee (11 Oct 2012)


Touching a tag to a phone to connect it to a Bluetooth headset is a much faster and simpler connection process than using Bluetooth to first search and then pair the headset. This article shows how you can enable this use case by writing the necessary data to a Type 2 NFC tag (using Qt Mobility Connectivity APIs).

The data written to the tag is formatted according to a handover specification defined by the NFC forum. This data includes the target headset device address and name, and additional information like the record type name, class of device etc. This other information is used by the device middleware uses to determine the handover type (i.e. so that the address is interpreted as a Bluetooth device target rather than as, say, a phone number we want to dial).

The relevant code snippet is shown in the following section. At the end of the article is a test application that you can use write the tag, and read it to confirm that the information has been written correctly. To use the newly written tag you just need to touch it to the phone.

Source code

The following code shows how to write the headset specific data and other payload information to the NFC tag.

QString btaddressadr =  ui->btaddress->text();
QString btnamestr = ui->btname->text();
QNdefMessage message;
QNdefNfcHandOverRecord Record;
QNdefRecord::TypeNameFormat tyname = QNdefRecord::Mime;
QByteArray array;
int totalpayload = btnamestr.toUtf8().count()+21;
array.append((char)totalpayload); // filled later
for(int i = 10; i >= 0;i = i-2 ) {
bool ok;
QString y = btaddressadr.mid(i, 2);
int hex = y.toInt(&ok, 16);
int count = btnamestr.toUtf8().toHex().count();
array.append((char)( btnamestr.count()+1));
QByteArray arry = message.toByteArray();
qDebug() << " payloaddata formatted going to write " << arry.toHex();

Download example

Example code for writing the Bluetooth information to a NFC tag is available here: Select the Store button to write data to the tag (you can also read the information using the Retrieve button).

Tip.pngTip: The application can be used to read or write the content of the tag. Note however that once written, you don't need this application to use the card and it should be closed; the device middleware "knows" to connect the headset based on the handover information stored in the tag payload.


  • The example has been been tested with NFC tag type 2 and Nokia BH-900 headset.
  • The example will only display information related to the headset - it won't display anything if it can't find any.
  • The Bluetooth address should be in the format: 00137042d5eb
  • The code does not contain error checking. Production code should check that the data was written successfully.
This page was last modified on 11 October 2012, at 01:17.
111 page views in the last 30 days.