Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MainActivity.cs
1using Android.App;
2using Android.Content;
3using Android.Content.PM;
4using Android.Nfc;
5using Android.Nfc.Tech;
6using Android.OS;
7using Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;
11using System.Text;
12
13namespace NeuroAccessMaui
14{
15 [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true,
16 ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density | ConfigChanges.Locale,
17 ScreenOrientation = ScreenOrientation.Portrait, LaunchMode = LaunchMode.SingleTop)]
18 [IntentFilter([NfcAdapter.ActionNdefDiscovered],
19 Categories = [Intent.CategoryDefault], DataMimeType = "*/*")]
20 [IntentFilter([Intent.ActionView],
21 Categories = [Intent.CategoryDefault, Intent.CategoryBrowsable],
22 DataSchemes = ["iotid", "iotdisco", "iotsc", "tagsign", "obinfo", "edaler", "nfeat", "xmpp", "aes256", "neuroaccess"])]
23 public class MainActivity : MauiAppCompatActivity
24 {
25 private static NfcAdapter? nfcAdapter = null;
26
27 protected override async void OnPostCreate(Bundle? savedInstanceState)
28 {
29 try
30 {
31 base.OnPostCreate(savedInstanceState);
32 await this.HandleIntent(this.Intent);
33 nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
34
35 }
36 catch (Exception ex)
37 {
38 StringBuilder msg = new();
39
40 msg.AppendLine("An error occurred in the Android MainActivity.OnPostCreate method.");
41 msg.AppendLine("Exception message:");
42 msg.Append(ex.Message);
43 msg.AppendLine();
44 msg.AppendLine("```");
45 msg.AppendLine(ex.StackTrace);
46 msg.AppendLine("```");
47
48 App.SendAlert(msg.ToString(), "text/plain").Wait();
49 }
50
51 App.Current?.On<Microsoft.Maui.Controls.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
52
53 }
54
55 protected override void OnResume()
56 {
57 base.OnResume();
58
59 if (nfcAdapter is not null)
60 {
61 Intent Intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop);
62
63 PendingIntent? PendingIntent = PendingIntent.GetActivity(this, 0, Intent, PendingIntentFlags.Mutable);
64 nfcAdapter.EnableForegroundDispatch(this, PendingIntent, null, null);
65 }
66
67 this.RemoveAllNotifications();
68 }
69
70 protected override void OnPause()
71 {
72 base.OnPause();
73 nfcAdapter?.DisableForegroundDispatch(this);
74 }
75
76 protected override async void OnNewIntent(Intent? Intent)
77 {
78 base.OnNewIntent(Intent);
79 await this.HandleIntent(Intent);
80 }
81
82 async Task HandleIntent(Intent? Intent)
83 {
84 if (Intent is null)
85 return;
86
87 try
88 {
89 switch (Intent.Action)
90 {
91 case Intent.ActionView:
92 string? Url = Intent?.Data?.ToString();
93 Console.WriteLine(Url);
94 if (!string.IsNullOrEmpty(Url))
95 App.OpenUrlSync(Url);
96 break;
97
98 case NfcAdapter.ActionTagDiscovered:
99 case NfcAdapter.ActionNdefDiscovered:
100 case NfcAdapter.ActionTechDiscovered:
101 Tag? Tag = Intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
102 if (Tag is null)
103 break;
104
105 byte[]? ID = Tag.GetId();
106 if (ID is null)
107 break;
108
109 string[]? TechList = Tag.GetTechList();
110 if (TechList is null)
111 break;
112
113 List<INfcInterface> Interfaces = [];
114
115 foreach (string Tech in TechList)
116 {
117 switch (Tech)
118 {
119 case "android.nfc.tech.IsoDep":
120 IsoDep? IsoDep = IsoDep.Get(Tag);
121 if (IsoDep is not null)
122 Interfaces.Add(new IsoDepInterface(Tag, IsoDep));
123 break;
124
125 case "android.nfc.tech.MifareClassic":
126 MifareClassic? MifareClassic = MifareClassic.Get(Tag);
127 if (MifareClassic is not null)
128 Interfaces.Add(new MifareClassicInterface(Tag, MifareClassic));
129 break;
130
131 case "android.nfc.tech.MifareUltralight":
132 MifareUltralight? MifareUltralight = MifareUltralight.Get(Tag);
133 if (MifareUltralight is not null)
134 Interfaces.Add(new MifareUltralightInterface(Tag, MifareUltralight));
135 break;
136
137 case "android.nfc.tech.Ndef":
138 Ndef? Ndef = Ndef.Get(Tag);
139 if (Ndef is not null)
140 Interfaces.Add(new NdefInterface(Tag, Ndef));
141 break;
142
143 case "android.nfc.tech.NdefFormatable":
144 NdefFormatable? NdefFormatable = NdefFormatable.Get(Tag);
145 if (NdefFormatable is not null)
146 Interfaces.Add(new NdefFormatableInterface(Tag, NdefFormatable));
147 break;
148
149 case "android.nfc.tech.NfcA":
150 NfcA? NfcA = NfcA.Get(Tag);
151 if (NfcA is not null)
152 Interfaces.Add(new NfcAInterface(Tag, NfcA));
153 break;
154
155 case "android.nfc.tech.NfcB":
156 NfcB? NfcB = NfcB.Get(Tag);
157 if (NfcB is not null)
158 Interfaces.Add(new NfcBInterface(Tag, NfcB));
159 break;
160
161 case "android.nfc.tech.NfcBarcode":
162 NfcBarcode? NfcBarcode = NfcBarcode.Get(Tag);
163 if (NfcBarcode is not null)
164 Interfaces.Add(new NfcBarcodeInterface(Tag, NfcBarcode));
165 break;
166
167 case "android.nfc.tech.NfcF":
168 NfcF? NfcF = NfcF.Get(Tag);
169 if (NfcF is not null)
170 Interfaces.Add(new NfcFInterface(Tag, NfcF));
171 break;
172
173 case "android.nfc.tech.NfcV":
174 NfcV? NfcV = NfcV.Get(Tag);
175 if (NfcV is not null)
176 Interfaces.Add(new NfcVInterface(Tag, NfcV));
177 break;
178 }
179 }
180
181 INfcService Service = App.Instantiate<INfcService>();
182 await Service.TagDetected(new NfcTag(ID, [.. Interfaces]));
183 break;
184 }
185 }
186 catch (Exception ex)
187 {
189 // TODO: Handle read & connection errors.
190 }
191 }
192
193 private void RemoveAllNotifications()
194 {
195 NotificationManager? Manager = (NotificationManager?)this.GetSystemService(NotificationService);
196 Manager?.CancelAll();
197 }
198 }
199}
The Application class, representing an instance of the Neuro-Access app.
Definition: App.xaml.cs:69
override async void OnResume()
Definition: App.xaml.cs:417
static void OpenUrlSync(string Url)
Opens an URL in the application.
Definition: App.xaml.cs:906
static new? App Current
Gets the current application, type casted to App.
Definition: App.xaml.cs:99
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Critical(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a critical event.
Definition: Log.cs:1017
Interface for the Near-Field Communication (NFC) Service.
Definition: INfcService.cs:11
Task TagDetected(INfcTag Tag)
Method called when a new NFC Tag has been detected.
Definition: App.xaml.cs:4