【问题标题】:Cordova Ionic NFC tag write : Tag capacity is 0 bytes?Cordova Ionic NFC 标签写入:标签容量为 0 字节?
【发布时间】:2018-11-10 16:43:58
【问题描述】:

我目前正在尝试在我的原生科尔多瓦应用程序中实现一个简单的读/写功能,每次我尝试擦除或写入标签时,我都会收到以下错误: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)
                    });
                }
            })

我的 UI 中有一个按钮将 this.isWriting 设置为 true,这样我们就不会误写。

任何帮助将不胜感激!

【问题讨论】:

  • 目前用的是三星note 8

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


【解决方案1】:

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

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)
    )
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    相关资源
    最近更新 更多