我正在使用此ndeftools库与NFC硬件进行通信。除了安装应用程序之后的一个问题,NFC似乎不想返回任何内容,它工作正常。它使NFC听起来像是在扫描NFC标签,但没有其他任何事情发生。如果我退出我的应用并扫描标签,则会像往常一样激活原生NFC应用。重启设备后,NFC功能将从此后恢复到我的应用程序。我想知道是否有人遇到过这个问题。任何帮助将不胜感激。
编辑:我包含了用于与NFC库接口的代码。对于读取标签,我只使用enableForegroundMode(),它在大多数情况下工作,除了安装后的这一个场景。还有更多我应该做的吗?
public class NfcHelper {
private final String TAG = NfcHelper.class.getSimpleName();
protected NfcAdapter m_NFCAdapter;
protected PendingIntent m_NFCPendingIntent;
private Context m_Context = null;
public NfcHelper(Context p_Context) {
m_Context = p_Context;
m_NFCAdapter = NfcAdapter.getDefaultAdapter(m_Context);
m_NFCPendingIntent = PendingIntent.getActivity(m_Context, 0, new Intent(m_Context, m_Context.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
public void disableForegroundMode() {
Log.i(TAG, "disableForegroundMode");
m_NFCAdapter.disableForegroundDispatch((Activity) m_Context);
}
public void enableForegroundMode() {
Log.i(TAG, "enableForegroundMode");
// foreground mode gives the current active application priority for reading scanned tags
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); // filter for tags
IntentFilter[] writeTagFilters = new IntentFilter[] {tagDetected};
m_NFCAdapter.enableForegroundDispatch((Activity) m_Context, m_NFCPendingIntent, writeTagFilters, null);
}
public NfcAdapter getNFCAdapter() {
return m_NFCAdapter;
}
public SimpleEntry<String, String> getNFCData(Intent p_intent) {
Log.i(TAG, "getNFCData");
byte[] arrTagSerial = null;
String sTagSerial = "";
String sTagContent = "";
try {
// check for NFC related actions
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(p_intent.getAction())) {
arrTagSerial = p_intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
sTagSerial = new BigInteger(arrTagSerial).toString(16);
Parcelable[] messages = p_intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (messages != null) {
Log.d(TAG, "Found " + messages.length + " NDEF messages");
//vibrate();
// parse to records
for (int i = 0; i < messages.length; i++) {
try {
List<Record> records = new Message((NdefMessage)messages[i]);
Log.d(TAG, "Found " + records.size() + " records in message " + i);
for(int k = 0; k < records.size(); k++) {
Log.d(TAG, " Record #" + k + " is of class " + records.get(k).getClass().getSimpleName());
Record record = records.get(k);
if (record instanceof TextRecord) {
TextRecord txtrecord = (TextRecord) records.get(k);
sTagContent = txtrecord.getText();
}
else if(record instanceof AndroidApplicationRecord) {
AndroidApplicationRecord aar = (AndroidApplicationRecord)record;
Log.d(TAG, "Package is " + aar.getPackageName());
}
}
}
catch (Exception e) {
Log.e(TAG, "Problem parsing message", e);
}
}
}
} else {
// ignore
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return new SimpleEntry<String, String>(sTagSerial, sTagContent);
}
public boolean writeText(String text, Intent intent) {
Message message = new Message();
org.ndeftools.wellknown.TextRecord textRecord = new org.ndeftools.wellknown.TextRecord();
try {
textRecord.setText(text);
textRecord.setEncoding(Charset.forName("UTF-8"));
textRecord.setLocale(Locale.ENGLISH);
message.add(textRecord);
return writeMessage(message, intent);
}
catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
public boolean writeMessage(Message message, Intent intent) {
return writeMessage(message.getNdefMessage(), intent);
}
public boolean writeMessage(NdefMessage rawMessage, Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
Log.d(TAG, "Write unformatted tag");
try {
format.connect();
format.format(rawMessage);
//writeNdefSuccess();
return true;
}
catch (Exception e) {
//writeNdefFailed(e);
}
finally {
try {
format.close();
}
catch (IOException e) {
// ignore
}
}
Log.d(TAG, "Cannot write unformatted tag");
}
else {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
Log.d(TAG, "Write formatted tag");
ndef.connect();
if (!ndef.isWritable()) {
Log.d(TAG, "Tag is not writeable");
//writeNdefNotWritable();
return false;
}
if (ndef.getMaxSize() < rawMessage.toByteArray().length) {
Log.d(TAG,
"Tag size is too small, have "
+ ndef.getMaxSize() + ", need "
+ rawMessage.toByteArray().length);
//writeNdefTooSmall(rawMessage.toByteArray().length, ndef.getMaxSize());
return false;
}
ndef.writeNdefMessage(rawMessage);
//writeNdefSuccess();
return true;
}
catch (Exception e) {
//writeNdefFailed(e);
}
finally {
try {
ndef.close();
}
catch (IOException e) {
// ignore
}
}
}
else {
//writeNdefCannotWriteTech();
}
Log.d(TAG, "Cannot write formatted tag");
}
return false;
}
public int getMaxNdefSize(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
Log.d(TAG, "Format tag with empty message");
try {
if (!format.isConnected()) {
format.connect();
}
format.format(new NdefMessage(new NdefRecord[0]));
}
catch (Exception e) {
Log.d(TAG, "Problem checking tag size", e);
return -1;
}
}
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
try {
if (!ndef.isConnected()) {
ndef.connect();
}
if (!ndef.isWritable()) {
Log.d(TAG, "Capacity of non-writeable tag is zero");
//writeNdefNotWritable();
return 0;
}
int maxSize = ndef.getMaxSize();
ndef.close();
return maxSize;
}
catch (Exception e) {
Log.d(TAG, "Problem checking tag size", e);
}
}
else {
//writeNdefCannotWriteTech();
}
Log.d(TAG, "Cannot get size of tag");
return -1;
}
}
EDIT2:以下是我使用NfcHelper的活动代码:
private NfcHelper mNFC = new NfcHelper(this);
@Override
public void onNewIntent(Intent intent) {
SimpleEntry<String, String> NFCData = null;
// If intent from NFC tag scan
if (intent.getAction() != null &&
intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
NFCData = mNFC.getNFCData(intent);
// Do stuff with NFC data
}
}
@Override
protected void onResume() {
super.onResume();
mNFC.enableForegroundMode();
}
@Override
protected void onPause() {
super.onResume();
mNFC.disableForegroundMode();
}