-
Notifications
You must be signed in to change notification settings - Fork 10
/
NFCScanActivity.cs
195 lines (167 loc) · 6.81 KB
/
NFCScanActivity.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Linq;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Nfc;
using IO.Anyline.Nfc;
using Android.Nfc.Tech;
using IO.Anyline.Nfc.NFC;
using Android.Graphics;
using Xamarin.Forms;
using System.IO;
using Anyline.NFC;
namespace Anyline.Droid.NFC
{
[Activity(Label = "NFCScanActivity")]
public class NFCScanActivity : Activity, NfcDetector.INfcDetectionHandler
{
protected NfcAdapter mNfcAdapter;
private string passportNumber;
private string dateOfExpiry;
private string dateOfBirth;
Android.Widget.ProgressBar nfcReadProgressBar;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Title = "Scan NFC";
// You can customize this Activity so it best fits your App's visual identity and requirements
SetContentView(Resource.Layout.ActivityNFC);
nfcReadProgressBar = (Android.Widget.ProgressBar)FindViewById(Resource.Id.readProgressBar);
// Obtains the system's NFC Service
NfcManager NfcManager = (NfcManager)Android.App.Application.Context.GetSystemService(NfcService);
mNfcAdapter = NfcManager.DefaultAdapter;
// Retrieves the passport information from the previous activity
if (Intent != null)
{
try
{
passportNumber = Intent.GetStringExtra("pn");
dateOfBirth = Intent.GetStringExtra("db");
dateOfExpiry = Intent.GetStringExtra("de");
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}
/// <summary>
/// OnResume, we start listening for the NFC Tag.
/// </summary>
protected override void OnResume()
{
base.OnResume();
try
{
if (mNfcAdapter != null)
{
var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
mNfcAdapter.EnableForegroundDispatch(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
new string[][] {
new string[] {
"android.nfc.tech.IsoDep",
},
}
);
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("onResume error: " + e.Message);
}
}
/// <summary>
/// OnPause, we stop listening for the NFC Tag.
/// </summary>
protected override void OnPause()
{
base.OnPause();
if (mNfcAdapter != null)
{
mNfcAdapter.DisableForegroundDispatch(this);
}
}
/// <summary>
/// OnNewIntent we check if the NFC Tag was recognized, and start the detection.
/// </summary>
/// <param name="intent"></param>
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
Tag tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
TagProvider.Tag = IsoDep.Get(tag);
if (NfcAdapter.ActionTechDiscovered == intent.Action)
{
if (tag.GetTechList().ToList().Contains("android.nfc.tech.IsoDep"))
{
try
{
// Begins to read the NFC chip
NfcDetector nfcDetector = new NfcDetector(ApplicationContext, this);
nfcDetector.StartNfcDetection(passportNumber, dateOfBirth, dateOfExpiry);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
}
}
/* As the NFC reading may take a few seconds, we show a progress bar to the user, increasing it's value after each DataGroup read */
public void OnDg1Success(DataGroup1 dataGroup1)
{
Device.BeginInvokeOnMainThread(() => nfcReadProgressBar.SetProgress(nfcReadProgressBar.Progress + 33, true));
}
public void OnSODSuccess(SOD sod)
{
Device.BeginInvokeOnMainThread(() => nfcReadProgressBar.SetProgress(nfcReadProgressBar.Progress + 33, true));
}
public void OnDg2Success(Bitmap faceImage)
{
Device.BeginInvokeOnMainThread(() => nfcReadProgressBar.SetProgress(nfcReadProgressBar.Progress + 33, true));
}
/// <summary>
/// Once the NFC reading is done, we send the results back to the Xamarin.Forms layer
/// </summary>
/// <param name="result">The NFC results</param>
public void OnNfcSuccess(NFCResult result)
{
// Once we have the results, we don't need to react to the NFC Tag anymore.
mNfcAdapter.DisableForegroundDispatch(this);
Device.BeginInvokeOnMainThread(() => nfcReadProgressBar.SetProgress(100, true));
MyNFCScanResults myNFCScanResults = new MyNFCScanResults
{
FirstName = result.DataGroup1.FirstName,
LastName = result.DataGroup1.LastName,
Gender= result.DataGroup1.Gender,
DocumentNumber = result.DataGroup1.DocumentNumber,
DateOfBirth = result.DataGroup1.DateOfBirth,
DateOfExpiry = result.DataGroup1.DateOfExpiry,
DocumentType = result.DataGroup1.DocumentType,
IssuingStateCode = result.DataGroup1.IssuingStateCode,
Nationality = result.DataGroup1.Nationality,
FaceImage = ConvertBitmapToByteArray(result.DataGroup2.FaceImage)
};
// Sends the parsed results to the Message Listener (in this example, the NFCScanExamplePage)
MessagingCenter.Send(App.Current, "NFC_SCAN_FINISHED_SUCCESS", myNFCScanResults);
// Finishes this native NFC scanning activity
Finish();
}
public void OnNfcFailure(string e)
{
MessagingCenter.Send(App.Current, "NFC_SCAN_FINISHED_ERROR", e);
// Finishes this native NFC scanning activity
Finish();
}
public byte[] ConvertBitmapToByteArray(Bitmap bitmap)
{
MemoryStream stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
return stream.ToArray();
}
}
}