简体   繁体   English

Ionic 3-读取NFC卡了吗?

[英]Ionic 3 - Read NFC Card?

I'm stuck for weeks trying read NFC card from my ionic project. 我被困了几个星期,试图从我的离子项目中读取NFC卡。

Running the app on a real device (Samsung S7 Edge with Android). 在真实设备上运行该应用程序(带有Android的Samsung S7 Edge)。

I'm following this: https://ionicframework.com/docs/native/nfc/ 我正在关注: https : //ionicframework.com/docs/native/nfc/

Then, I installed the plugin on my project: 然后,我在项目上安装了插件:

ionic cordova plugin add phonegap-nfc

npm install @ionic-native/nfc

Only need read the Card Tag Id into the variable tagId (string) to show it. 只需要将 Card Tag ID读入变量tagId(字符串)以显示它即可。

My source: 我的来源:

app.module.ts app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { NFC, Ndef } from '@ionic-native/nfc/ngx';

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    NFC,
    Ndef,
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

home.ts home.ts

import { Component } from '@angular/core';
import { NavController, Platform, AlertController, ToastController } from 'ionic-angular';
import { NFC, Ndef } from '@ionic-native/nfc/ngx';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  tagId: string;

  constructor(public platform: Platform,
              private alertCtrl: AlertController, 
              private toastCtrl: ToastController,
              public navCtrl: NavController, 
              private nfc: NFC,
              private ndef: Ndef) {

    this.platform.ready().then(() => { 
      this.addListenNFC();
    });

  } // del constructor

    addListenNFC() {
      console.log('entra a addListenNFC');

      this.nfc.addNdefListener(() => {
        console.log('successfully attached ndef listener');
      }, (err) => {
        console.log('error attaching ndef listener', err);

        let toast = this.toastCtrl.create({
          message: err,
          duration: 1000,
          position: 'bottom'
        });

        toast.present(); 

      }).subscribe((event) => {
        console.log('received ndef message. the tag contains: ', event.tag);
        console.log('decoded tag id', this.nfc.bytesToHexString(event.tag.id));

        let toast = this.toastCtrl.create({
          message: this.nfc.bytesToHexString(event.tag.id),
          duration: 1000,
          position: 'bottom'
        });

        toast.present(); 
      });

    }
} 

home.html home.html的

<ion-header>
  <ion-navbar>
    <ion-title>
      NFC-Access Card
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <h1>Please Scan Access Card</h1>
  <ion-card>
    <ion-card-content>
      <p>Account Tag ID: {{ tagId  }}</p>
    </ion-card-content>
  </ion-card>
</ion-content>

I'm getting this error in the console: 我在控制台中收到此错误:

console error adding listener 控制台错误添加监听器

Why do I get this error when adding the listener? 为什么在添加侦听器时出现此错误? What is wrong?? 怎么了??

Thanks. 谢谢。

Its a version incompatibility error. 其版本不兼容错误。 Check your ionic.config.json . 检查您的ionic.config.json If your type is ionic angular then you need to downgrade your nfc plugin version to 4.xx Therefore when you are importing the plugin to your app.module.ts and your respective components file(s), it becomes; 如果您的类型是离子型,那么您需要将nfc插件版本降级为4.xx。因此,当您将插件导入到app.module.ts和您各自的组件文件时,它将变为:

import { NFC, NDef } from '@ionic-native/nfc'

instead of: 代替:

import { NFC, NDef } from '@ionic-native/nfc/ngx'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM