cordova - ionic 2 : Use NFC

标签 cordova angular ionic2 nfc

我尝试创建一个函数,在检测到 NFC 卡时进行手机振动。 我使用 Ionic 2 和这个: https://github.com/chariotsolutions/phonegap-nfc

我试过这个方法。 在 .ts 文件中:

readNFCNdefListener() {
    console.log("NFC READ OK");
    NFC.addTagDiscoveredListener(onSuccess => function () {
      this.nfc_status = 'Read NdefListener';
      console.log('Read NdefListener', onSuccess);
      Vibration.vibrate([1000,250,1000]);
    });
  }

在 .html 文件中:

<ion-card>
    <ion-item (click)="readNFCNdefListener()">
      <ion-label>NFC Nef : {{this.nfc_status}}</ion-label>
    </ion-item>
  </ion-card>

这只是没有效果。我实在不明白这个插件怎么用。 有人可以告诉我该怎么做吗?

PS: 我在我的设备上激活 NFC。

我导入了 NFC 和 Ndef。

最佳答案

您必须在 config.xml 中添加一些内容:

  <platform name="android">
    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
    <intent-filter>
      <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
      <data android:mimeType="text/pg"/>
      <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
  </platform>

如果您有 xml 错误,请将其添加到小部件中的文件顶部

xmlns:android="http://schemas.android.com/apk/res/android"

有关 Android 意图过滤器的更多详细信息,请参阅: https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#ndef-disc

在我的 ts 文件中:

import {NFC, Ndef} from 'ionic-native';
..
  addNfcListeners():void {
        NFC.addTagDiscoveredListener((tagEvent:Event) => this.tagListenerSuccess(tagEvent));
        NFC.addNdefListener((tagEvent:Event) => this.tagListenerSuccess(tagEvent));
    }
  tagListenerSuccess(tagEvent:Event) {
       console.log(tagEvent);
   }

然后在您的日志中您将看到它。如果您不知道如何查看日志,一个简单(但不是最好)的解决方案是打开 android studio,您将在 Android 显示器上看到它

就我而言,它是:

03-02 17:31:33.712 27750-16921/com.ionicframework.tbcbyjeff820435 V/NfcPlugin: var e = document.createEvent('Events');
                                                                               e.initEvent('ndef-mime');
                                                                               e.tag = {"id":[59,12,-6,-33],"techTypes":["android.nfc.tech.NfcA","android.nfc.tech.MifareClassic","android.nfc.tech.Ndef"],"type":"com.nxp.ndef.mifareclassic","maxSize":716,"isWritable":true,"ndefMessage":[{"tnf":1,"type":[84],"id":[],"payload":[2,102,114,78,73,67,79,76]}],"canMakeReadOnly":true};

您的标签内容位于

"ndefMessage":[{"tnf":1,"type":[84],"id":[],"payload":[2,102,114,78,73,67,79,76]}]

我仍在努力解码。希望对您有帮助

关于cordova - ionic 2 : Use NFC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42535930/

相关文章:

mysql - 直接从登陆页面单击“忘记密码”时出现错误“找不到网址”

javascript - 使用装饰器中的原型(prototype)/访问基类属性访问派生类中的基类属性

cordova - Ionic 2/3 中的圆角图像/个人资料图片

css - 在 Angular 2/Ionic 2 中给定条件下右对齐列

android - 如何在没有 Google Play 商店的情况下动态更新 Phonegap Build Android 应用程序

Angular 5 Material Snackbar 面板类配置

windows-phone-7 - 将 AngularJS 1.2 $location 服务注入(inject)到在 PhoneGap 3.0 中不起作用的 Controller 中

angular - ionic 3 带存储

android - Windows 8.1 64 位 "ionic build android": Error: Hook failed with error code ENOENT

android - 无法使用Gradle构建Ionic/Cordova Android项目