Search code examples
xamarin.androidnfcndef

How do you send message using NFC with Xamarin.Android?


I am developing and app to demostrate how NFC works. My goal is to make and app that will work very similary to Android Beam. I am using Xamarin.Android. The goal is to type message to one device, press button and it should be send to another device with the same app where it should be shown. I have tried almost everything even the documentation but it seems like it doesnt work. Does anyone have any experience with this technology? Is this technology even available nowadays?

There is some of my code to get you an idea about what i am trying to do:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        mNfcAdapter = NfcAdapter.GetDefaultAdapter(this);


        myButton.Click += (e, o) => { 
            mNfcAdapter.SetNdefPushMessageCallback(this, this);
            mNfcAdapter.SetOnNdefPushCompleteCallback(this, this);
            };
    }

   public NdefMessage CreateNdefMessage(NfcEvent e)
    {
        DateTime time = DateTime.Now;
        var text = (time.ToString("HH:mm:ss") + message2);
        NdefMessage msg = new NdefMessage(
        new NdefRecord[] { CreateMimeRecord (
            text, Encoding.UTF8.GetBytes (text))});
        return msg;
    }

    private NdefRecord CreateMimeRecord(string mimeType, byte[] payload)
    {
        byte[] mimeBytes = Encoding.UTF8.GetBytes(mimeType);
        NdefRecord mimeRecord = new NdefRecord(
            NdefRecord.TnfMimeMedia, mimeBytes, new byte[0], payload);
        return mimeRecord;
    }

    public void OnNdefPushComplete(NfcEvent e)
    {
        Toast.MakeText(this.ApplicationContext, "Message sent", ToastLength.Long).Show();
    }

    protected override void OnResume()
    {
        base.OnResume();
        if (NfcAdapter.ActionNdefDiscovered == Intent.Action)
        {
            ProcessIntent(Intent);
        }
    }

    protected override void OnNewIntent(Intent intent)
    {
        Intent = intent;
    }

    void ProcessIntent(Intent intent)
    {
        IParcelable[] rawMsgs = intent.GetParcelableArrayExtra(
            NfcAdapter.ExtraNdefMessages);
        NdefMessage msg = (NdefMessage)rawMsgs[0];
        var textViewMsg = FindViewById<TextView>(Resource.Id.textViewMsg);
        textViewMsg.Text = Encoding.UTF8.GetString(msg.GetRecords()[0].GetPayload());
    }

Thank you all :)


Solution

  • OnNdefPushComplete and the whole Android Beam was deprecated and removed from Android 10

    https://developer.android.com/reference/android/nfc/NfcAdapter.OnNdefPushCompleteCallback

    If you want to do Device to Device NFC going forward then it should be possible with one phone doing Host Card Emulation (HCE) and the other using enableReaderMode

    But Google recommend using Bluetooth or Wifi Direct as a more reliable replacement for Android Beam. One of the replacement methods Google provided was Android Nearby https://developers.google.com/nearby