2015-11-09 30 views
1

我正在使用phonegap-nfc插件来读取我的科尔多瓦应用程序中的NFC标签。我还在AndroidManifest.xml中包含了uses-permission标签。 我的打字稿码是:科尔多瓦NFC插件工作,但没有引发任何事件

module myapp.nfcRead { 

export class NfcCtrl { 
    private status:string; 

    constructor(private $cordovaNfc:any, private $cordovaNfcUtil:any) { 
     this.status = "nfc"; 
    } 

    public readNFC() { 
     console.log("trying to find nfc"); 
     this.$cordovaNfc.then((nfcInstance:any) => { 
      nfcInstance.addNdefListener((nfcEvent:any) => { 
       this.status = "NFC Detected"; 
      }) 
       .then(
       //Success callback 
       (event:any) => { 
        this.status = "Reading NFC"; 
        console.log("Reading NFC"); 
       }, 
       //Fail callback 
       (err:any) => { 
        this.status = "error"; 
        console.log("error"); 
       }); 
     }); 
     this.$cordovaNfcUtil.then((nfcUtil:any) => { 
      this.status = nfcUtil.bytesToString("some bytes"); 
     }); 
    } 
} 

NfcCtrl.$inject = ['$cordovaNfc', '$cordovaNfcUtil']; 
angular.module('myapp.nfcRead', ['ngCordova.plugins.nfc']).controller('NfcCtrl', NfcCtrl);} 

如果我在浏览器中启动应用程序,我调用方法“readNFC()”的状态保持为“NFC”。如果我在我的android手机上部署应用程序,并且我称之为“readNFC()”功能并且NFC被禁用,则状态为“错误”。激活NFC并再次调用该功能后,它会显示“正在读取NFC”。现在我想要读取NFC标签,但状态不会改变。 (无事件引发)

我下载了一个应用程序从谷歌Play商店,看看标签发现: (APP:NFC工具) NFC Tools output

你对我有什么提示? 谢谢

+0

您是否找到解决方案?非常感谢 – Danilo

回答

0

您的代码使用的是nfc.addNdefListener,它只能找到包含NDEF消息的标签。

screenshot from NFC Tools说你的标签是NDEF Formatable,所以我不认为它包含一个NDEF消息。

您可以更改您的代码以使用nfc.addTagDiscoveredListenernfc.addNdefFormatableListener。有多个听众很好。 Android会为所扫描的标签调用最具体的侦听器。

或者,您可以使用NFCTools将NDEF消息写入标签。然后尝试用您现有的代码阅读它。

相关问题