Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

NFC Implementation in Android Custom Renderer

$
0
0

Hi ,

We have build an application in xamarin.forms for supporting NFC to receive and send message from one device to another.

We tried the same in Xamarin.Android and it works fine. Now we want to integrate the same with xamarin.forms application.

For this,
- We tried to call the activity from forms page and throws error as null object for initializing nfc adapter.

  • Next, we tried to make use of page renderer approach. But, there is no lifecycle like activity lifecycle.

Anyone, please suggest us to implement NFC in Xamarin.Forms Application.

Xamarin Android Code:

[Activity(Label = "BeamActivity", MainLauncher = true, Theme = "@android:style/Theme.Material.Light")]
[IntentFilter(new[] {"android.nfc.action.NDEF_DISCOVERED"}, Categories = new[] { Intent.CategoryDefault}, DataMimeType = "application/com.sample.beam")]
public class BeamActivity : Activity, NfcAdapter.ICreateNdefMessageCallback
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);
    }

    protected override void OnResume()
    {
        base.OnResume();
        NfcAdapter nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
        nfcAdapter.SetNdefPushMessageCallback(this, this);
        if (NfcAdapter.ActionNdefDiscovered == Intent.Action)
        {
            extractPayload(Intent);
        }
    }

    void extractPayload(Intent intent)
    {
        IParcelable[] messages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
        NdefMessage msg = (NdefMessage)messages[0];
        NdefRecord record = msg.GetRecords()[0];
        TextView text = FindViewById<TextView>(Resource.Id.text);
        text.Text = Encoding.ASCII.GetString(record.GetPayload());
    }

    public NdefMessage CreateNdefMessage(NfcEvent e)
    {
        return createMessage();
    }

    private NdefMessage createMessage()
    {
        string mimeType = "application/com.sample.beam";
        byte[] mimeBytes = Encoding.ASCII.GetBytes(mimeType);

        //GENERATE PAYLOAD
        TextView text = FindViewById<TextView>(Resource.Id.text);
        byte[] payLoad = Encoding.ASCII.GetBytes(text.Text);

        //GENERATE NFC MESSAGE
        return new NdefMessage(
                new NdefRecord[]{
                    new NdefRecord(NdefRecord.TnfMimeMedia,
                            mimeBytes,
                            null,
                            payLoad),
                    NdefRecord.CreateApplicationRecord("com.sample.beam")
                });
    }

Kindly advise, as we need to use the above code in Xamarin Forms app with Custom Renderer Approach in Android platform.?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>