From 092081e82d71281d54cf416bdd0a8791a48f63d1 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Sat, 11 Feb 2023 14:54:55 +0100 Subject: [PATCH] Added: NTAG scan on Android --- Borepin/Borepin.Android/MainActivity.cs | 27 ++++++++++++++++--- .../Properties/AndroidManifest.xml | 16 ++++++----- .../Properties/AssemblyInfo.cs | 1 + Borepin/Borepin/Model/FabFireCard.cs | 26 +++++++++++++----- .../Borepin/PageModel/CreateCardPageModel.cs | 25 ++++++++++------- Borepin/Borepin/PageModel/UserPageModel.cs | 3 ++- 6 files changed, 71 insertions(+), 27 deletions(-) diff --git a/Borepin/Borepin.Android/MainActivity.cs b/Borepin/Borepin.Android/MainActivity.cs index 628ea20..dffd46d 100644 --- a/Borepin/Borepin.Android/MainActivity.cs +++ b/Borepin/Borepin.Android/MainActivity.cs @@ -1,16 +1,33 @@ - -using Android.App; +using Android.App; +using Android.Content; using Android.Content.PM; using Android.OS; using AndroidX.AppCompat.App; +using Java.Interop; namespace Borepin.Droid { - [Activity(Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] + [Activity(MainLauncher = true, Exported = true, Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] + [IntentFilter( + new[] + { + "android.nfc.action.NDEF_DISCOVERED" + }, + Categories = new[] + { + Intent.CategoryDefault + }, + DataScheme = "fabaccess", + DataHost = "innovisionlab.de" + )] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { + Android.Net.Uri test = Intent.Data; + string text = Intent.GetStringExtra("MyData") ?? "Data not available"; + + AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo; TabLayoutResource = Resource.Layout.Tabbar; @@ -24,6 +41,10 @@ namespace Borepin.Droid Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App(new PlatformInitializer())); } + protected override void OnNewIntent(Intent intent) + { + base.OnNewIntent(intent); + } public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { diff --git a/Borepin/Borepin.Android/Properties/AndroidManifest.xml b/Borepin/Borepin.Android/Properties/AndroidManifest.xml index 45d1042..77db275 100644 --- a/Borepin/Borepin.Android/Properties/AndroidManifest.xml +++ b/Borepin/Borepin.Android/Properties/AndroidManifest.xml @@ -1,9 +1,13 @@  - - - - - - \ No newline at end of file + + + + + + + + + + diff --git a/Borepin/Borepin.Android/Properties/AssemblyInfo.cs b/Borepin/Borepin.Android/Properties/AssemblyInfo.cs index 79beedf..94577a1 100644 --- a/Borepin/Borepin.Android/Properties/AssemblyInfo.cs +++ b/Borepin/Borepin.Android/Properties/AssemblyInfo.cs @@ -27,6 +27,7 @@ using Android.App; // Add some common permissions, these can be removed if not needed [assembly: UsesPermission(Android.Manifest.Permission.Internet)] [assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)] +[assembly: UsesPermission(Android.Manifest.Permission.Nfc)] //#if DEBUG //[assembly: Application(Debuggable=true)] diff --git a/Borepin/Borepin/Model/FabFireCard.cs b/Borepin/Borepin/Model/FabFireCard.cs index fccb91e..511ca93 100644 --- a/Borepin/Borepin/Model/FabFireCard.cs +++ b/Borepin/Borepin/Model/FabFireCard.cs @@ -13,8 +13,8 @@ namespace Borepin.Model public class FabFireCard { #region Private Fields - INFCService _NFCService; - IErrorMessageService _ErrorMessageService; + readonly INFCService _NFCService; + readonly IErrorMessageService _ErrorMessageService; #endregion #region Constructors @@ -85,20 +85,32 @@ namespace Borepin.Model card.AuthenticateISO_AES(0x00, MasterKey._Key); card.ChangeOtherKey_AES(0x01, AuthKey._Key, _Default_AESKey._Key, AuthKey._KeyVersion); - UInt16 accesRights = card.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00); + UInt16 accessRights = card.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00); - card.CreateFile_Standard(0x01, FileCommunication.PLAIN, accesRights, (uint)cardConfig.MetaInfo.Length); - card.CreateFile_Standard(0x02, FileCommunication.PLAIN, accesRights, (uint)cardConfig.SpaceInfo.Length); - card.CreateFile_Standard(0x03, FileCommunication.PLAIN, accesRights, (uint)cardConfig.CardToken.Length); + card.CreateFile_Standard(0x01, FileCommunication.PLAIN, accessRights, (uint)cardConfig.MetaInfo.Length); + card.CreateFile_Standard(0x02, FileCommunication.PLAIN, accessRights, (uint)cardConfig.SpaceInfo.Length); + card.CreateFile_Standard(0x03, FileCommunication.PLAIN, accessRights, (uint)47);// TODO (uint)cardConfig.CardToken.Length); card.WriteData(0x01, 0x00, cardConfig.MetaInfo); card.WriteData(0x02, 0x00, cardConfig.SpaceInfo); - card.WriteData(0x03, 0x00, cardConfig.CardToken); + card.WriteData(0x03, 0x00, _TODOFixDataFileSize(cardConfig.CardToken)); _NFCService.Disconnect(); return AuthKey._Key; } + + /// + /// TODO implement GetFileInfo in DESFire + /// + /// + /// + private byte[] _TODOFixDataFileSize(byte[] data) + { + byte[] array = ByteOperation.GenerateEmptyArray(47); + data.CopyTo(array, 0); + return array; + } #endregion } } diff --git a/Borepin/Borepin/PageModel/CreateCardPageModel.cs b/Borepin/Borepin/PageModel/CreateCardPageModel.cs index 7cbefca..a59609c 100644 --- a/Borepin/Borepin/PageModel/CreateCardPageModel.cs +++ b/Borepin/Borepin/PageModel/CreateCardPageModel.cs @@ -23,7 +23,6 @@ namespace Borepin.PageModel private CardConfig _CardConfig; private User _User; private INFCService _NFCService; - private User.ICardDESFireInterface _CardDESFireInterface; private IErrorMessageService _ErrorMessageService; private KeyTypes _ScanKeyType = KeyTypes.NONE; @@ -44,9 +43,6 @@ namespace Borepin.PageModel RandomAPPKeyCommand = new DelegateCommand(RandomAPPKeyCommandExecute); RefreshCommand = new DelegateCommand(async () => await RefreshCommandExecute().ConfigureAwait(true)); - - // TODO Use Server Interface - _CardDESFireInterface = new DESFireInterfaceDummy(); } #endregion @@ -281,9 +277,9 @@ namespace Borepin.PageModel _CardConfig.DoFormat = FormatCard; - _CardConfig.CardToken = (await _CardDESFireInterface.GenCardToken().ConfigureAwait(false)).ToArray(); - _CardConfig.SpaceInfo = (await _CardDESFireInterface.GetSpaceInfo().ConfigureAwait(false)).ToArray(); - _CardConfig.MetaInfo = (await _CardDESFireInterface.GetMetaInfo().ConfigureAwait(false)).ToArray(); + _CardConfig.CardToken = (await _User.CardDESFireEV2.GenCardToken().ConfigureAwait(false)).ToArray(); + _CardConfig.SpaceInfo = (await _User.CardDESFireEV2.GetSpaceInfo().ConfigureAwait(false)).ToArray(); + _CardConfig.MetaInfo = (await _User.CardDESFireEV2.GetMetaInfo().ConfigureAwait(false)).ToArray(); FabFireCard fabFireCard = new FabFireCard(_NFCService, _ErrorMessageService); @@ -294,7 +290,7 @@ namespace Borepin.PageModel fabFireCard.FormatCard(ReaderID, _CardConfig); } } - catch + catch(Exception exception) { Device.BeginInvokeOnMainThread(async () => { @@ -302,14 +298,17 @@ namespace Borepin.PageModel IsBusy = false; }); + + _NFCService.Disconnect(); + return; } try { byte[] key = fabFireCard.CreateCard(ReaderID, _CardConfig); - await _CardDESFireInterface.Bind(_CardConfig.CardToken, new List(key)).ConfigureAwait(false); + await _User.CardDESFireEV2.Bind(_CardConfig.CardToken, new List(key)).ConfigureAwait(false); } - catch + catch(Exception excpetion) { Device.BeginInvokeOnMainThread(async () => { @@ -317,8 +316,14 @@ namespace Borepin.PageModel IsBusy = false; }); + _NFCService.Disconnect(); + return; } + Device.BeginInvokeOnMainThread(async () => + { + INavigationResult result = await _NavigationService.GoBackAsync().ConfigureAwait(false); + }); IsBusy = false; } diff --git a/Borepin/Borepin/PageModel/UserPageModel.cs b/Borepin/Borepin/PageModel/UserPageModel.cs index d87831c..5c1bea9 100644 --- a/Borepin/Borepin/PageModel/UserPageModel.cs +++ b/Borepin/Borepin/PageModel/UserPageModel.cs @@ -100,7 +100,8 @@ namespace Borepin.PageModel CanCreateCard = !((User.CardDESFireInterface_Proxy)_User.CardDESFireEV2).IsNull; if(CanCreateCard) { - HasCardBinded = (await _User.CardDESFireEV2.GetTokenList().ConfigureAwait(false)).Count > 0; + IReadOnlyList> list = await _User.CardDESFireEV2.GetTokenList().ConfigureAwait(false); + HasCardBinded = list != null && list.Count > 0; } } #endregion