NFC enabling a Bluetooth Headset using Qt
This code example demonstrates how to write to a NFC tag so it can be touched to connect the phone to a Bluetooth headset.
Article Metadata
Code Example
Tested with
Compatibility
Article
Introduction
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;
Record.setTypeNameFormat(tyname);
QByteArray array;
int totalpayload = btnamestr.toUtf8().count()+21;
array.append((char)totalpayload); // filled later
array.append((char)0x00);
for(int i = 10; i >= 0;i = i-2 ) {
bool ok;
QString y = btaddressadr.mid(i, 2);
int hex = y.toInt(&ok, 16);
array.append(hex);
}
int count = btnamestr.toUtf8().toHex().count();
array.append((char)( btnamestr.count()+1));
array.append((char)0x09);
array.append(btnamestr.toUtf8());
array.append((char)0x04);
array.append((char)0x0D);
array.append((char)0x04);
array.append((char)0x04);
array.append((char)0x20);
array.append((char)0x05);
array.append((char)0x03);
array.append((char)0x04);
array.append((char)0x18);
array.append((char)0x11);
array.append((char)0x23);
array.append((char)0x11);
Record.setPayload(array);
message.append(Record);
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: File:QtHandOver.zip. Select the Store button to write data to the tag (you can also read the information using the Retrieve button).
Notes:
- 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.



Alexei t - Why the tag contains DirectPrinting UUID, and not HS/HF/A2DP UUID
Hello,
I found this page trying to connect Nokia BH-505 HS to Samsung Nexus S running Android 2.3.3.
I read the NDEF message for BH-505 which is generally the same as one writed to the tag in your article.
I wonder why the last four bytes on the tag and in the BH-505 contains short UUID for Printing instead of HS/HFP/A2DP, and which full UUID shall I use in the rfcomm socket for Bluetooth connection after NFC Static Handover.
Thanks in advance,
Alexeialexei_t 21:08, 29 May 2012 (EEST)