Home > Net >  Xamarin.Android Disable NFC discober events in MainActivity
Xamarin.Android Disable NFC discober events in MainActivity

Time:06-07

As the title suggests, I would like to disable / hide all events related to NFC discovery.

My App don't use NFC and my goal is to hide automatic pop-ups, from Google Pay for example.

I find these threads here that help me to find a way to solution

1: Android app enable NFC only for one Activity

2: Android app enable NFC only for one Activity

This is my MainActivity:

[Activity(Label = "Title", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        //CrossNFC.Init(this);


        var intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop);
        PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Mutable);



        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        LoadApplication(new App());
    }


    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    protected override void OnResume()
    {
        base.OnResume();


        NfcAdapter nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
        var intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop);
        PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.Mutable);
        nfcAdapter.EnableForegroundDispatch(this, pendingIntent, null, null);

        //CrossNFC.OnResume();
    }

    protected override void OnPause()
    {
        base.OnPause();

        NfcAdapter nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
        nfcAdapter.DisableForegroundDispatch(this);
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);

        if (NfcAdapter.ActionTagDiscovered.Equals(intent.Action))
            return;
    }
}

But when I try it(I have two NFC tags for testing and 1 App that works like NFC card emulation) with 1 I can catch the event onNewIntent but the other two are not even revealed.

If I use Plugin.NFC nugget, I can detect 2 of my 3 tags..

Am I doing something wrong?

Thanks

CodePudding user response:

First I would not use EnableForegroundDispatch to try and capture and hide NFC events. Even if you set to get the system NFC service will still make a sound when it sees a Tag before sending it to you to ignore.

It also it does not disable Android Beam functionality (when available on older Android versions), this might be getting picked up if you are emulating a Tag on an Android Phone before the Tag is picked up.

I would recommend using the newer and better enableReaderMode API as this disables Android Beam and can disable the System NFC service sound as well.

Java example of enableReaderMode

  • Related