阅读带有未知标签类型Xamarin的Android NFC标签 - C#

时间:2016-12-09 11:28:47

标签: c# android xamarin nfc

我想在我的Android应用程序中使用NFC技术。

我希望用户能够扫描 NFC卡,以便他们可以登录该应用程序。该卡包含一个号码。

所以这个想法只是检索代码并将其转换为字符串

但我得到Nullobjectreference

  

尝试调用虚方法' boolean   android.nfc.Tag.hasTech(INT)'在空对象引用上

更新

源代码:

using System;
using System.Text;
using Android.App;
using Android.Nfc;
using Android.OS;
using Android.Widget;
using Android.Content;
using Android.Nfc.Tech;

namespace NFC_NDEF
{
[Activity(Label = "NFC_NDEF_TST", MainLauncher = true, Icon = "@drawable/icon")]
[IntentFilter(new[] { NfcAdapter.ActionTechDiscovered })]

public class MainActivity : Activity 
{
NfcAdapter nfcAdapter;
PendingIntent nfcPi;
IntentFilter nfcFilter;
Tag nfcTag;
string newLine = System.Environment.NewLine;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);

    // Get our button from the layout resource,
    // and attach an event to it
    Button btnScan = FindViewById<Button>(Resource.Id.MyButton);
    btnScan.Click += Scan;

    //var writerButton = FindViewById<Button>(Resource.Id.WriteButton);
    //writerButton.Click += Write;

    var label = FindViewById<TextView>(Resource.Id.textView_rslt);

    nfcAdapter = NfcAdapter.GetDefaultAdapter(ApplicationContext);
    if (nfcAdapter == null)
    {
        label.Text = "NFC is not available.";
        return;
    }

    if (!nfcAdapter.IsEnabled)
    {
        label.Text = "NFC is disabled.";
        return;
    }


    var intent = new Intent(this, this.Class);
    intent.AddFlags(ActivityFlags.SingleTop);
    nfcPi = PendingIntent.GetActivity(this, 0, intent, 0);
    nfcFilter = new IntentFilter(NfcAdapter.ActionTechDiscovered);
    nfcFilter.AddCategory(Intent.CategoryDefault);
}

private void Scan(object sender, EventArgs e)
{
    var label = FindViewById<TextView>(Resource.Id.textView_rslt);

    try
    {
        if (nfcTag == null)
        {
            label.Text = "nfc tag is null";
            return;
        }

        var ndef = Ndef.Get(nfcTag);
        ndef.Connect();
        var data = Encoding.UTF8.GetString(ndef.NdefMessage.ToByteArray());
        ndef.Close();

        label.Text = $"Data:{newLine}{data}";
    }
    catch (Exception ex)
    {
        label.Text += $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}";
    }
}

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

    nfcAdapter.EnableForegroundDispatch(this, nfcPi, new IntentFilter[] { nfcFilter }, null);

    if (NfcAdapter.ActionTechDiscovered == Intent.Action)
    {
        ProcessIntent(Intent);
    }
}

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);
    Intent = intent;
    if (NfcAdapter.ActionTechDiscovered == intent.Action)
    {
        ProcessIntent(Intent);
    }
}

private void ProcessIntent(Intent intent)
{
    var label = FindViewById<TextView>(Resource.Id.textView_rslt);

    try
    {
        nfcTag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

        label.Text = $"Id: 0x{Smartisan.Nfc.Utils.ByteToHex(nfcTag.GetId())}";
        label.Text += $"{newLine}Techs: {newLine}";
        label.Text += string.Join(newLine, nfcTag.GetTechList());
    }
    catch (Exception ex)
    {
        label.Text += $"{newLine} Exception: {newLine} {ex.Message} {newLine} {ex.StackTrace}";
    }
}

}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="NFC_NDEF.NFC_NDEF" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-sdk android:targetSdkVersion="15" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
<application android:label="NFC_NDEF"></application>
</manifest>

1 个答案:

答案 0 :(得分:0)

问题只是过滤卡NFCA的类型 它在使用https://github.com/poz1/NFCForms

之后起作用了