Cordova Ionic NFC标签写:标签容量是0字节?

时间:2018-05-31 15:46:55

标签: javascript cordova ionic-framework phonegap-plugins nfc

我目前正在尝试在我的原生cordova应用程序中实现一个简单的读/写功能,每当我尝试擦除或写入标记时,我都会收到以下错误:Tag capacity is 0 bytes, message is 4 bytes.

出于信息目的,我与我的标签是:https://www.amazon.ca/Robojax-chain-13-56Mhz-Arduino-Raspberry/dp/B079Q9RYL3/ref=sr_1_1?ie=UTF8&qid=1527781307&sr=8-1&keywords=nfc+tags+keychain

我订购的存储容量为8Kbit的标签,所以我不明白是什么原因导致写入失败。

我正在使用示例离子NFC项目来编写和阅读标签:https://ionicframework.com/docs/native/nfc/

以下是尝试写入/读取的一小段测试代码

this.nfc.addNdefListener( () => {
                console.log("Successfully attached NDEF listener");
            }, (err: any) => {
                console.log("error attaching ndef listener", err);
            }).subscribe( (event) => {
                console.log("received NDF message", event);

                if (this.isWriting) {
                    this.nfc.erase().then( () => {
                        console.log("Sucessfully erased the tag");

                        const a = this.ndef.textRecord("Hello world", "en", "usertoken");
                        this.nfc.write([a]).then( () => {
                            console.log("We wrote to the tag");
                        }).catch( (err: any) => {
                            console.log("we could not write to the tag", err);
                        })
                    }, (err: any) => {
                        console.log("Problem while attempting to erase tag", err)
                    });
                }
            })

我的界面中有一个按钮,将this.isWriting设置为true,这样我们就不会意外地写。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

不要删除标记,只需将标记写入标记即可。如果标记是可写的,则新消息将覆盖旧消息。

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

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

  constructor(public navCtrl: NavController, private nfc: NFC, private ndef: Ndef) {
    nfc.addNdefListener().subscribe(this.onNdefTagScanned.bind(this));
  }

  onNdefTagScanned(nfcEvent: any) {

    // Create an NDEF text record
    const record = this.ndef.textRecord("Hello world", "en", null);
    // an NDEF message is an array of NDEF records    
    const message = [record];

    // write to the tag
    this.nfc.write(message).then(
      _ => console.log('Wrote message to tag'),
      error => console.log('Write failed', error)
    )
  }

}