简体   繁体   English

Cordova/Phonegap NFC 插件 NDEF_PUSH_DISABLED [已解决]

[英]Cordova/Phonegap NFC Plugin NDEF_PUSH_DISABLED [Solved]

I am trying to build a basic Cordova NFC App using the plugin phonegap-nfc that sends one message using NDEF Peer-to-Peer Messaging from one device to another.我正在尝试使用插件phonegap-nfc构建一个基本的 Cordova NFC 应用程序,该插件使用 NDEF Peer-to-Peer Messaging 从一台设备发送一条消息到另一台设备。 Therefore I have two Android (8+) devices.因此我有两个 Android (8+) 设备。 My problem is similar to this one here: https://forum.ionicframework.com/t/ndef-push-disabled-ionic-3/142617 but the given solutions doesn't fit the problem.我的问题与此处的问题类似: https://forum.ionicframework.com/t/ndef-push-disabled-ionic-3/142617但给定的解决方案不适合该问题。

Whenever I try to send a NDEF message using nfc.share([ndef.textRecord("Hello")) I receive the error NDEF_PUSH_DISABLED .每当我尝试使用nfc.share([ndef.textRecord("Hello"))发送 NDEF 消息时,我都会收到错误NDEF_PUSH_DISABLED I tried a lot of things and finally I was able to find a solution - see the answer below我尝试了很多东西,最后我找到了解决方案 - 请参阅下面的答案

There are not many posts around having this problem, so I want to provide a detailed description here how to solve the problem.有这个问题的帖子不多,所以我想在这里提供一个详细的描述如何解决这个问题。

In order to find the base of the problem I have had a look in the sourcecode of the phonegap-nfc plugin itself , which contains:为了找到问题的根源,我查看了phonegap-nfc 插件本身的源代码,其中包含:

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
if (!nfcAdapter.isNdefPushEnabled()) {
   callbackContext.error(STATUS_NDEF_PUSH_DISABLED);
}

So the problem is not the code itself, it's the NFC adapter from the Android device that returns NDEF message push is disabled.所以问题不在于代码本身,而是来自 Android 设备的 NFC 适配器返回 NDEF 消息推送被禁用。 So I watched my NFC settings on the device and enabled Android Beam, which is using especially NDEF peer-to-peer sharing.所以我在设备上查看了我的 NFC 设置并启用了 Android Beam,它特别使用了 NDEF 点对点共享。 Normally you disable this feature as it always pops up and tries to share your current app information per default connecting two NFC enabled devices, what is really annoying...通常,您禁用此功能,因为它总是弹出并尝试默认连接两个启用 NFC 的设备共享您当前的应用程序信息,这真的很烦人......

But in this context, you have to enable and especially use Android Beam.但在这种情况下,您必须启用并特别使用 Android Beam。 The solution I have found is that the message you want to send using nfc.share() is only sent if you execute the application code and afterwards tap on the Android Beam popup.我发现的解决方案是,仅当您执行应用程序代码然后点击 Android Beam 弹出窗口时才会发送您要使用nfc.share()发送的消息。 After this action your NDEF message will be send to the other device using Android Beam and your given message.在此操作之后,您的 NDEF 消息将使用 Android Beam 和您给定的消息发送到其他设备。 To send and receive the message I was using the following code:要发送和接收消息,我使用以下代码:

nfc.addNdefListener(
    function (nfcEvent) {
        var tag = nfcEvent.tag,
            ndefMessage = tag.ndefMessage;

        // dump the raw json of the message, note: real code will need to decode the payload from each record
        alert("NDS1 " + JSON.stringify(ndefMessage));

        // assuming the first record in the message has a payload that can be converted to a string.
        alert("NDS2 " + nfc.bytesToString(ndefMessage[0].payload).substring(3));

        nfc.share([ndef.textRecord("Hello")], () => {alert("Success")}, (err) => alert(JSON.stringify(err)));
    },
    function () { // success callback when listener was enabled
        alert("Waiting for NDEF tag");
    },
    function (error) { // error callback
        alert("Error adding NDEF listener " + JSON.stringify(error));
    }
)

You don't have to wait until the NdefListener is available you can use nfc.share() wherever you want, you will just receive the callbacks when you clicked on the Android Beam tap.您不必等到 NdefListener 可用,您可以在任何地方使用nfc.share() ,当您单击 Android Beam tap 时,您只会收到回调。 The last message you have provided using nfc.share() will be send using Android Beam.您使用nfc.share()提供的最后一条消息将使用 Android Beam 发送。

Cordova Android Beam NDEF 消息接收

I hope this error description helps some people getting NFC - NDEF peer-to-peer messaging - using Cordova, Phonegap or Ionic enabled.我希望这个错误描述可以帮助一些人使用 Cordova、Phonegap 或 Ionic 获得 NFC - NDEF 点对点消息传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM