Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 捕捉连续按压NFC卡至NFC读卡器(“LongPress”)_Android_Events_Android Intent_Tags_Nfc - Fatal编程技术网

Android 捕捉连续按压NFC卡至NFC读卡器(“LongPress”)

Android 捕捉连续按压NFC卡至NFC读卡器(“LongPress”),android,events,android-intent,tags,nfc,Android,Events,Android Intent,Tags,Nfc,这是在我的应用程序中检测NFC标记的通常方式: protected void onNewIntent(Intent intent) { if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) { Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); ... } } 现在我还需要听听NFC标签是

这是在我的应用程序中检测NFC标记的通常方式:

protected void onNewIntent(Intent intent) {      
    if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
        Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        ...
    }
}
现在我还需要听听NFC标签是否长时间(大约3秒)靠近阅读器。在这种情况下,我想做一些其他的事情(类似于在视图上区分普通压力机和长压力机)。这可能吗?

方法

断开连接()


告诉您与标记的连接是否仍处于活动状态。如果定期检查连接,可以检测到长连接

NFC背后的概念是在标签和NFC设备(或两个NFC设备)之间快速交换少量数据,而不是检测交互的持续时间。因此,没有专门的事件可以让您区分短交互和稍长交互

如图所示,您可以连接到标签,并检查标签在一定时间(例如3秒)后是否仍然连接。但是,您只能从通知应用程序标记的时间点(即调用
onNewIntent()
后)开始测量时间。在用户实际扫描标签后,你无法测量Android通知你的应用程序所需的时间

请注意,
isConnected()
本身并不适用于所有设备/标签组合。测试标记是否仍然存在的最可靠方法是向标记发送有效命令,并检查是否收到响应:

new AsyncTask<Tag, Void, Boolean>() {
    protected Boolean doInBackground(Tag... tags) {
        try {
            Thread.sleep(3000);

            // test if tag is still connected
            Ndef ndef = Ndef.get(tags[0]);
            if (ndef != null) {
                try {
                    ndef.connect();
                    ndef.getNdefMessage();
                } finally {
                    ndef.close();
                }
                return Boolean.TRUE;
            }
        } catch (Exception e) {
        }

        return Boolean.FALSE;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            // "long press" event
        }
    }
}.execute(tag);

是Ndef isConnected()方法吗?但是我的卡除了nfc ID之外没有任何内容,因此Ndef总是空的。您可以尝试:
nfcTag.connect();布尔连接=nfcTag.isConnected();nfcTag.close()
// test if tag is still connected
NfcA nfca = NfcA.get(tags[0]);
if (nfca != null) {
    try {
        nfca.connect();
        byte[] response = nfca.transceive(new byte[] { (byte)0x30, (byte)0x00 });
        if ((response != null) && (response.length > 0))
            return Boolean.TRUE;
        }
    } finally {
        ndef.close();
    }
}