我尝试创建一个函数,在检测到 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/