diff --git a/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/MainActivity.cs b/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/MainActivity.cs index 79ae32b..e23a7af 100644 --- a/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/MainActivity.cs +++ b/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/MainActivity.cs @@ -9,7 +9,8 @@ namespace NFCSample.Droid { [Activity(Label = "NFCSample", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode = LaunchMode.SingleTask)] [IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = MainPage.MIME_TYPE)] - public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity + [MetaData(NfcAdapter.ActionTechDiscovered, Resource = "@xml/nfc_tech_filters")] + public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { diff --git a/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/Plugin.NFC.Forms.Sample.Android.csproj b/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/Plugin.NFC.Forms.Sample.Android.csproj index d567e51..b47bc76 100644 --- a/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/Plugin.NFC.Forms.Sample.Android.csproj +++ b/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/Plugin.NFC.Forms.Sample.Android.csproj @@ -92,6 +92,10 @@ + + + + @@ -99,6 +103,7 @@ + diff --git a/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/Resources/xml/nfc_tech_filters.xml b/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/Resources/xml/nfc_tech_filters.xml new file mode 100644 index 0000000..0974750 --- /dev/null +++ b/src/Plugin.NFC.Forms.Sample/Plugin.NFC.Forms.Sample.Android/Resources/xml/nfc_tech_filters.xml @@ -0,0 +1,8 @@ + + + + android.nfc.tech.NfcA + + android.nfc.tech.MifareClassic + + diff --git a/src/Plugin.NFC.Maui.Sample/Platforms/Android/MainActivity.cs b/src/Plugin.NFC.Maui.Sample/Platforms/Android/MainActivity.cs index 372af46..62ce7a3 100644 --- a/src/Plugin.NFC.Maui.Sample/Platforms/Android/MainActivity.cs +++ b/src/Plugin.NFC.Maui.Sample/Platforms/Android/MainActivity.cs @@ -8,6 +8,7 @@ namespace Plugin.NFC.Maui.Sample { [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] [IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered }, Categories = new[] { Intent.CategoryDefault }, DataMimeType = MainPage.MIME_TYPE)] + [MetaData(NfcAdapter.ActionTechDiscovered, Resource = "@xml/nfc_tech_filters")] public class MainActivity : MauiAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) diff --git a/src/Plugin.NFC.Maui.Sample/Platforms/Android/Resources/xml/nfc_tech_filters.xml b/src/Plugin.NFC.Maui.Sample/Platforms/Android/Resources/xml/nfc_tech_filters.xml new file mode 100644 index 0000000..0974750 --- /dev/null +++ b/src/Plugin.NFC.Maui.Sample/Platforms/Android/Resources/xml/nfc_tech_filters.xml @@ -0,0 +1,8 @@ + + + + android.nfc.tech.NfcA + + android.nfc.tech.MifareClassic + + diff --git a/src/Plugin.NFC.Maui.Sample/Plugin.NFC.Maui.Sample.csproj b/src/Plugin.NFC.Maui.Sample/Plugin.NFC.Maui.Sample.csproj index e8d0f5c..21eb1df 100644 --- a/src/Plugin.NFC.Maui.Sample/Plugin.NFC.Maui.Sample.csproj +++ b/src/Plugin.NFC.Maui.Sample/Plugin.NFC.Maui.Sample.csproj @@ -47,4 +47,7 @@ + + + diff --git a/src/Plugin.NFC/Android/NFC.android.cs b/src/Plugin.NFC/Android/NFC.android.cs index da90162..01cb6a8 100644 --- a/src/Plugin.NFC/Android/NFC.android.cs +++ b/src/Plugin.NFC/Android/NFC.android.cs @@ -14,8 +14,8 @@ namespace Plugin.NFC /// /// Android implementation of /// - public class NFCImplementation : INFC - { + public class NFCImplementation : Java.Lang.Object, INFC, NfcAdapter.IReaderCallback + { public event EventHandler OnTagConnected; public event EventHandler OnTagDisconnected; public event NdefMessageReceivedEventHandler OnMessageReceived; @@ -92,33 +92,11 @@ public void StartListening() if (_nfcAdapter == null) return; - var intent = new Intent(CurrentActivity, CurrentActivity.GetType()).AddFlags(ActivityFlags.SingleTop); + var flags = NfcReaderFlags.NfcA | NfcReaderFlags.NfcB | NfcReaderFlags.NfcF + | NfcReaderFlags.NfcV | NfcReaderFlags.NfcBarcode | NfcReaderFlags.SkipNdefCheck; + _nfcAdapter.EnableReaderMode(CurrentActivity, this, flags, null); - // We don't use MonoAndroid12.0 as targetframework for easier backward compatibility: - // MonoAndroid12.0 needs JDK 11. - PendingIntentFlags pendingIntentFlags = 0; - -#if NET6_0_OR_GREATER - if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.S) - pendingIntentFlags = PendingIntentFlags.Mutable; -#else - if ((int)Android.OS.Build.VERSION.SdkInt >= 31) //Android.OS.BuildVersionCodes.S - pendingIntentFlags = (PendingIntentFlags)33554432; //PendingIntentFlags.Mutable -#endif - - var pendingIntent = PendingIntent.GetActivity(CurrentActivity, 0, intent, pendingIntentFlags); - - var ndefFilter = new IntentFilter(NfcAdapter.ActionNdefDiscovered); - ndefFilter.AddDataType("*/*"); - - var tagFilter = new IntentFilter(NfcAdapter.ActionTagDiscovered); - tagFilter.AddCategory(Intent.CategoryDefault); - - var filters = new IntentFilter[] { ndefFilter, tagFilter }; - - _nfcAdapter.EnableForegroundDispatch(CurrentActivity, pendingIntent, filters, null); - - _isListening = true; + _isListening = true; OnTagListeningStatusChanged?.Invoke(_isListening); } @@ -129,7 +107,7 @@ public void StopListening() { DisablePublishing(); if (_nfcAdapter != null) - _nfcAdapter.DisableForegroundDispatch(CurrentActivity); + _nfcAdapter.DisableReaderMode(CurrentActivity); _isListening = false; OnTagListeningStatusChanged?.Invoke(_isListening); @@ -265,39 +243,31 @@ internal void WriteOrClearMessage(ITagInfo tagInfo, bool clearMessage = false, b } } - /// - /// Handle Android OnNewIntent - /// - /// Android - internal void HandleNewIntent(Intent intent) - { - if (intent == null) - return; - - if (intent.Action == NfcAdapter.ActionTagDiscovered || intent.Action == NfcAdapter.ActionNdefDiscovered) - { - _currentTag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; - if (_currentTag != null) - { - var nTag = GetTagInfo(_currentTag); - if (_isWriting) - { - // Write mode - OnTagDiscovered?.Invoke(nTag, _isFormatting); - } - else - { - // Read mode - OnMessageReceived?.Invoke(nTag); - } - } - } - } - - /// - /// Handle Android OnResume - /// - internal void HandleOnResume() + void NfcAdapter.IReaderCallback.OnTagDiscovered(Tag tag) + { + _currentTag = tag; + + if (_currentTag != null) + { + var nTag = GetTagInfo(_currentTag); + + if (_isWriting) + { + // Write mode + OnTagDiscovered?.Invoke(nTag, _isFormatting); + } + else + { + // Read mode + OnMessageReceived?.Invoke(nTag); + } + } + } + + /// + /// Handle Android OnResume + /// + internal void HandleOnResume() { // Android 10 fix: // If listening mode is already enable, we restart listening when activity is resumed