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));
}
}
}
}
}
}
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));
}
}
}
}
}
}