Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Use newer API to read tags #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@
<AndroidResource Include="Resources\mipmap-xxhdpi\launcher_foreground.png" />
<AndroidResource Include="Resources\mipmap-xxxhdpi\icon.png" />
<AndroidResource Include="Resources\mipmap-xxxhdpi\launcher_foreground.png" />
<AndroidResource Include="Resources\xml\nfc_tech_filters.xml">
<SubType></SubType>
<Generator></Generator>
</AndroidResource>
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\drawable-hdpi\" />
<Folder Include="Resources\drawable-xhdpi\" />
<Folder Include="Resources\drawable-xxhdpi\" />
<Folder Include="Resources\drawable-xxxhdpi\" />
<Folder Include="Resources\drawable\" />
<Folder Include="Resources\xml\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Plugin.NFC\Plugin.NFC.csproj">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
<!-- Support old tags -->
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
<!-- Support old tags -->
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
</resources>
3 changes: 3 additions & 0 deletions src/Plugin.NFC.Maui.Sample/Plugin.NFC.Maui.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@
<ProjectReference Include="..\Plugin.NFC\Plugin.NFC.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Platforms\Android\Resources\xml\" />
</ItemGroup>
</Project>
94 changes: 32 additions & 62 deletions src/Plugin.NFC/Android/NFC.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace Plugin.NFC
/// <summary>
/// Android implementation of <see cref="INFC"/>
/// </summary>
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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -129,7 +107,7 @@ public void StopListening()
{
DisablePublishing();
if (_nfcAdapter != null)
_nfcAdapter.DisableForegroundDispatch(CurrentActivity);
_nfcAdapter.DisableReaderMode(CurrentActivity);

_isListening = false;
OnTagListeningStatusChanged?.Invoke(_isListening);
Expand Down Expand Up @@ -265,39 +243,31 @@ internal void WriteOrClearMessage(ITagInfo tagInfo, bool clearMessage = false, b
}
}

/// <summary>
/// Handle Android OnNewIntent
/// </summary>
/// <param name="intent">Android <see cref="Intent"/></param>
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);
}
}
}
}

/// <summary>
/// Handle Android OnResume
/// </summary>
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);
}
}
}

/// <summary>
/// Handle Android OnResume
/// </summary>
internal void HandleOnResume()
{
// Android 10 fix:
// If listening mode is already enable, we restart listening when activity is resumed
Expand Down