How to get NFC working on Android using Qt 5.6

If you are using NFC tag of certain manufacturer the same should be present in the mobile NFC also then only it will pair correctly as of now NFC do not support globally. For eg. if the NFC Present inside the Sony device will max support its manufacture only and in most cases it fails to connect to others devices like nexus. So try to find your Manufacturer and connect it . Hope it helps you..


I don't believe you want those intent filters in your manifest. Adding those, tells the operating system to start your app when a tag is detected (which is why it is doing so). It does look like you're registering correctly in your code for NFC events, so perhaps the issue is the brand of NFC chip in your phone, in conjunction with the tag you're using to test with. If your phone is equipped with a Broadcom NFC chip, and you're trying to use NXP's Mifare Classic tag, you'll run into issues. Using a Desfire, or NTAG tag might help.


I have solved this one.

The reason is that in QtNfc.java where qt handles NFC intents it handles only NDEF tags by filtering ACTION_NDEF_DISCOVERED actions (and ACTION_TECH_DISCOVERED for NDEF tags that will report as tech) without simple ACTION_TAG_DISCOVERED (despite the fact it handles it in getStartIntent fuction).

But I supposed you just want to scan a simple tag to read uid, as I do. So you need to add ACTION_TAG_DISCOVERED to filter list in QtNfc.java start() function:

IntentFilter[] filters = new IntentFilter[3];
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
...

I think it would be more correct to modify filter to ACTION_TAG_DISCOVERED in setContext too. The fastest way is to open in qt creator qtconnectivity .pro for corresponding branch, correct QtNfc.java, build it and replace libQt5Nfc.so in android_armv7\lib qt folder (QtNfc.jar and QtNfc-bundled.jar in android_armv7\jar folder will be updated during build).

That is. No need to modify manifest in working application.

By the way this one:

<uses-permission android:name="android.permission.NFC"/>

qt add automatically when you add module nfc to .pro

This one

<uses-feature android:name="android.hardware.nfc" android:required="true"/>

is not necessary I suppose. It works without it.

But you can add this intent-filter if you want to tell android to start your app when a tag is detected as Anansi mentioned upper. But I really recommend to add android:alwaysRetainTaskState="true" android:launchMode="singleInstance" in application activity (like here).

I test all this with android 4.4.4 tablet and ndefeditor example. It fires targetDetected/targetLost perfectly. There can be another default application for tags in system (for example NFC Reader) and it opens up on every tag detecting, but not the time ndefeditor is waiting tag (button retrieve). And of course qt example says "NDEF read error" for non-NDEF tags, but it detects them and reads uid. Precisely what I needed.

I add the suggestion to Qt Jira and submit the patch.

The only thing I didn't understand - why ndefeditor had worked on another tablet with android 4.2. Maybe it is a hardware aspect and android on another tablet was always intent ACTION_NDEF_DISCOVERED?