Search Unity

Question NFC in Mobile Unity game

Discussion in 'Editor & General Support' started by unity_rvIe7sZzzZhmbQ, Feb 13, 2023.

  1. unity_rvIe7sZzzZhmbQ

    unity_rvIe7sZzzZhmbQ

    Joined:
    Feb 25, 2021
    Posts:
    3
    Hello everyone

    I'm currently trying to enable Foreground Dispatching for a Unity / board game I'm working on. My problem is that, although I do have example of how it was achieved/is achievable, Visual Studio won't recognize the "Android" namespace, thus not allowing me to fiddle with Android functionnalities such as Intent and NFC.

    The code I am testing with is as follow :

    Code (CSharp):
    1. using System;
    2. using Xamarin.Essentials;
    3. using static UnityEditor.PlayerSettings.Android;
    4.  
    5. using UnityEditor.Android;
    6. using Android.App;
    7. using Android.Content;
    8. using Android.Nfc;
    9. using Android.Runtime;
    10. using Android.Views;
    11. using Android.Widget;
    12. using Unity.Android;
    13. using UnityEngine.Android;
    14. using UnityEngine;
    15. using UnityEngine.XR;
    16.  
    17.  
    18.  
    19. public class NfcForegroundDispatch : MonoBehaviour
    20. {
    21.     NfcAdapter nfcAdapter;
    22.     PendingIntent pendingIntent;
    23.     IntentFilter[] intentFiltersArray;
    24.     string[][] techListsArray;
    25.  
    26.    
    27.  
    28.    
    29.  
    30.     void Start()
    31.     {
    32.         // Get the NFC adapter
    33.         nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
    34.         if (nfcAdapter == null)
    35.         {
    36.             // NFC not available on this device
    37.             return;
    38.         }
    39.  
    40.         // Create a pending intent to handle NFC intents
    41.         pendingIntent = PendingIntent.GetActivity(this, 0,
    42.             new Intent(this, typeof(NfcForegroundDispatch)).AddFlags(ActivityFlags.SingleTop), 0);
    43.  
    44.         // Create an intent filter for NFC actions
    45.         IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered);
    46.         try
    47.         {
    48.             ndefDetected.AddDataType("/");
    49.         }
    50.         catch (MalformedMimeTypeException)
    51.         {
    52.             throw new Exception("Check the MIME type");
    53.         }
    54.  
    55.         // Assign the filters to the intent filter array
    56.         intentFiltersArray = new IntentFilter[] { ndefDetected };
    57.  
    58.         // Specify the tech lists you want to support
    59.         techListsArray = new string[][] { new string[] { typeof(Android.Nfc.Tech.Ndef).Name } };
    60.     }
    61.  
    62.     void OnResume()
    63.     {
    64.         // Enable foreground dispatch when the app is resumed
    65.         nfcAdapter.EnableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);
    66.     }
    67.  
    68.     void OnPause()
    69.     {
    70.         // Disable foreground dispatch when the app is paused
    71.         nfcAdapter.DisableForegroundDispatch(this);
    72.     }
    73.  
    74.     void OnNewIntent(Intent intent)
    75.     {
    76.         // Handle the NFC intent
    77.         // ...
    78.     }
    79. }
    All the 'using Android' declarations are underlined in red if I open the script from Unity.

    But if I open the script directly from VS Community, no errors show on screen.

    This led me to believe that my problem was in the libraries used by Unity, but I just can't seem to find the right library for it. I already put the Xamarin.Essentials.dll file in the appropriate folder, but that didn't solve anything...

    Am I correct about it being a library issue ? If so, what is the appropriate library I should be looking for ?

    And if not, what am I doing wrong ? I would really appreciate someone pointing me in the right direction.

    Thanks a lot!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    I have no idea what that bare
    Android
    namespace is.

    It isn't part of Unity AFAIK. Perhaps this library relies on another library??

    Or perhaps that Android package is something to be delivered via the Package Mangler?

    ALSO: You can't use ANYTHING in the
    UnityEditor
    namespace when you build your game.

    That said, whatever library you're using should not require UnityEditor when building to a finished game.