简体   繁体   中英

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. Therefore I have two Android (8+) devices. 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.

Whenever I try to send a NDEF message using nfc.share([ndef.textRecord("Hello")) I receive the error 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:

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. So I watched my NFC settings on the device and enabled Android Beam, which is using especially NDEF peer-to-peer sharing. 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...

But in this context, you have to enable and especially use 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. After this action your NDEF message will be send to the other device using Android Beam and your given message. 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. The last message you have provided using nfc.share() will be send using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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