场景:我有一个用于扫描条形码和NFC的android应用程序。
扫描条形码的应用程序来自Famoco http://www.famoco.com/new-product-launch-fx200-fx300/的扫描仪,但不是NFC阅读器。
因此,我所做的只是在内置条形码阅读器应用程序中实现了一个旧的NFC项目。一切正常,唯一的问题是,NFC需要大约2-3秒来输出结果,因为当我点击读取NFC标签时,应用程序仍然运行条形码阅读器。
所以我想知道是否有一种方法可以在我调用NFC时不调用Barcode Intent,反之亦然。
据我所知,首先调用onPause(),然后调用onResume(),然后调用newIntent(),但无论我尝试什么,似乎都不起作用。我甚至尝试在intent上添加一个额外的字符串,以检索最重要的意图,但这也不起作用。
我试着在NFC后面添加一个标志,使其不再继续,但这会导致下次使用NFC后条形码无法工作。
请帮帮忙..。
下面是我的代码
// Called with the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyApplication.getInstance().acquireWakeLock(this);
mainScreen();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// sound
tg = new ToneGenerator(AudioManager.STREAM_MUSIC, ToneGenerator.MAX_VOLUME);
chBeep.setChecked(beepMode);
}
// check if nfc is detected
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
((TextView) findViewById(R.id.textDecode)).setText(
ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)));
//THIS IS NOT WORKING AND CAUSE THE BARCODE TO STOP AFTER I RUN NFC
/*isNFC = true*/
}
}
//Function to decode the NFC to String
private String ByteArrayToHexString(byte[] inarray) {
int i, j, in;
String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
String out = "";
for (j = inarray.length - 1; j >= 0; --j) {
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
@Override
protected void onPause() {
super.onPause();
// disabling foreground dispatch:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
if (bcr != null) {
setIdle();
bcr.release();
bcr = null;
}
}
// Called when the activity is about to start interacting with the user.
@Override
protected void onResume() {
super.onResume();
// creating pending intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// creating intent receiver for NFC events:
IntentFilter filter = new IntentFilter();
filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
// enabling foreground dispatch for getting intent from NFC event:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[] { filter }, this.techList);
//THIS IS NOT WORKING AND CAUSE THE BARCODE TO STOP AFTER I RUN NFC
/*if(isNFC)
return;*/
// Bar code reader
state = STATE_IDLE;
try {
dspStat(getResources().getString(R.string.app_name) + " v"
+ this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName);
if (android.os.Build.VERSION.SDK_INT >= 18)
bcr = BarCodeReader.open(getApplicationContext()); // Android
else
bcr = BarCodeReader.open(); // Android 2.3
if (bcr == null) {
dspErr("open failed");
return;
}
bcr.setDecodeCallback(this);
} catch (Exception e) {
dspErr("open excp:" + e);
}
bcr.setParameter(765, 0);
}
发布于 2019-10-08 12:00:27
是。如果您使用自定义条形码扫描活动,例如google mobile vision API,您可以在一个活动中扫描NFC和条形码。
https://developers.google.com/vision/android/barcodes-overview
在本练习中,您可以添加模块来扫描NFC卡。因此,当扫描NFC时,您可以停下来扫描条形码,反之亦然。在您的项目中,将有两个函数。一个是扫描近场通信时的onNewIntent,另一个是扫描条形码时的onBarcodeDetected。
希望能对你有所帮助。谢谢。
https://stackoverflow.com/questions/41963561
复制相似问题