-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.cs
97 lines (83 loc) · 3.38 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Text;
using Android.App;
using Android.Content;
using Android.Nfc;
using Android.Nfc.Tech;
using Android.OS;
namespace XamarinNFC
{
[Activity(Label = "XamarinNFC", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private NfcAdapter _nfcAdapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
_nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
}
protected override void OnResume()
{
base.OnResume();
if (_nfcAdapter == null)
{
var alert = new AlertDialog.Builder(this).Create();
alert.SetMessage("NFC is not supported on this device.");
alert.SetTitle("NFC Unavailable");
alert.Show();
}
else
{
var tagDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered);
var filters = new[] { tagDetected };
var intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
_nfcAdapter.EnableForegroundDispatch(this, pendingIntent, filters, null);
}
}
protected override void OnNewIntent(Intent intent)
{
if (intent.Action == NfcAdapter.ActionTagDiscovered)
{
var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
if (tag != null)
{
// First get all the NdefMessage
var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
if (rawMessages != null)
{
var msg = (NdefMessage)rawMessages[0];
// Get NdefRecord which contains the actual data
var record = msg.GetRecords()[0];
if (record != null)
{
if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/
{
// Get the transfered data
var data = Encoding.ASCII.GetString(record.GetPayload());
}
}
}
}
}
}
public void WriteToTag(Intent intent, string content)
{
var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
if (tag != null)
{
Ndef ndef = Ndef.Get(tag);
if (ndef != null && ndef.IsWritable)
{
var payload = Encoding.ASCII.GetBytes(content);
var mimeBytes = Encoding.ASCII.GetBytes("text/plain");
var record = new NdefRecord(NdefRecord.TnfWellKnown, mimeBytes, new byte[0], payload);
var ndefMessage = new NdefMessage(new[] { record });
ndef.Connect();
ndef.WriteNdefMessage(ndefMessage);
ndef.Close();
}
}
}
}
}