ATEXInspect
ATEXInspect

Reputation: 3

Integrate legacy Xamarin Android code into .NET Maui

I am using the below legacy code (Xamarin.Android) as a complimentary application for an asset management application written in Xamarin.Forms. I am trying to update the Xamarin.Forms application and integrate the Xamarin.Android code into a new .NET Maui version but struggling to find examples of how to integrate the platform specific code.

Any help appreciated. Thanks Paul.

namespace NfcGuidWriter
{
    using Android.App;
    using Android.Content;
    using Android.Nfc;
    using Android.Nfc.Tech;
    using Android.OS;
    using Android.Views;
    using Android.Widget;
    using System;
    using System.Linq;

    [Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {

        //--------------------------------------------------------------------------------------------------------------------------------------------

        public static readonly string Tag = "NFC Guid Writer";

        private bool inWriteMode;
        private NfcAdapter nfcAdapter;
        private TextView textInfo; //will need refactoring
        private Button writeTagButton;

        //--------------------------------------------------------------------------------------------------------------------------------------------

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.MainActivity);

            // Get a reference to the default NFC adapter for this device. This adapter 
            // is how an Android application will interact with the actual hardware.
            nfcAdapter = NfcAdapter.GetDefaultAdapter(this);

            writeTagButton = FindViewById<Button>(Resource.Id.button_write_tag);
            writeTagButton.Click += OnClickWriteTagButton;

            textInfo = FindViewById<TextView>(Resource.Id.tv_info);
        }

