Android NFC scan on method

和自甴很熟 提交于 2020-01-06 06:58:08

问题


I'm creating an app that will also use NFC to read specific tags. I have a parent activity class where I would like to put function like:

public String scanTagId(){...}

I'm able to get actually tagID from tag the problem is it only occurs onNewIntent so actually first it will finish scanTagId() function and then invoke onNewIntent actually. So the question is how to write scanTagId() to actually wait for reading Tag ID from NFC tag and return string with this tag ID.

Maybe I need to approach this somehow differently or it's not possible - but right now I have a headache from trying to solve this :)


回答1:


Normally NFC is handled via onNewIntentas you already said. It works like this: everytime a new NFC-Tag (that matches your filters) is detected by the phone the TAG gets connected and your app gets a reference to the TAG via the onNewIntent. Therefore you don't need an extra function scanTagId() since the device is already scaning for TAGs all the time.

If you want to disable that your app gets notified you can do it like this:

if (mNfcAdapter != null) {
        mNfcAdapter.disableForegroundDispatch(this);
    }

If you want to (re-)enable the notification about new TAG via onNewIntent you can use this code:

if (mNfcAdapter != null) {
        mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
    }

If you still have to have a function like you mentioned, I'd store the TAG-Id in an instance-variable and implement the function like this:

protected String mLastTagId = "";

protected synchronized void setLastTagId(String id) {
    mLastTagId = id;
}

public synchronized String scanTagId(){
    return mLastTagId;
}

Please notice, that this function will not wait for a TAG to be connected, but will just return the ID of the last TAG that was found in the past.

Update

Another approach would be to synchronize onNewIntent and your scanTagId-function with wait() and notifyAll(). I assume you want to invoke your function from main-thread. In combination with synchronization this is a very dangerous thing to do and therefore I strictly advice against it. It will put your main-thread to sleep which is going to cause android to notify the user ("App is not responding") or even quit your app. At least your app won't be responding to UI-events. You could avoid these effects if you'd use an AsyncTask. But I'd really advice to rethink your design and prepare it for async-events.

What are you trying to achieve in first place?

Update2

If you want to quit your Activity after a specific time has elapsed without a TAG being found, you can use a Timer:

protected String tag = null;
protected Timer timer = new Timer();

And in your onCreate-method activate the timer:

timer.schedule(new TimerTask() {
    public void run() {
        AlertDialog.Builder b = new AlertDialog.Builder(MyActivity.this);
        b.setTitle("Timeout").setMessage("Nothing found");
        b.setPositiveButton("OK", new OnClickListener() {
            MyActivity.finish();
        });
        b.setCancelable(false);
        b.create().show();
    }, 10000);

This code hasn't been tested.



来源:https://stackoverflow.com/questions/20331027/android-nfc-scan-on-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!