Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by haider1515 · Apr 16, 2017 at 03:00 PM · androidjavajarunity pluginintent

Read NFC tag from Unity3d android

I have been trying to read NFC tag from Unity3d with a very little knowledge of JAVA. But I am able to check weather NFC is enable in android or not. Heres my code for Unity Side.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class UnitySide : MonoBehaviour {
     private AndroidJavaObject toastExample = null;
     private AndroidJavaObject activityContext = null;
 
     void Start() {
         if(toastExample == null) {
             using(AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                 activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
             }
 
             using(AndroidJavaClass pluginClass = new AndroidJavaClass("com.rsi.nfc.reader")) {
                 if(pluginClass != null) {
                     toastExample = pluginClass.CallStatic<AndroidJavaObject>("instance");
                     toastExample.Call("setContext", activityContext);
                     activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
                         toastExample.Call("showMessage", "This is a Toast message");
                     }));
                 }
             }
         }
     }
 
 
     public void ShowToastMessage(string msg)
     {
 
         using(AndroidJavaClass pluginClass = new AndroidJavaClass("com.rsi.nfc.reader")) {
             if(pluginClass != null) {
                 toastExample = pluginClass.CallStatic<AndroidJavaObject>("instance");
                 toastExample.Call("setContext", activityContext);
                 activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
                     toastExample.Call("showMessage", msg);
                 }));
             }
         }
     }
 
     public void ShowToastMessage2(string msg)
     {
 
         using(AndroidJavaClass pluginClass = new AndroidJavaClass("com.rsi.nfc.reader")) {
             if(pluginClass != null) {
                 toastExample = pluginClass.CallStatic<AndroidJavaObject>("instance");
                 toastExample.Call("setContext", activityContext);
                 activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {
                     toastExample.Call("showMessage2", msg);
                 }));
             }
         }
     }
 
 
 
 }


This Is my Java Code which I make jar and import in Unity3d

 package com.rsi.nfc.reader;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.content.Context;
 import android.widget.Toast;
 import android.nfc.NfcAdapter;
 
 import android.os.Parcelable;
 import android.nfc.tech.MifareUltralight;
 
 public class MainActivity  {
 
     private Context context;
     private NfcAdapter nfcAdapter;
 
 
 
     public void setContext(Context context) {
         this.context = context;
     }
 
     public void showMessage(String message) {
       nfcAdapter = nfcAdapter.getDefaultAdapter(this.context) ;
       if(nfcAdapter.isEnabled())
       {
         Toast.makeText(this.context,"Adapter is Enabled ", Toast.LENGTH_SHORT).show();
       }
       else
       {
         Toast.makeText(this.context,"Adapter is not Enabled ", Toast.LENGTH_SHORT).show();
       }
     }
     public void showMessage2(String message) {
 
         Toast.makeText(this.context,message, Toast.LENGTH_SHORT).show();
       }
 
       protected void onNewIntent(Intent intent)
       {
           handleSendText(intent);
       }
 
       void handleSendText(Intent intent)
 {
     Log.v("VM","intent : " +  intent);
     String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
     if (sharedText != null)
     {
         Log.v("VM","Handling Text : " +  sharedText);
         // Update UI to reflect text being shared
         UnityPlayer.UnitySendMessage("JavaCallback", "HandleText", sharedText);
     }
 }
 
 }
 



Can anyone help me and tell me that how I will be going to read the TAG with intent.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image icoder30 · Jun 22, 2017 at 01:00 PM 0
Share

Hello, did you found a solution ? Thx :)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Gabrio · Sep 24, 2017 at 01:45 PM

Hi, here is what I use to read the message from the tag.

 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");
 
                         AndroidJavaObject[] mNdefMessage = mIntent.Call<AndroidJavaObject[]>("getParcelableArrayExtra", "android.nfc.extra.NDEF_MESSAGES");
                         AndroidJavaObject[] mNdefRecord = mNdefMessage[0].Call<AndroidJavaObject[]>("getRecords");
                         byte[] payLoad = mNdefRecord[0].Call<byte[]>("getPayload");
                         string text = System.Text.Encoding.UTF8.GetString(payLoad);
                         Debug.Log("******** " + text.Substring(3));
                         
                         /*
                         // 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;
                             InterfaceMgr.instance.TAGTrovato(tagID);
                         }
                         else {
                             tag_output_text.text = "No ID found !";
                         }
                         */
                         tagFound = true;
                         Invoke("DeselectNFC",2);
                         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) {
                     Debug.Log(ex.Message);
                 }
             }


     void DeselectNFC(){
         mIntent.Call("removeExtra", "android.nfc.extra.TAG");
         mIntent.Call("removeExtra", "android.nfc.extra.NDEF_MESSAGES");
         tagFound = false;
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ilirvg · Sep 18, 2018 at 09:27 AM 0
Share

When I use two android phones and both of them are turned on it dose not read the tag but it opens the app in the other phone. But if one of the phones is on sleep than the tag is being read properly. Any idea how to solve this issue?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling static jar function from Unity3D 0 Answers

Android manifest does not merge with generated manifest. 0 Answers

Launch main Unity activity in response to Android ACTION_BOOT_COMPLETED? 0 Answers

How to handle an intent from another Android App in Unity? 1 Answer

Android plugin & Activity help needed - "GetMethodID method not found" problems 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges