我想在不使用意图的情况下扫描NFC标签。换句话说,我想强制扫描。我已经读过了:
http://developer.android.com/guide/topics/connectivity/nfc/index.html
https://code.google.com/p/ndef-tools-for-android/
但两者都使用意图。
附注:我的情况是NFC标签是永久贴在设备上的,所以我不能使用intents。
发布于 2016-01-05 23:58:40
使用foreground dispatch
:http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null || !nfcAdapter.isEnabled()) {
Log.e(TAG, "No NFC Adapter found.");
//finish();
}
Intent intent = new Intent(this, getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
// Handles all MIME based dispatches. You should specify only the ones that you need.
ndef.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("failed to add MIME type", e);
}
//intentFiltersArray = new IntentFilter[]{ndef,};
//Use no intent filters to accept all MIME types
intentFiltersArray = new IntentFilter[]{};
// The tech list array can be set to null to accept all types of tag
techListsArray = new String[][]{new String[]{
IsoDep.class.getName(),
NfcA.class.getName(),
NdefFormatable.class.getName()
}};
}
public void onPause() {
nfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
public void onResume() {
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);
super.onResume();
}
public void onNewIntent(Intent intent) {
Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//do something with tagFromIntent
}
发布于 2014-11-22 22:53:36
很简单。当你达到了你的“灵活性”时间...你真的改变了。使用INTENt,读取标签,并将信息保存在您的SD卡、共享首选项、云中的任何位置。并在每次要读取标记时使用此信息。取而代之的是读取在附加标签时创建的文件。下次删除标记并添加另一个标记时,将重新创建您的文件。
不读取标签,读取由附加到设备的标签创建的文件。
发布于 2014-11-23 00:27:29
你不能随心所欲。由于Android NFC子系统的构建方式,在没有意图的情况下读取标签是不可能的。
此外,将标签粘在手机背面也是一个非常糟糕的主意。只要没有检测到标签,NFC就会周期性地检查标签的存在。一旦检测到标签,它将通过空中供电,直到标签不再应答。
如果标签总是在手机的范围内,它将疯狂地耗尽电池寿命。
https://stackoverflow.com/questions/21307898
复制相似问题