首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android条形码扫描仪和NFC

Android条形码扫描仪和NFC
EN

Stack Overflow用户
提问于 2017-02-01 01:14:42
回答 1查看 676关注 0票数 0

场景:我有一个用于扫描条形码和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后条形码无法工作。

请帮帮忙..。

下面是我的代码

代码语言:javascript
运行
复制
// 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);
    }
EN

回答 1

Stack Overflow用户

发布于 2019-10-08 12:00:27

是。如果您使用自定义条形码扫描活动,例如google mobile vision API,您可以在一个活动中扫描NFC和条形码。

https://developers.google.com/vision/android/barcodes-overview

https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/BarcodeCaptureActivity.java

在本练习中,您可以添加模块来扫描NFC卡。因此,当扫描NFC时,您可以停下来扫描条形码,反之亦然。在您的项目中,将有两个函数。一个是扫描近场通信时的onNewIntent,另一个是扫描条形码时的onBarcodeDetected。

希望能对你有所帮助。谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41963561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档