NFC Test vol 1.

This commit is contained in:
2025-01-24 14:37:21 +01:00
parent 0d44cd48fb
commit e74df61ca0
9 changed files with 183 additions and 1 deletions
+2
View File
@@ -5,10 +5,12 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:WorkFlowCheck.MAUI"
xmlns:security="clr-namespace:WorkFlowCheck.MAUI.Pages.Security"
xmlns:nfc="clr-namespace:WorkFlowCheck.MAUI.Pages.NFC"
Shell.FlyoutBehavior="Disabled"
Title="WorkFlowCheck.MAUI">
<ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" />
<ShellContent Title="Login" ContentTemplate="{DataTemplate security:LoginPage}" Route="LoginPage" />
<ShellContent Title="NFC Test" ContentTemplate="{DataTemplate nfc:NFCTestPage}" Route="NFCTestPage" />
</Shell>
+6
View File
@@ -37,6 +37,12 @@
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
<Button
x:Name="NFCTestBtn"
Text="NFC Test"
SemanticProperties.Hint="Test NFC capability"
Clicked="OnButtonNFCTest"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>
+4
View File
@@ -44,6 +44,10 @@ namespace WorkFlowCheck.MAUI
}
}
private async void OnButtonNFCTest(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("//NFCTestPage");
}
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WorkFlowCheck.MAUI.Pages.NFC.NFCTestPage"
Title="NFCTestPage">
<VerticalStackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ContentPage>
@@ -0,0 +1,130 @@
using Plugin.NFC;
using System.Text;
namespace WorkFlowCheck.MAUI.Pages.NFC;
public partial class NFCTestPage : ContentPage
{
public NFCTestPage()
{
InitializeComponent();
}
public const string ALERT_TITLE = "NFC";
bool _eventsAlreadySubscribed = false;
protected override async void OnAppearing()
{
base.OnAppearing();
CrossNFC.Legacy = false;
if (CrossNFC.IsSupported)
{
if (!CrossNFC.Current.IsAvailable)
await ShowAlert("NFC is not available");
if (!CrossNFC.Current.IsEnabled)
await ShowAlert("NFC is disabled");
await AutoStartAsync().ConfigureAwait(false);
}
else
{
await ShowAlert("NFC is not available");
}
}
Task ShowAlert(string message, string title = null) => DisplayAlert(string.IsNullOrWhiteSpace(title) ? ALERT_TITLE : title, message, "OK");
async Task AutoStartAsync()
{
// Some delay to prevent Java.Lang.IllegalStateException "Foreground dispatch can only be enabled when your activity is resumed" on Android
await Task.Delay(500);
await StartListeningIfNotiOS();
}
async Task StartListeningIfNotiOS()
{
await BeginListening();
}
async Task BeginListening()
{
try
{
MainThread.BeginInvokeOnMainThread(() =>
{
SubscribeEvents();
CrossNFC.Current.StartListening();
});
}
catch (Exception ex)
{
await ShowAlert(ex.Message);
}
}
void SubscribeEvents()
{
if (_eventsAlreadySubscribed)
UnsubscribeEvents();
_eventsAlreadySubscribed = true;
CrossNFC.Current.OnMessageReceived += Current_OnMessageReceived;
//CrossNFC.Current.OnMessagePublished += Current_OnMessagePublished;
//CrossNFC.Current.OnTagDiscovered += Current_OnTagDiscovered;
//CrossNFC.Current.OnNfcStatusChanged += Current_OnNfcStatusChanged;
//CrossNFC.Current.OnTagListeningStatusChanged += Current_OnTagListeningStatusChanged;
}
void UnsubscribeEvents()
{
CrossNFC.Current.OnMessageReceived -= Current_OnMessageReceived;
//CrossNFC.Current.OnMessagePublished -= Current_OnMessagePublished;
//CrossNFC.Current.OnTagDiscovered -= Current_OnTagDiscovered;
//CrossNFC.Current.OnNfcStatusChanged -= Current_OnNfcStatusChanged;
//CrossNFC.Current.OnTagListeningStatusChanged -= Current_OnTagListeningStatusChanged;
_eventsAlreadySubscribed = false;
}
async void Current_OnMessageReceived(ITagInfo tagInfo)
{
if (tagInfo == null)
{
await ShowAlert("No tag found");
return;
}
// Customized serial number
var identifier = tagInfo.Identifier;
var serialNumber = NFCUtils.ByteArrayToHexString(identifier, ":");
var title = !string.IsNullOrWhiteSpace(serialNumber) ? $"Tag [{serialNumber}]" : "Tag Info";
if (!tagInfo.IsSupported)
{
await ShowAlert("Unsupported tag (app)", title);
}
else if (tagInfo.IsEmpty)
{
await ShowAlert("Empty tag", title);
}
else
{
var first = tagInfo.Records[0];
await ShowAlert(GetMessage(first), title);
}
}
string GetMessage(NFCNdefRecord record)
{
var message = $"Message: {record.Message}";
message += Environment.NewLine;
message += $"RawMessage: {Encoding.UTF8.GetString(record.Payload)}";
message += Environment.NewLine;
message += $"Type: {record.TypeFormat}";
if (!string.IsNullOrWhiteSpace(record.MimeType))
{
message += Environment.NewLine;
message += $"MimeType: {record.MimeType}";
}
return message;
}
}
@@ -21,7 +21,7 @@ public partial class LoginPage : ContentPage
await DisplayAlert("Success", "You are now logged in.", "OK");
// Visszatérés a MainPage-re
await Navigation.PopAsync();
await Shell.Current.GoToAsync("//MainPage");
}
else
{
@@ -3,4 +3,6 @@
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="false" />
</manifest>
@@ -1,6 +1,8 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Plugin.NFC;
namespace WorkFlowCheck.MAUI
{
@@ -12,5 +14,26 @@ namespace WorkFlowCheck.MAUI
SecureStorage.Default.Remove("WFCUser");
base.OnDestroy();
}
protected override void OnCreate(Bundle savedInstanceState)
{
// Plugin NFC: Initialization before base.OnCreate(...) (Important on .NET MAUI)
CrossNFC.Init(this);
base.OnCreate(savedInstanceState);
}
protected override void OnResume()
{
base.OnResume();
// Plugin NFC: Restart NFC listening on resume (needed for Android 10+)
CrossNFC.OnResume();
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
// Plugin NFC: Tag Discovery Interception
CrossNFC.OnNewIntent(intent);
}
}
}
@@ -73,6 +73,9 @@
</ItemGroup>
<ItemGroup>
<MauiXaml Update="Pages\NFC\NFCTestPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="Pages\Security\LoginPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>