NFC csak Androidra javítás ...
This commit is contained in:
@@ -1,132 +1,88 @@
|
||||
using Plugin.NFC;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.MAUI.Pages.NFC;
|
||||
|
||||
public partial class NFCWaiter : ContentPage
|
||||
{
|
||||
private bool _nfcIsActive = false;
|
||||
private TaskCompletionSource<string> _taskCompletionSource = new();
|
||||
|
||||
private bool _eventsAlreadySubscribed = false;
|
||||
private bool _onReceiveing = false;
|
||||
private TaskCompletionSource<string> _taskCompletionSource;
|
||||
public static async Task<string> ShowModalAsync()
|
||||
{
|
||||
var page = new NFCWaiter();
|
||||
await Shell.Current.Navigation.PushModalAsync(page);
|
||||
return await page._taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
public NFCWaiter(TaskCompletionSource<string> taskCompletionSource)
|
||||
public NFCWaiter()
|
||||
{
|
||||
InitializeComponent();
|
||||
_taskCompletionSource = taskCompletionSource;
|
||||
}
|
||||
|
||||
protected override async void OnAppearing()
|
||||
{
|
||||
base.OnAppearing();
|
||||
|
||||
CrossNFC.Legacy = false;
|
||||
if (CrossNFC.IsSupported)
|
||||
#if ANDROID
|
||||
if (Platform.CurrentActivity is Android.App.Activity activity && activity is MainActivity mainActivity)
|
||||
{
|
||||
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");
|
||||
mainActivity.StartNfc(OnTagRead);
|
||||
_nfcIsActive = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
protected override void OnDisappearing()
|
||||
{
|
||||
base.OnDisappearing();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
async Task StopListening()
|
||||
{
|
||||
try
|
||||
{
|
||||
MainThread.BeginInvokeOnMainThread(() =>
|
||||
{
|
||||
CrossNFC.Current.StopListening();
|
||||
UnsubscribeEvents();
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//await ShowAlert(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
void SubscribeEvents()
|
||||
{
|
||||
if (_eventsAlreadySubscribed)
|
||||
UnsubscribeEvents();
|
||||
|
||||
_eventsAlreadySubscribed = true;
|
||||
|
||||
CrossNFC.Current.OnMessageReceived += OnMessageReceived_NFCData;
|
||||
|
||||
|
||||
}
|
||||
void UnsubscribeEvents()
|
||||
{
|
||||
CrossNFC.Current.OnMessageReceived -= OnMessageReceived_NFCData;
|
||||
_eventsAlreadySubscribed = false;
|
||||
}
|
||||
async void OnMessageReceived_NFCData(ITagInfo tagInfo)
|
||||
{
|
||||
_onReceiveing = true;
|
||||
|
||||
|
||||
|
||||
if (tagInfo == null)
|
||||
{
|
||||
await ShowAlert("No tag found");
|
||||
_onReceiveing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Customized serial number
|
||||
var identifier = tagInfo.Identifier;
|
||||
var serialNumber = NFCUtils.ByteArrayToHexString(identifier, ":");
|
||||
_taskCompletionSource.TrySetResult(serialNumber);
|
||||
_onReceiveing = false;
|
||||
|
||||
await Task.Delay(500);
|
||||
await StopListening();
|
||||
|
||||
await MainThread.InvokeOnMainThreadAsync(async () =>
|
||||
{
|
||||
await Navigation.PopModalAsync();
|
||||
//await Navigation.PopModalAsync();
|
||||
await Task.Delay(TimeSpan.FromSeconds(30));
|
||||
Finish(null);
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
Task ShowAlert(string message, string title = null) => DisplayAlert(string.IsNullOrWhiteSpace(title) ? "NFC olvasás" : title, message, "OK");
|
||||
|
||||
protected override void OnDisappearing()
|
||||
{
|
||||
|
||||
#if ANDROID
|
||||
if (Platform.CurrentActivity is Android.App.Activity activity && activity is MainActivity mainActivity)
|
||||
{
|
||||
mainActivity.StopNfc();
|
||||
}
|
||||
#endif
|
||||
base.OnDisappearing();
|
||||
}
|
||||
|
||||
private void OnTagRead(string tagId)
|
||||
{
|
||||
Finish(tagId);
|
||||
}
|
||||
|
||||
private async void Finish(string tagId)
|
||||
{
|
||||
#if ANDROID
|
||||
if (Platform.CurrentActivity is Android.App.Activity activity && activity is MainActivity mainActivity)
|
||||
{
|
||||
if (_nfcIsActive)
|
||||
{
|
||||
mainActivity.StopNfc();
|
||||
_nfcIsActive = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!_taskCompletionSource.Task.IsCompleted)
|
||||
_taskCompletionSource.SetResult(tagId);
|
||||
|
||||
if (Navigation.ModalStack.Count > 0)
|
||||
{
|
||||
await Shell.Current.Navigation.PopModalAsync();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user