티스토리 뷰

반응형

[unity/nfc] NFC 리더 라이브러리 구현(singleton / 이벤트 방식)

 

#이벤트(event) 방식으로 구현하였다.

아주 사용이 쉽게 구현하였다.

 

#풀 소스경로

https://github.com/gofogo2/nfc-reader-event.git

 

 

#Helper 소스

public class NfcHelper : MonoBehaviour
{
    [SerializeField]
    bool tagFound = false;

    [SerializeField]
    private string sAction;

    private AndroidJavaObject mActivity;
    private AndroidJavaObject mIntent;
    
    
    //이벤트 핸들러를 통한 데이터 취득
    public delegate void TagReceiveEventHandler(string code);
    public static event TagReceiveEventHandler TagReceiveEventHandlerEvent;

    private void Update()
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            FindNfcTag();
        }
    }

   void FindNfcTag()
    {
        try
        {
            if (!tagFound)
            {
                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")
                {
                    string text = GetPayload();
                    tagFound = true;
                    TagReceiveEventHandlerEvent(text);
                    Invoke("DeselectNFC", 2);
                }
                else if (sAction == "android.nfc.action.TAG_DISCOVERED")
                {
                    Debug.Log("This type of tag is not supported !");
                }
                else
                {
                    return;
                }
            }
        }catch(Exception e)
        {
            return;
        }
    }

    void DeselectNFC()
    {
        mIntent.Call("removeExtra", "android.nfc.extra.TAG");
        mIntent.Call("removeExtra", "android.nfc.extra.NDEF_MESSAGES");
        tagFound = false;
    }

    string GetPayload()
    {
        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);
        return text;
    }

    void WriteLog(string msg)
    {
        Debug.LogWarning($"################################{msg}################################");
    }

 

#사용 이벤트 선언 뒤 손쉽게 사용하면된다.

 void Start()
    {
        NFCDevice.TagReceiveEventHandlerEvent += NFCDevice_TagReceiveEventHandlerEvent;
    }

    private void NFCDevice_TagReceiveEventHandlerEvent(string code)
    {
        text.text = code;
    }

 

#AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    xmlns:tools="http://schemas.android.com/tools"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">

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


    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"/>

    <application
        android:theme="@style/UnityThemeSelector"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name">
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
	    <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" /> 
            </intent-filter>
            <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" android:value="true" />
        </activity>
    </application>
</manifest>

 

#nfc #unity #package #library #유니티 #nfc 리더 #reader #tag 

반응형
댓글
반응형