Xamarin for Android NFC的读取

using Android.App;
using Android.Widget;
using Android.OS;
using Android;
using Android.Content.PM;
using Android.Nfc;
using Android.Content;
using System.Text;
using System.Linq;


[assembly:UsesPermission(Manifest.Permission.Nfc)]
[assembly:UsesFeature(PackageManager.FeatureNfc,Required =true)]


namespace NFCReader
{
    [Activity(Label = "NFCReader", MainLauncher = true,LaunchMode =LaunchMode.SingleTop)]
    public class MainActivity : Activity
    {
        private TextView textView;
        private NfcAdapter nfcAdapter;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);


            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            textView = FindViewById<TextView>(Resource.Id.ReadInfo);


        }


        protected override void OnResume()
        {
            base.OnResume();
            //获取默认NFC控制器
            nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
            if (nfcAdapter==null) {
                textView.Text += string.Format("NFC not support on this devices.\n");
                return;
            }
            //判断设备是否支持NFC
            if (!nfcAdapter.IsEnabled) {
                textView.Text += string.Format("NFC is not enabled.\n");
                return;
            }
            textView.Text += string.Format("Hold the tag against the phone to begin reading...\n");
            //当NFC标签被发现时创建过滤器
            var tagDetected = new IntentFilter(NfcAdapter.ActionTagDiscovered);
            var filters = new[]{tagDetected };
            //当检测到NFC标签时,PendingIntent用来返回这个Activity
            var intent = new Intent(this,GetType());
            var pendingIntent = PendingIntent.GetActivity(this,0,intent,0);
            //使用前台系统发布
            nfcAdapter.EnableForegroundDispatch(this,pendingIntent,filters,null);
        }
        protected override void OnPause()
        {
            base.OnPause();
            //取消前台系统发布
            if (nfcAdapter!=null) {
                nfcAdapter.DisableForegroundDispatch(this);
            }
        }
        protected override void OnNewIntent(Intent intent)
        {
            textView.Text += string.Format("Possible NFC tag detected:'{0}'.\n",intent.Action);
            //获得标签
            var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
            if (tag!=null) {
                textView.Text += string.Format("NFC tag detected:\n");
                textView.Text += string.Format("Tag ID :'{0}'\n",Encoding.UTF8.GetString(tag.GetId()));
                textView.Text += string.Format("Tech List:\n");
                foreach (var tech in tag.GetTechList()) {
                    textView.Text += string.Format("Tech:'{0}'",tech);
                }
            }
            //获取信息
            var ndefMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
            if (ndefMessages!=null) {
                textView.Text += string.Format("NFC NDEF message detected:\n");
                //获得在标签中的每一条信息
                foreach (var message in ndefMessages.Cast<NdefMessage>()) {
                    textView.Text += string.Format("Message:{0}\n",message.GetType().FullName);
                    foreach (var record in message.GetRecords()) {
                        textView.Text += string.Format("Record:{0}\n",record.GetType().FullName);
                        textView.Text += string.Format("ID:{0}\n",Encoding.UTF8.GetString(record.GetId()));
                        textView.Text += string.Format("Type Info:{0}\n",Encoding.UTF8.GetString(record.GetTypeInfo()));
                        var payload = record.GetPayload();
                        textView.Text += string.Format("Payload:{0}\n",Encoding.UTF8.GetString(payload));
                    }
                }
            }
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值