Skip to content

Instantly share code, notes, and snippets.

View jpd21122012's full-sized avatar
:octocat:

Ing. Jorge Perales Díaz jpd21122012

:octocat:
View GitHub Profile
public partial class MainPage : ContentPage
{
private readonly INfcService _nfcService;
public MainPage(INfcService nfcService)
{
_nfcService = nfcService;
InitializeComponent();
}
<VerticalStackLayout>
<Button Text="Read NFC Tag" Clicked="OnReadClicked" />
<Button Text="Write to NFC Tag" Clicked="OnWriteClicked" />
<Label x:Name="OutputLabel" Text="NFC Output will appear here" />
</VerticalStackLayout>
public Task<bool> WriteTagAsync(string text)
{
// iOS uses NFCNDEFReaderSession with write capability
var record = new NFCNDEFPayload(
NFCTypeNameFormat.NFCWellKnown,
"T".ToArray(), // Text type
"en".ToArray(),
text.ToArray()
);
// Write using NFCNDEFReaderSession
public async Task<bool> WriteTagAsync(string text)
{
NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[] {
NdefRecord.CreateTextRecord("en", text)
}
);
// Write to tag using Ndef.WriteNdefMessage
return true;
}
builder.Services.AddSingleton<INfcService>(_ =>
{
#if ANDROID
return new NfcServiceAndroid();
#elif IOS
return new NfcServiceiOS();
#else
return null;
#endif
});
using CoreNFC;
public class NfcServiceiOS : INfcService
{
private NFCNdefReaderSession _nfcSession;
public Task<string> ReadTagAsync()
{
// Start an NFC reader session
_nfcSession = new NFCNdefReaderSession(this, null, true);
using Android.Nfc;
using Android.Nfc.Tech;
public class NfcServiceAndroid : INfcService
{
public async Task<string> ReadTagAsync()
{
// Implementation using Android.Nfc
// Example: Listen for NDEF messages
return await Task.FromResult("NFC Tag Data from Android");
public interface INfcService
{
Task<string> ReadTagAsync();
Task<bool> WriteTagAsync(string text);
}
<key>NFCReaderUsageDescription</key>
<string>This app uses NFC to read and write tags</string>
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
<string>NDEF</string>
</array>
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />