Android NFC扫描方法 [英] Android NFC scan on method

查看:706
本文介绍了Android NFC扫描方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个还将使用NFC读取特定标签的应用程序.我有一个父活动类,我想在其中放置以下函数:

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(){...}

我能够从标记中实际获取tagID的问题是它仅发生在onNewIntent上,因此实际上首先它将完成scanTagId()函数,然后实际调用onNewIntent.因此,问题是如何编写scanTagId()来实际等待从NFC标签读取标签ID并返回具有此标签ID的字符串.

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 :)

推荐答案

通常,您已经说过,NFC是通过onNewIntent处理的.它的工作方式如下:每当电话检测到新的NFC标签(与您的过滤器匹配)时,TAG就会被连接,您的应用会通过onNewIntent获取对TAG的引用.因此,您不需要额外的功能scanTagId(),因为设备一直在扫描TAG.

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);
    }

如果您想通过onNewIntent(重新)启用有关新TAG的通知,则可以使用以下代码:

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);
    }

如果您仍然必须具有如上所述的功能,则可以将TAG-Id存储在实例变量中,并实现以下功能:

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;
}

请注意,此功能不会等待TAG的连接,而只会返回过去找到的最后一个TAG的ID.

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.

另一种方法是将onNewIntentscanTagId功能与wait()notifyAll()同步.我假设您想从主线程调用函数.与同步结合使用,这是一件非常危险的事情,因此,我强烈建议不要这样做.它将使您的主线程进入睡眠状态,这将导致android通知用户(应用未响应"),甚至退出您的应用.至少您的应用不会响应UI事件. 如果使用AsyncTask,则可以避免这些影响.但我真的建议您重新考虑您的设计,并为异步事件做准备.

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.

您首先要实现什么?

如果您想在特定时间过去后退出活动而不找到TAG,可以使用Timer:

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();

然后在您的onCreate方法中激活计时器:

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);

此代码未经测试.

这篇关于Android NFC扫描方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