【发布时间】:2020-01-24 09:25:55
【问题描述】:
我有一个应用程序必须能够连续读取主屏幕上的 NFC 标签。 有时它可以从 2-3 厘米的距离很好地读取,有时 5-10 秒没有任何反应,即使我多次将卡触摸到内置读卡器。
我认为 NFC 读取器可能会在一段时间后进入睡眠模式,或者我不知道是什么原因造成的。
有什么方法可以确保连续阅读(至少在设备插入时)?
在onCreate():
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
(new Intent(this, this.getClass())).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
在onResume():
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if ( nfcAdapter != null ) {
if ( !nfcAdapter.isEnabled() ) {
this.showNFCSettings();
}
nfcAdapter.enableForegroundDispatch(this, this.pendingIntent, null, null);
}
【问题讨论】:
-
您正在使用 2 种读取 NFC 卡的模式中的哪一种?
enableForegroundDispatch或enableReaderMode -
我编辑了这个问题。 :) 我正在使用
enableForegroundDispatch