Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
ekke
Posts: 1,428
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone
Accepted Solution

HowTo convert nfc_ndef_record_t into QByteArray

in one of my apps I have to use NFC with Custom Record Type instead of Text Record to be able to invoke my app from the NFC Tag directly.

Also inside my app there's a feature allowing the user to write these NFC Tags

I would like to use an InvokeActionItem to use the sys.NFCViewer - I really like the nice UI from this one.

 

For my NFC Text Records it works well.

 

Here's the InvokeActionItem:

       InvokeActionItem {
            imageSource: "asset:///images/ic_add.png"
            title: qsTr("NFC Tag Text")
            enabled: nfcActionSelector.selectedValue > 0
            ActionBar.placement: ActionBarPlacement.OnBar
            query {
                mimeType: "application/vnd.rim.nfc.ndef"
                invokeTargetId: "sys.NFCViewer"
                invokeActionId: "bb.action.SHARE"
            }
            handler: InvokeHandler {
                id: shareHandler
                onInvoking: {
                    tagWriteAction.data = app.getNdefTextMessage("en", nfcText);
                    shareHandler.confirm();
                }
            }
        }

 This works well using a QByteArray:

QByteArray ApplicationUI::getNdefTextMessage(const QVariant &lang,
		const QVariant &text) {
	QString ndefLang = lang.toString();
	QString ndefText = text.toString();
	QtMobilitySubset::QNdefRecord ndefRecord = QtMobilitySubset::QNdefRecord();
	int langLength = (ndefLang.length() <= 63) ? ndefLang.length() : 63;
	QByteArray ndefPayload;
	ndefPayload[0] = langLength;
	ndefPayload.append(ndefLang.left(langLength)).append(ndefText.toUtf8());
	ndefRecord.setTypeNameFormat(QtMobilitySubset::QNdefRecord::NfcRtd);
	QByteArray nfcRtdText = "T";
	ndefRecord.setType(nfcRtdText);
	ndefRecord.setPayload(ndefPayload);
	QtMobilitySubset::QNdefMessage ndefMessage = QtMobilitySubset::QNdefMessage(
			ndefRecord);
	return ndefMessage.toByteArray();
}

 so far so good

 

Now I wanted to implement similar but using a Custom NFC Record type.

This sample app

has a method to create a Custom record:

nfc_ndef_record_t* ApplicationUI::makeCustomRecord(QString domain, QString type,
		QString content) {

	nfc_ndef_record_t* record = 0;

	int textLen = content.length();
	QString domainPlusType = domain.append(":");
	domainPlusType = domainPlusType.append(type);
	int totalLen = textLen;

	uchar_t payload[totalLen];

	int offset = 0;
	memcpy(&payload[offset], content.toUtf8().constData(), textLen);

	CHECK(
			nfc_create_ndef_record(NDEF_TNF_EXTERNAL,
					domainPlusType.toUtf8().constData(), payload, totalLen, 0,
					&record));

	return record;
}

 unfortunately I'm getting a nfc_ndef_record_t but need a QByteArray to use the InvokeActionItem same way as for NFC Text records.

 

Who knows HowTo convert the nfc_ndef_record_t into QByteArray ?

 

thx for help

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
ekke
Posts: 1,428
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: HowTo convert nfc_ndef_record_t into QByteArray

thx to John Murray's help on twitter (@jcmrim) I'm creating a QNdefRecord:

 

QByteArray ApplicationUI::makeCustomRecord(QString domain, QString type,
		QString content) {

	QString domainPlusType = domain.append(":");
	domainPlusType = domainPlusType.append(type);

	QtMobilitySubset::QNdefRecord ndefRecord = QtMobilitySubset::QNdefRecord();

	ndefRecord.setTypeNameFormat(QtMobilitySubset::QNdefRecord::ExternalRtd);
	ndefRecord.setType(domainPlusType.toUtf8());
	ndefRecord.setPayload(content.toUtf8());

	QtMobilitySubset::QNdefMessage ndefMessage = QtMobilitySubset::QNdefMessage(
			ndefRecord);

	return ndefMessage.toByteArray();
}

 Now the sys.NFCViewer comes up:

 

InvokeActionItem {
            imageSource: "asset:///images/ic_add.png"
            title: qsTr("NFC Custom")
            ActionBar.placement: ActionBarPlacement.OnBar
            query {
                mimeType: "application/vnd.rim.nfc.ndef"
                invokeTargetId: "sys.NFCViewer"
                invokeActionId: "bb.action.SHARE"
            }
            handler: InvokeHandler {
                id: shareHandler2
                onInvoking: {
                    tagWriteAction.data = app.makeCustomRecord("org.xxxx", "mytype", nfcText); 
                    shareHandler2.confirm();
                }
            }
        }

 But then if tapping on a NFC Tag I'm getting

 

The data could not be shared over NFC

 Any Idea ?

Or does the NFCViewer not accept ExternalRtd ?

 

from the documentation :

 

NDEF message is sent in the form of a byte array. The following method is used to create the byte array message:

 it works with Text records

 

Or am I doing something wrong constructing the payload or the record ?

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
Developer
ekke
Posts: 1,428
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: HowTo convert nfc_ndef_record_t into QByteArray

the solution I found together with John Murray is working

 

at least from testing some different actions there was a typo. the id was wrong: instead of 'tagWriteAction.data' in my case it was 'tagWriteAction2.data'

 

so now after converting to a QByteArray all works fine: I can invoke my app from outside and execute an action.

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.