Created
August 23, 2022 16:30
-
-
Save FreakyAli/d6872bfc3c51367e95941e5c4410cebe to your computer and use it in GitHub Desktop.
NFC service for iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using CoreNFC; | |
using NDEF.MAUI.Enums; | |
using NDEF.MAUI.Interfaces; | |
using UIKit; | |
public class NfcService : INfcService | |
{ | |
public async Task SendAsync(byte[] bytes) | |
{ | |
var isNfcAvailable = UIDevice.CurrentDevice.CheckSystemVersion(11, 0); | |
if (isNfcAvailable && NFCNdefReaderSession.ReadingAvailable) | |
{ | |
await MainThread.InvokeOnMainThreadAsync(async () => | |
{ | |
try | |
{ | |
var sessionDelegate = new SessionDelegate(bytes); | |
var session = new NFCNdefReaderSession(sessionDelegate, null, true); | |
session.BeginSession(); | |
var status = await sessionDelegate.WasDataTransmitted.Task; | |
if (status != NfcTransmissionStatus.Success) | |
{ | |
await Application.Current.MainPage.DisplayAlert("Error", "Suitable error message", "Ok"); | |
} | |
} | |
catch | |
{ | |
await Application.Current.MainPage.DisplayAlert("Error", "There was an error while trying to create a NFC session", "Ok"); | |
} | |
}); | |
} | |
else | |
{ | |
await Application.Current.MainPage.DisplayAlert("Error", "Read is not supported by this tag", "Ok"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment