Created
September 27, 2017 04:45
-
-
Save xtellurian/e2ab0e5048cfd1362c41ca229d597fd0 to your computer and use it in GitHub Desktop.
Xamarin.Android - Using NFC, and reading HEX id's
This file contains hidden or 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 System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Android.Nfc; | |
using MyApp.Contract; | |
using MyApp.Contract.EventArgs; | |
using Xamarin.Forms; | |
namespace MyApp.Droid.Services | |
{ | |
class DroidNfcService : INfcService | |
{ | |
private static DroidNfcService _instance; | |
public static DroidNfcService Instance => _instance ?? (_instance = new DroidNfcService()); | |
private DroidNfcService() | |
{ | |
} | |
public event EventHandler<NfcEventArgs> NfcFound; | |
internal void OnTagUuidFound(string idHex) | |
{ | |
NfcFound?.Invoke(this, new NfcEventArgs(idHex)); | |
} | |
} | |
} |
This file contains hidden or 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 System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using MyApp.Contract.EventArgs; | |
namespace MyApp.Contract | |
{ | |
public interface INfcService | |
{ | |
event EventHandler<NfcEventArgs> NfcFound; | |
} | |
} |
This file contains hidden or 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 System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.Content.PM; | |
using Android.Nfc; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Android.OS; | |
namespace MyApp.Droid | |
{ | |
[Activity( Label = "bmx", Icon = "@drawable/icon", Theme = "@style/MainTheme", | |
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTop)] | |
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity | |
{ | |
protected override void OnCreate(Bundle bundle) | |
{ | |
TabLayoutResource = Resource.Layout.Tabbar; | |
ToolbarResource = Resource.Layout.Toolbar; | |
base.OnCreate(bundle); | |
Window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds); | |
Window.SetStatusBarColor(Android.Graphics.Color.Black); | |
global::Xamarin.Forms.Forms.Init(this, bundle); | |
LoadApplication(new App()); // Xamarin.Forms app | |
} | |
protected override void OnResume() | |
{ | |
base.OnResume(); | |
SetupNfc(); | |
} | |
private void SetupNfc() | |
{ | |
var adapter = NfcAdapter.GetDefaultAdapter(Application.Context); | |
var tagDetected = new IntentFilter(NfcAdapter.ActionTagDiscovered); | |
var filters = new[] { tagDetected }; | |
var intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop); | |
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0); | |
adapter.EnableForegroundDispatch(this, pendingIntent, filters, null); | |
} | |
protected override void OnNewIntent(Intent intent) | |
{ | |
if (intent.Action == NfcAdapter.ActionTagDiscovered) | |
{ | |
var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; | |
if (tag != null) | |
{ | |
var idBytes = tag.GetId(); | |
var id = idBytes.ToHexString(); | |
// singleton handler class | |
DroidNfcService.Instance.OnTagUuidFound(id); | |
} | |
} | |
} | |
} | |
} | |
This file contains hidden or 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 System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace MyApp.Contract.EventArgs | |
{ | |
public class NfcEventArgs : System.EventArgs | |
{ | |
public NfcEventArgs(string idHex) | |
{ | |
IdHex = idHex; | |
} | |
public string IdHex { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment