我是Xamarin跨平台应用程序开发的新手,我正在尝试在Android版本的应用程序中实现外部NFC标签读取。
扫描标签后,我希望我的应用程序打开并读取标签内的文本,并最终根据读取的内容执行一些特定的操作。
我已经在MainActivity.cs上实现了该实现,但是它无法正常工作,因为似乎我听不到意图:
using Android.Content;
using System;
using System.Text;
using System.Diagnostics;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Xamarin.Forms;
using Android.Content.Res;
using FFImageLoading.Forms.Platform;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
using Plugin.CurrentActivity;
using Android.Nfc;
using Android.Nfc.Tech;
using Poz1.NFCForms.Abstract;
using Poz1.NFCForms.Droid;
namespace Kibelis.Droid
{
[Activity(Label = "Kibelis", Icon = "@drawable/icon", Theme = "@style/MainTheme", LaunchMode = LaunchMode.SingleTop, MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
private NfcAdapter _nfcAdapter;
public object NFCUtil { get; private set; }
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
CachedImageRenderer.Init(true);
base.OnCreate(savedInstanceState);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
_nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
System.Diagnostics.Debug.WriteLine("CREATE");
// is attached.
LoadApplication(new App());
}
protected override void OnResume()
{
base.OnResume();
if (_nfcAdapter == null)
{
System.Diagnostics.Debug.WriteLine("NFC UNAVIABLE");
}
else
{
var tagDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered);
var filters = new[] { tagDetected };
var intent = new Intent(this, this.GetType()).AddFlags(ActivityFlags.SingleTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
_nfcAdapter.EnableForegroundDispatch(this, pendingIntent, filters, null);
System.Diagnostics.Debug.WriteLine("FOREGRAUND DISPATCH");
}
}
protected override void OnPause()
{
base.OnPause();
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
System.Diagnostics.Debug.WriteLine("NEW INTENT");
if (intent.Extras.IsEmpty)
{
System.Diagnostics.Debug.WriteLine("empty");
}
else
{
System.Diagnostics.Debug.WriteLine("Not empty");
}
//For start reading
if (intent.Action == NfcAdapter.ActionTagDiscovered || intent.Action == NfcAdapter.ActionNdefDiscovered || intent.Action == NfcAdapter.ActionAdapterStateChanged
|| intent.Action == NfcAdapter.ActionTransactionDetected || intent.Action == NfcAdapter.ExtraNdefMessages || intent.Action == NfcAdapter.ExtraNdefMessages)
{
System.Diagnostics.Debug.WriteLine("DISCOVERD");
var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
if (tag != null)
{
System.Diagnostics.Debug.WriteLine("TAG");
// First get all the NdefMessage
var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
if (rawMessages != null)
{
var msg = (NdefMessage)rawMessages[0];
System.Diagnostics.Debug.WriteLine("MESSAGE");
// Get NdefRecord which contains the actual data
var record = msg.GetRecords()[0];
if (record != null)
{
if (record.Tnf == NdefRecord.TnfWellKnown)
{
// Get the transfered data
var data = Encoding.ASCII.GetString(record.GetPayload());
System.Diagnostics.Debug.WriteLine("RECORD");
}
}
}
}
}
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
你能帮我吗?
答案 0 :(得分:0)
您正在为ActionNdefDiscovered
意向注册前台调度。但是,此意图过滤器还需要特定的数据类型(在标签上存在并为意图注册)。如果您要这样做,则需要将该数据类型(MIME类型或URI)添加到意图过滤器(变量tagDetected
)中。
相反,如果您只想听所有标记,则想改用ActionTagDiscovered
意图。实际上,您可以简单地跳过对EnableForegroundDispatch
的调用的所有内部过滤器:
_nfcAdapter.EnableForegroundDispatch(this, pendingIntent, null, null);