        //--------------------------------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// This method is called when an NFC tag is discovered by the application.
        /// </summary>
        /// <param name="intent"></param>
        protected override void OnNewIntent(Intent intent)
        {

            try
            {

                if (inWriteMode)
                {

                    inWriteMode = false;

                    Tag tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

                    byte[] id = tag.GetId();

                    NfcV nfcvTag = NfcV.Get(tag);

                    int[] intArray = new int[36];

                    // 4d196b8c-aa42-4f90-924f-b953654c31f7 (Length = 36)
                    string guidStr = Guid.NewGuid().ToString();

                    char[] guidChars = guidStr.ToCharArray();

                    for (int i = 0; i < guidChars.Count(); i++)
                    {
                        int value = Convert.ToInt32(guidChars[i]);
                        intArray[i] = value;
                    }

                    if (nfcvTag != null)
                    {

                        nfcvTag.Connect();

                        // 0x20 with id (UID) works! (addressed v non addressed using 0,0,0,0,0,0,0,0)

                        //---------------------------------------------------------------------------------------

                        // E1 40 0E 00
                        byte[] cmd0 = new byte[] {
                        /* FLAGS   */ (byte)0x20, // 0x00 0x20 0x22 0x40 0x42 ????
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x00,
                        /* DATA    */ (byte)0xE1, (byte)0x40, (byte)0x0E, (byte)0x00
                        };

                        Array.Copy(id, 0, cmd0, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // 03 2B D1 01
                        byte[] cmd1 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x01,
                        /* DATA    */ (byte)0x03, (byte)0x2B, (byte)0xD1, (byte)0x01
                        };

                        Array.Copy(id, 0, cmd1, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // 27 54 02 65
                        byte[] cmd2 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x02,
                        /* DATA    */ (byte)0x27, (byte)0x54, (byte)0x02, (byte)0x65
                        };

                        Array.Copy(id, 0, cmd2, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // 6E XX XX XX
                        byte[] cmd3 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x03,
                        /* DATA    */ (byte)0x6E, (byte)intArray[0], (byte)intArray[1], (byte)intArray[2]
                        };

                        Array.Copy(id, 0, cmd3, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd4 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x04,
                        /* DATA    */ (byte)intArray[3], (byte)intArray[4], (byte)intArray[5], (byte)intArray[6]
                        };

                        Array.Copy(id, 0, cmd4, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd5 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x05,
                        /* DATA    */ (byte)intArray[7], (byte)intArray[8], (byte)intArray[9], (byte)intArray[10]
                        };

                        Array.Copy(id, 0, cmd5, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd6 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x06,
                        /* DATA    */ (byte)intArray[11], (byte)intArray[12], (byte)intArray[13], (byte)intArray[14]
                        };

                        Array.Copy(id, 0, cmd6, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd7 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x07,
                        /* DATA    */ (byte)intArray[15], (byte)intArray[16], (byte)intArray[17], (byte)intArray[18]
                        };

                        Array.Copy(id, 0, cmd7, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd8 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x08,
                        /* DATA    */ (byte)intArray[19], (byte)intArray[20], (byte)intArray[21], (byte)intArray[22]
                        };

                        Array.Copy(id, 0, cmd8, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd9 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x09,
                        /* DATA    */ (byte)intArray[23], (byte)intArray[24], (byte)intArray[25], (byte)intArray[26]
                        };

                        Array.Copy(id, 0, cmd9, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd10 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x0A,
                        /* DATA    */ (byte)intArray[27], (byte)intArray[28], (byte)intArray[29], (byte)intArray[30]
                        };

                        Array.Copy(id, 0, cmd10, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd11 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x0B,
                        /* DATA    */ (byte)intArray[31], (byte)intArray[32], (byte)intArray[33], (byte)intArray[34]
                        };

                        Array.Copy(id, 0, cmd11, 2, 8);

                        //---------------------------------------------------------------------------------------

                        // XX XX XX XX
                        byte[] cmd12 = new byte[] {
                        /* FLAGS   */ (byte)0x20,
                        /* COMMAND */ (byte)0x21,
                        /* UID     */ (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
                        /* OFFSET  */ (byte)0x0C,
                        /* DATA    */ (byte)intArray[35], (byte)0x00, (byte)0x00, (byte)0x00
                        };

                        Array.Copy(id, 0, cmd12, 2, 8);

                        //---------------------------------------------------------------------------------------

                        byte[] response0 = nfcvTag.Transceive(cmd0);
                        byte[] response1 = nfcvTag.Transceive(cmd1);
                        byte[] response2 = nfcvTag.Transceive(cmd2);
                        byte[] response3 = nfcvTag.Transceive(cmd3);
                        byte[] response4 = nfcvTag.Transceive(cmd4);
                        byte[] response5 = nfcvTag.Transceive(cmd5);
                        byte[] response6 = nfcvTag.Transceive(cmd6);
                        byte[] response7 = nfcvTag.Transceive(cmd7);
                        byte[] response8 = nfcvTag.Transceive(cmd8);
                        byte[] response9 = nfcvTag.Transceive(cmd9);
                        byte[] response10 = nfcvTag.Transceive(cmd10);
                        byte[] response11 = nfcvTag.Transceive(cmd11);
                        byte[] response12 = nfcvTag.Transceive(cmd12);

                        //---------------------------------------------------------------------------------------

                        nfcvTag.Close();

                    }

                    DisplayMessage("Guid (" + guidStr + ") written to tag.");

                }

            }
            catch (Exception ex)
            {
                DisplayMessage("Exception - " + ex.Message);
            }

        }

        //--------------------------------------------------------------------------------------------------------------------------------------------

        protected override void OnPause()
        {
            base.OnPause();
            // App is paused, so no need to keep an eye out for NFC tags.
            if (nfcAdapter != null)
                nfcAdapter.DisableForegroundDispatch(this);
        }

        //--------------------------------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Displays the message into a text view and into a new toast.
        /// </summary>
        /// <param name="ndefMessage">Message.</param>
        private void DisplayMessage(string ndefMessage)
        {
            textInfo.Text = ndefMessage;
            Toast.MakeText(this, ndefMessage, ToastLength.Long).Show();
        }

        //--------------------------------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Identify to Android that this activity wants to be notified when 
        /// an NFC tag is discovered. 
        /// </summary>
        private void EnableWriteMode()
        {
            inWriteMode = true;
            
            // Create an intent filter for when an NFC tag is discovered. 
            var tagDetected = new IntentFilter(NfcAdapter.ActionTagDiscovered);
            var filters = new[] { tagDetected };

            // When an NFC tag is detected, Android will use the PendingIntent to come back to this activity.
            // The OnNewIntent method will invoked by Android.
            var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Mutable);

            if (nfcAdapter == null)
            {
                var alert = new AlertDialog.Builder(this).Create();
                alert.SetMessage("NFC is not supported on this device");
                alert.SetTitle("NFC Unavailable");
                alert.SetButton("OK", delegate
                {
                    writeTagButton.Enabled = false;
                    textInfo.Text = "NFC is not supported on this device";
                });
                alert.Show();
            }
            else
                nfcAdapter.EnableForegroundDispatch(this, pendingIntent, filters, null);
            
        }

        //-------------------------------------------------------------------------------------------------------------------------------------------

        private void OnClickWriteTagButton(object sender, EventArgs eventArgs)
        {
            var view = (View)sender;
            if (view.Id == Resource.Id.button_write_tag)
            {
                DisplayMessage("Click the button then hold the ISO 15693 tag against the device to write.");
                EnableWriteMode();
            }
        }

        //-------------------------------------------------------------------------------------------------------------------------------------------

    }

}

Upvotes: 0

Views: 244

Answers (1)

Jianwei Sun - MSFT
Jianwei Sun - MSFT

Reputation: 4332

I am trying to update the Xamarin.Forms application and integrate the Xamarin.Android code into a new .NET Maui version but struggling to find examples of how to integrate the platform specific code.

Well, about using NFC on MAUI Android you can refer to this case on Q&A: NFC plugin or library for .NET Maui Android? and this tutorial: NFC NDEF communication with MAUI and Xamarin.Forms.

Wish these can help you.

Upvotes: 0

Related Questions