Phonegap NFC Send and Read Data Between Two Devices

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 14:59:47

问题


I am trying to send information (just text) between two NFC enabled devices using phonegap-nfc in ionic angular app.

So far I am able to connect the devices and the Ndef Event Listener captures the tag, but the information in the tag is always the same, it does not give me the message I am sending.

My function to send the tag when NFC connection is identified:

 async connectPeerToPeer() {
    var listener = NFC.addNdefListener(onSucess => {
      console.log('Listening: ', onSucess);
    }, onFail => {
      console.log('Failure: ', onFail);
    }).subscribe(event => {
  
      NFC.share([Ndef.textRecord("hello world!")]).then(res => {
        this.service.Popup("The write message was sent!", ["OK"]);
      }).catch(err => {
        this.service.Popup("Error: "+ err, ["OK"]);
      });

    });
  }

My event to receive the tag:

   NFC.addNdefListener((listening) => {
      console.log("Listening");
    }, (fail) => {
      this.service.Popup(fail, ['OK']);
    }).subscribe(ev => {
      this.onNfc(ev);
    });

  onNfc(nfcEvent) {
    console.log(nfcEvent);
  }

With this the tag is transferred to the receiving device but the information is not the message I am sending.

This is what I get when I put a breakpoint on the inspector:

{"id":[0],"techTypes":["android.nfc.tech.Ndef"],"type":"android.ndef.unknown","maxSize":0,"isWritable":false,"ndefMessage":[{"tnf":1,"type":[85],"id":[],"payload":[3,112,108,97,121,46,103,111,111,103,108,101,46,99,111,109,47,115,116,111,114,101,47,97,112,112,115,47,100,101,116,97,105,108,115,63,105,100,61,105,111,46,105,111,110,105,99,46,115,116,97,114,116,101,114,38,102,101,97,116,117,114,101,61,98,101,97,109]},{"tnf":4,"type":[97,110,100,114,111,105,100,46,99,111,109,58,112,107,103],"id":[],"payload":[105,111,46,105,111,110,105,99,46,115,116,97,114,116,101,114]}],"canMakeReadOnly":false}

I am assuming my message could be inside ndefMessage:

But when I do NFC.bytesToString(nfcEvent.tag.ndefMessage[0].payload) I get the following string "play.google.com/store/apps/details?id=io.ionic.starter&feature=beam"

Am I missing something?

Thanks in advance!


回答1:


This is probably because Android Beam (The functionality used in peer to peer NFC sharing) was deprecated in Android 10. (The OS is looking for an App to replace this missing feature).

See https://source.android.com/setup/start/android-10-release#nfc

Going forward it is probably better to use Wifi Direct or Bluetooth so send data between devices, Google were pushing their "Nearby" API https://developers.google.com/nearby/ as a replacement.



来源:https://stackoverflow.com/questions/64599128/phonegap-nfc-send-and-read-data-between-two-devices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!