在应用程序启动或恢复时读取 Nfc 标签
Read a Nfc Tag when app is started or resumed
在我的 Xamarin Forms android 应用程序中,我收到并读取了一个 nfc 标签。当应用程序打开并获得焦点时,以下代码可以正常工作。
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,
NfcAdapter.IOnNdefPushCompleteCallback, NfcAdapter.ICreateNdefMessageCallback
{
private NfcAdapter _nfcAdapter;
const string _appModePrefix = "@AppMode";
const string _package = "com.companyname.nfc";
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
_nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
protected override void OnResume()
{
base.OnResume();
if (_nfcAdapter != null && _nfcAdapter.IsEnabled && _nfcAdapter.IsNdefPushEnabled)
{
var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
_nfcAdapter.EnableForegroundDispatch
(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionNdefDiscovered) },
new String[][] {
new string[] {
NFCTechs.Ndef
},
new string[] {
NFCTechs.MifareClassic,
},
}
);
_nfcAdapter.SetOnNdefPushCompleteCallback(this, this);
_nfcAdapter.SetNdefPushMessageCallback(this, this);
}
}
public void OnNdefPushComplete(NfcEvent e)
{
}
public NdefMessage CreateNdefMessage(NfcEvent e)
{
byte[] mimeBytes = System.Text.Encoding.ASCII.GetBytes(string.Format("application/{0}",_package));
byte[] id = new byte[] { 1, 3, 3, 7 };
string appMode = "1";
string appId = "826F3361-2E93-4378-A6B9-33D2B6087246";
byte[] payload = System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}|{2}", _appModePrefix, appMode, appId));
return new NdefMessage(new NdefRecord[] {
NdefRecord.CreateApplicationRecord(_package),
new NdefRecord(NdefRecord.TnfMimeMedia, mimeBytes, id, payload),
});
}
protected override void OnPause()
{
base.OnPause();
if (_nfcAdapter != null && _nfcAdapter.IsEnabled && _nfcAdapter.IsNdefPushEnabled)
_nfcAdapter.DisableForegroundDispatch(this);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
if (intent.Action == NfcAdapter.ActionTechDiscovered || intent.Action == NfcAdapter.ActionTagDiscovered)
{
var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
if (rawMessages == null)
return;
//...
}
}
}
当应用程序未 运行(或在后台运行)时,应用程序将按预期打开。我也收到了 onNewIntent 事件,但在这种情况下
intent.Action == "android.nfc.action.MAIN"
和
intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
returns 空。此时是否有可能获得原始意图(通过 Tech 或 Tag 发现的操作)?
我确定这是否是适合您的完美解决方案。
我的工作:
1.) 在 TECH_DISCOVERED
上使用 Intent 过滤器定义 Activity
<activity
android:name=".activities.general.NFCActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
2.) 定义技术资源
<resources>
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcBarcode</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
</resources>
3.) 使用标签信息
if (getIntent() != null) {
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey(NfcAdapter.EXTRA_ID)) {
byte[] id_array = getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID);
}
}
}
希望对您有所帮助:)
在我的 Xamarin Forms android 应用程序中,我收到并读取了一个 nfc 标签。当应用程序打开并获得焦点时,以下代码可以正常工作。
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,
NfcAdapter.IOnNdefPushCompleteCallback, NfcAdapter.ICreateNdefMessageCallback
{
private NfcAdapter _nfcAdapter;
const string _appModePrefix = "@AppMode";
const string _package = "com.companyname.nfc";
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
_nfcAdapter = NfcAdapter.GetDefaultAdapter(this);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
protected override void OnResume()
{
base.OnResume();
if (_nfcAdapter != null && _nfcAdapter.IsEnabled && _nfcAdapter.IsNdefPushEnabled)
{
var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
_nfcAdapter.EnableForegroundDispatch
(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionNdefDiscovered) },
new String[][] {
new string[] {
NFCTechs.Ndef
},
new string[] {
NFCTechs.MifareClassic,
},
}
);
_nfcAdapter.SetOnNdefPushCompleteCallback(this, this);
_nfcAdapter.SetNdefPushMessageCallback(this, this);
}
}
public void OnNdefPushComplete(NfcEvent e)
{
}
public NdefMessage CreateNdefMessage(NfcEvent e)
{
byte[] mimeBytes = System.Text.Encoding.ASCII.GetBytes(string.Format("application/{0}",_package));
byte[] id = new byte[] { 1, 3, 3, 7 };
string appMode = "1";
string appId = "826F3361-2E93-4378-A6B9-33D2B6087246";
byte[] payload = System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}|{2}", _appModePrefix, appMode, appId));
return new NdefMessage(new NdefRecord[] {
NdefRecord.CreateApplicationRecord(_package),
new NdefRecord(NdefRecord.TnfMimeMedia, mimeBytes, id, payload),
});
}
protected override void OnPause()
{
base.OnPause();
if (_nfcAdapter != null && _nfcAdapter.IsEnabled && _nfcAdapter.IsNdefPushEnabled)
_nfcAdapter.DisableForegroundDispatch(this);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
if (intent.Action == NfcAdapter.ActionTechDiscovered || intent.Action == NfcAdapter.ActionTagDiscovered)
{
var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
if (rawMessages == null)
return;
//...
}
}
}
当应用程序未 运行(或在后台运行)时,应用程序将按预期打开。我也收到了 onNewIntent 事件,但在这种情况下
intent.Action == "android.nfc.action.MAIN"
和
intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
returns 空。此时是否有可能获得原始意图(通过 Tech 或 Tag 发现的操作)?
我确定这是否是适合您的完美解决方案。
我的工作:
1.) 在 TECH_DISCOVERED
上使用 Intent 过滤器定义 Activity <activity
android:name=".activities.general.NFCActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
2.) 定义技术资源
<resources>
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcBarcode</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
</resources>
3.) 使用标签信息
if (getIntent() != null) {
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey(NfcAdapter.EXTRA_ID)) {
byte[] id_array = getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID);
}
}
}
希望对您有所帮助:)