私はC#でXamarinでコーディングしており、NFCによってMIFARE Classic 1Kカードをスキャンしようとしています。NFC Action_TechがフォアグラウンドでディスパッチするとMifare 1kカードがキャッチされません
m1card_testのインテントフィルタが正常に動作しています。しかし、私はどのアクティビティを開始したいかを選択したくありません。だから私はフォアグラウンドディスパッチを使用しようとしています。ここで
は私のコード(C#の)の一部です:
NfcManager manager = (NfcManager)GetSystemService(NfcService);
manager.DefaultAdapter.DisableForegroundDispatch(this);
OnPause
OnCreateの
Intent Myintent = new Intent(this, GetType()); Myintent.AddFlags(ActivityFlags.SingleTop); mPendingIntent = PendingIntent.GetActivity(this, 0, Myintent, 0); ndefDetected = new IntentFilter(NfcAdapter.ActionTechDiscovered); ndefDetected.AddDataType("*/*"); intentF = new IntentFilter[] { ndefDetected }; techLists = new string[][] {new string[] { typeof(Android.Nfc.Tech.NfcA).FullName, typeof(Android.Nfc.Tech.MifareClassic).FullName} };
OnResume
NfcManager manager = (NfcManager)GetSystemService(NfcService); manager.DefaultAdapter.EnableForegroundDispatch(this,mPendingIntent,intentF,techLists);
残念ながら、フォアグラウンドディスパッチが動作しない(すなわち、タグを拾いません)。
私は
manager.DefaultAdapter.EnableForegroundDispatch(this,mPendingIntent,null,null);
フォアグラウンド発送作業の罰金にEnableForegroundDispatch()
への呼び出しを変更した場合。しかし、MIFARE Classicだけでなく、すべてのタグを取得し、Action_Tech_Discoveredの代わりにAction_Tag_Discoveredを取得します。
フォアグラウンドディスパッチシステムでAction_Tech_Discoveredを使用するにはどうすればよいですか?
私は何かを見逃しましたか?
tech_list.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
</resources>
のAndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="m1card_test.m1card_test" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="16" />
<application android:label="m1card_test"></application>
<uses-permission android:name="android.permission.NFC" />
</manifest>
私のC#コード:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Nfc;
namespace m1card_test
{
[Activity(Label = "m1_read", Icon = "@drawable/icon", LaunchMode = Android.Content.PM.LaunchMode.SingleTask)]
[IntentFilter(
new[] {NfcAdapter.ActionTechDiscovered},
Categories = new[] {Intent.CategoryDefault,})]
[MetaData("android.nfc.action.TECH_DISCOVERED", Resource = "@xml/tech_list")]
public class m1_read : Activity
{
TextView mTV;
PendingIntent mPendingIntent;
IntentFilter ndefDetected;
IntentFilter[] intentF;
String[][] techLists;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.m1_read);
Intent Myintent = new Intent(this, GetType());
Myintent.AddFlags(ActivityFlags.SingleTop);
mPendingIntent = PendingIntent.GetActivity(this, 0, Myintent, 0);
ndefDetected = new IntentFilter(NfcAdapter.ActionTechDiscovered);
ndefDetected.AddDataType("*/*");
intentF = new IntentFilter[] { ndefDetected };
techLists = new string[][] {new string[] {
typeof(Android.Nfc.Tech.NfcA).FullName,
typeof(Android.Nfc.Tech.MifareClassic).FullName}
};
Button button = FindViewById<Button>(Resource.Id.Back_Button);
mTV = FindViewById<TextView>(Resource.Id.textview);
button.Click += delegate
{
Intent main_intent = new Intent(this, typeof(MainActivity));
this.StartActivity(main_intent);
Finish();
};
}
protected override void OnPause()
{
base.OnPause();
NfcManager manager = (NfcManager)GetSystemService(NfcService);
manager.DefaultAdapter.DisableForegroundDispatch(this);
}
protected override void OnResume()
{
base.OnResume();
NfcManager manager = (NfcManager)GetSystemService(NfcService);
manager.DefaultAdapter.EnableForegroundDispatch(this, mPendingIntent,intentF,techLists);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
mTV.Text = "OnNewIntent";
}
}
}
編集を最初にお願いします。私はデータ型を削除し、ハードコードを使用した後、それは動作します!私はこれに新しいので、あなたの容認と再助けに非常に感謝しています。 – wuken