I want to read NFC tags with Unity, but I didn’t found any good solution for this.
This solution is not updated (GitHub - twisprite-developers/unity-nfc-plugin: Unity 3D Android NFC Plugin), the Android NFC on the Asset Store is no longer available, … I only got crash or no detection…
All I found are obsolete solutions !
So, any solution for reading NFC tags in Unity 5.6 with Android device ?
You might want to email a developer of an “Android native tools” asset to see if they can implement it for you as part of their asset. An asset such as this one: Unity Asset Store - The Best Assets for Game Making
You might get the quickest solution this way.
I’m clueless as I haven’t need to do this before, but it probably require low level wrapping of methods with the android java libraries.
Thanks for your answer. I finally found a solution, without using java.
I think it can help some developpers, so here’s the solution :
go to your Unity installation folder and find the AndroidManifest.xml (like : C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Apk) and edit it like this :
now you can use NFC with Android. Here’s a little code to get the ID of the NFC tag in a text canvas (this code is not 100% working today, because you can only have one tag ID, then NFC stop trying to get another tag) :
using System.Collections;
using System.Collections.Generic;
using System;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
public class NFCDevice : MonoBehaviour {
public string tagID;
public Text tag_output_text;
public bool tagFound = false;
private AndroidJavaObject mActivity;
private AndroidJavaObject mIntent;
private string sAction;
void Start() {
tag_output_text.text = "No tag...";
}
void Update() {
if (Application.platform == RuntimePlatform.Android) {
if (!tagFound) {
try {
// Create new NFC Android object
mActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
mIntent = mActivity.Call<AndroidJavaObject>("getIntent");
sAction = mIntent.Call<String>("getAction");
if (sAction == "android.nfc.action.NDEF_DISCOVERED") {
Debug.Log("Tag of type NDEF");
}
else if (sAction == "android.nfc.action.TECH_DISCOVERED") {
Debug.Log("TAG DISCOVERED");
// Get ID of tag
AndroidJavaObject mNdefMessage = mIntent.Call<AndroidJavaObject>("getParcelableExtra", "android.nfc.extra.TAG");
if (mNdefMessage != null) {
byte[] payLoad = mNdefMessage.Call<byte[]>("getId");
string text = System.Convert.ToBase64String(payLoad);
tag_output_text.text = text;
tagID = text;
}
else {
tag_output_text.text = "No ID found !";
}
tagFound = true;
return;
}
else if (sAction == "android.nfc.action.TAG_DISCOVERED") {
Debug.Log("This type of tag is not supported !");
}
else {
tag_output_text.text = "No tag...";
return;
}
}
catch (Exception ex) {
string text = ex.Message;
tag_output_text.text = text;
}
}
}
}
}
I tried that but unfortunately it didn’t work. When building the APK I get a popup window with the message Failed to re-package resources. See console for details.
rest of the message is ommited because of its length-
UnityEditor.BuildPlayerWindow+BuildMethodException: Build failed with errors.
at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (BuildPlayerOptions options) [0x001b9] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:162
at UnityEditor.BuildPlayerWindow.CallBuildMethods (Boolean askForBuildLocation, BuildOptions defaultBuildOptions) [0x00050] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:83
UnityEditor.HostView:OnGUI()
I’m working with Unity Version 2017.1.0f3
Android SDK Build-Tools Shows Update Available 26.0.1 (I dont know much about Android dev, I usually work with Windows standalone applications).
Do you know what may be causing the problem?
Is it required to change the manifest.xml in Unity or is it enough to modifying the one in the project? I changed both but I don’t like changing the files in the Unity installation directory.
Which versions of Android SDK and Unity are you using?
Ok, I managed to solve that problem, now compiles and runs in the Galaxy S7 i’m testing it in.
Still have some issues with it reading more than one tag. I want it to notice when the tag is removed and read automatically the next tag that comes close, now I need to manually tell the program to read when a new tag is placed, but I guess I just need to play around a bit.
How did I solve it? In the player settings → Android → Other settings → Turn off Android TV compatibility
I also turned off Android Game option but I think the important option is TV comp since it says “update the manifest if needed”
I also played around with Minimum and Target API levels, currently I’ve set them to API level 22, need to check if that mattered.
GREAT !!! I was looking for a way to use NFC use for a so long time !!! I tried several tools but nothing worked. So I am very happy to read that something is working. But my problem is that I am very newbie in coding, and I don’t have any idea how I can make this code working. What I want to do is to open a scene when scanning a RFID code. I mean I plan to have several scenes, each one corresponding to an NFC code. I would be very gratefull if someone could help me with something I could understand
Alright, got it built into a scene after a hard battle with Android SDK and Java DK. Still, there is a problem - when I read an NFC, nothing happens. It let’s me choose to read it with default smartphone app or with Unity app (the one created by me). Still, if I choose Unity, nothing happens.
ChristophGeske. First of all thanks for your reply. Found you project on my ohne shortly after my piost here. The next problem that occurs while developing is the following. I try to use your NFC plugin together with Google Tango. So i have two plugins in my project which use the Androidmanifest.xml. Does any one know how i could manage to use them both in the same project. I found some solution but they did not worked at all.
I am trying to use the above examples to read tags between two Android Phones. But I am not being able to read tags if both phones are on. If in one of them I am running the app and the other one is on sleep it works fine. But if both of them are on than the beam is being used but with beam I am not being able to read the tag it is only opening the app in the second phone.
This stuff has worked great, but I’m having some trouble figuring out how to make it automatically use the currently open app when scanning an NFC. I either get no choice or it brings up the “Open with” and then I have to select the Unity app I made.
How do you make it automatically use the current app (my Unity project running on my Android phone) when I scan the NFC?