Phonegap NFC在两个设备之间发送和读取数据 [英] Phonegap NFC Send and Read Data Between Two Devices

查看:123
本文介绍了Phonegap NFC在两个设备之间发送和读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用离子角度应用程序中的phonegap-nfc在两个启用NFC的设备之间发送信息(纯文本).

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

到目前为止,我已经能够连接设备,并且Ndef Event Listener捕获了标签,但是标签中的信息始终是相同的,它不会向我发送正在发送的消息.

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.

识别NFC连接时发送标签的功能:

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"]);
      });

    });
  }

我收到标签的事件:

   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}

我假设我的消息可能在 ndefMessage:

I am assuming my message could be inside ndefMessage:

但是当我执行 NFC.bytesToString(nfcEvent.tag.ndefMessage [0] .payload)时,我得到了以下字符串" play.google.com/store/apps/details?id = io.ionic.starter& feature = beam"

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"

我想念什么吗?

提前谢谢!

推荐答案

这可能是因为Android 10中不推荐使用Android Beam(对等NFC共享中使用的功能).(操作系统正在寻找要替换的应用程序此缺失的功能).

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).

请参见 https://source.android.com/setup/start/android-10-release#nfc

展望未来,使用Wifi Direct或蓝牙可能会更好,因此在设备之间发送数据,谷歌将其"Nearby"推向了新的高度.使用API​​ https://developers.google.com/nearby/代替.

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.

这篇关于Phonegap NFC在两个设备之间发送和读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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