简体   繁体   中英

Send NDEF message from Android device using Xamarin

I want to send an NDEF message from one Android device to other device. Not to write in a tag, but send the message to the nearby device.

I'm using Xamarin.Android to develop the Android app and I create the message like this:

    NdefRecord uriRecord = NdefRecord.CreateUri("http://myURL");
    NdefMessage message = new NdefMessage(new[] { uriRecord });

I now want to send the message when nearby device is detected, but I don't know how it is done. Xamarin's documentation is not complete, and I'm not familiar with Android development.

Could anyone help me or show a simple example?

I figured out.

  1. The main activity has to implement NfcAdapter.ICreateNdefMessageCallback and NfcAdapter.IOnNdefPushCompleteCallback interfaces.
  2. Implement CreateNdefMessage and OnNdefPushComplete methods.
  3. Call SetNdefPushMessageCallback and SetOnNdefPushCompleteCallback methods of NfcAdapter in the OnCreate method of the main activity.
public class Activity1 : Activity, NfcAdapter.ICreateNdefMessageCallback, NfcAdapter.IOnNdefPushCompleteCallback
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        ...

        NfcAdapter adapter = NfcAdapter.GetDefaultAdapter(this);
        adapter.SetNdefPushMessageCallback(this, this);
        adapter.SetOnNdefPushCompleteCallback(this, this);           
    }

    public NdefMessage CreateNdefMessage(NfcEvent e)
    {
        NdefRecord uriRecord = NdefRecord.CreateUri("http://myURL");
        NdefMessage message = new NdefMessage(new[] { uriRecord });
        return message;
    }

    public void OnNdefPushComplete(NfcEvent e)
    {
        //throw new NotImplementedException();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM