通过NFC应用程序启动和启动时发送NDEF消息回 [英] Launch app via NFC and send a NDEF message back when started

查看:409
本文介绍了通过NFC应用程序启动和启动时发送NDEF消息回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序与它使用NFC其他设备进行交互。这种相互作用包括2个步骤基本上是:

I'm developing an Android app which interacts with other devices using NFC. This interaction consists of 2 steps basically:


  1. 当设备从其它设备接收到特定URI通过NFC,应用程序启动。

  2. 当应用程序启动时,它会发送一个NDEF消息返回到其他设备上。

有关的第一步,我添加了以下行的的Andr​​oidManifest.xml 的文件。这样一来, MainActivity 当设备接收类型的URI 将推出myprotocol:东西

For the first step, I have added the following lines to the AndroidManifest.xml file. That way, the MainActivity will be launched when the device receives a URI of type myprotocol:something:

<intent-filter>
     <action android:name="android.nfc.action.NDEF_DISCOVERED" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:scheme="myprotocol" />
 </intent-filter>

第二步,我的 MainActivity CreateNdefMessageCallback OnNdefPushCompleteCallback 。在code看起来像下面这样:

For the second step, my MainActivity class implements CreateNdefMessageCallback and OnNdefPushCompleteCallback. The code looks like following:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    adapter.setNdefPushMessageCallback(this, this);
    adapter.setOnNdefPushCompleteCallback(this, this);
}

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    NdefRecord uriRecord = NdefRecord.createUri("protocol:something");
    NdefMessage message = new NdefMessage(new NdefRecord[] { uriRecord });
    return message;
}

@Override
public void onNdefPushComplete(NfcEvent event) {

}

所以,现在的问题

这两部分做工精细独立,而不是当我添加同时向应用程序。

These two parts work fine independently, but not when I add both to the app.

也就是说,如果我想补充的第一部分,接收NDEF消息,当应用程序被正确启动。另外,如果我只加了第二部分,如果我点击我的设备对其他设备在运行的应用程序,我看到的轻触即可传输的接口,并且发送NDEF消息。

That is, if I add the first part, the app is correctly launched when receiving the NDEF message. Also, if I only add the second part, if I tap my device to other device while running the app, I see Touch to beam interface, and the NDEF message is sent.

问题是,如果我同时添加,攻到其它设备时,应用程序启动,但的轻触即可传输的界面从未显示。如果我分离设备和再次点击时, MainActivity 是重新启动,但我从来没有看到发送消息的选项。

The problem is, that if I add both, when tapping to the other device, the app is launched, but the Touch to beam interface is never shown. If I separate the devices and tap again, the MainActivity is relaunched, but I never get to see the option to send the message.

我怎么能达到预期的情况?

How could I achieve the desired scenario?

推荐答案

这两款Android设备上使用光束的一键式的做法是不可能的(请注意,与其他设备,特别是如果一个是Android的,一个是专用的NFC读写器或者你可以控制在较低水平,或模拟一个NFC标签的设备的NFC功能的设备)。

A one-tap approach is not possible using Beam on two Android devices (note that with other devices, particularly if one is Android and one is a dedicated NFC reader or a device where you can control the NFC functionality on a low level or a device that emulates an NFC tag).

不过,两抽头的做法是,只有很少的修改现有方案两款Android设备之间的可能。你只需要添加一个前景派遣截取传入NDEF消息,因此prevents重新启动您的活动安卓:

However, a two-tap approach is possible between two Android devices with only little modifications to your existing scenario. You simply need to add a foreground dispatch that intercepts your incoming NDEF message and consequently prevents Android from restarting your activity:

@Override
public void onResume() {
    super.onResume();
    NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
    PendingIntent pi = PendingIntent.getActivity(
            this,
            0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
            0);
    adapter.enableForegroundDispatch(this, pi, null, null);
}


修改

对于双抽头场景的更一般的方法是从设备A发送的NDEF消息到设备B上的第一抽头。这NDEF消息发送NDEF消息,设备A停止发送该消息后,立即开始在设备B上的应用程序。当设备B的应用程序被激活时,它registeres梁自身的NDEF消息。然后,在第二次敲击,光束用户界面将显示在设备B所,然后点击发送屏幕将发送响应NDEF消息装置的。

A more general approach for the two-tap scenario would be to send the NDEF message from device A to device B on the first tap. This NDEF message starts the app on device B. Immediately after sending the NDEF message, device A stop sending the message. When the app on device B is active, it registeres its own NDEF message for Beam. Then, in the second tap, the Beam UI will be shown on device B and clicking the Beam screen will send the response NDEF message to device A.

请注意,设备A必须停止发送其初始NDEF消息。否则,设备B的应用程序将获得一个新的NDEF消息,因此,不会打开梁UI。

Note that device A must stop sending its initial NDEF message. Otherwise, the app on device B will receive a new NDEF message and, consequently, won't open the Beam UI.

这篇关于通过NFC应用程序启动和启动时发送NDEF消息回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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