Reputation: 6424
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?
Upvotes: 1
Views: 2182
Reputation: 6424
I figured out.
NfcAdapter.ICreateNdefMessageCallback
and NfcAdapter.IOnNdefPushCompleteCallback
interfaces.CreateNdefMessage
and OnNdefPushComplete
methods.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();
}
}
Upvotes: 2