Closed ferri-17 closed 6 years ago
the app closes automatically
That sounds like a crash. If you adb logcat
can to see a stack trace from the app when it crashes?
Here's the stack trace
06-13 11:49:29.594 7832 7938 E AndroidRuntime: FATAL EXCEPTION: pool-1-thread-2
06-13 11:49:29.594 7832 7938 E AndroidRuntime: Process: com.megster.nfc.issue328, PID: 7832
06-13 11:49:29.594 7832 7938 E AndroidRuntime: java.lang.IllegalStateException: Close other technology first!
06-13 11:49:29.594 7832 7938 E AndroidRuntime: at android.nfc.Tag.setConnectedTechnology(Tag.java:458)
06-13 11:49:29.594 7832 7938 E AndroidRuntime: at android.nfc.tech.BasicTagTechnology.connect(BasicTagTechnology.java:78)
06-13 11:49:29.594 7832 7938 E AndroidRuntime: at android.nfc.tech.Ndef.connect(Unknown Source:0)
06-13 11:49:29.594 7832 7938 E AndroidRuntime: at com.chariotsolutions.nfc.plugin.NfcPlugin.lambda$writeNdefMessage$2$NfcPlugin(NfcPlugin.java:344)
06-13 11:49:29.594 7832 7938 E AndroidRuntime: at com.chariotsolutions.nfc.plugin.NfcPlugin$$Lambda$2.run(Unknown Source:6)
06-13 11:49:29.594 7832 7938 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
06-13 11:49:29.594 7832 7938 E AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
06-13 11:49:29.594 7832 7938 E AndroidRuntime: at java.lang.Thread.run(Thread.java:764)
I think this is because you're removing the NDEF listener in the event handler. Adding and removing listeners changes recently and restarts nfc as part of the process, so code that worked before might fail now.
Can you see if this works better?
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { NFC, Ndef } from '@ionic-native/nfc';
import { Subscription } from 'rxjs/subscription';
@IonicPage()
@Component({
selector: 'page-nfc',
templateUrl: 'nfc.html',
})
export class NfcPage {
subscription:Subscription;
constructor(public navCtrl: NavController, private nfc: NFC, private ndef: Ndef) {
}
ionViewWillEnter() {
// long form, handles the promise when adding the listener
this.subscription = this.nfc.addNdefListener(
() => console.log('Added NDEF Listener'),
error => console.log('Error adding NDEF listener')
).subscribe(this.onNfcEvent.bind(this));
// optionally you can skip the promise and just subscribe
//this.subscription = this.nfc.addNdefListener().subscribe(this.onNfcEvent.bind(this));
}
onNfcEvent(nfcEvent) {
console.log('NFC Event', nfcEvent);
let tagId = this.nfc.bytesToHexString(nfcEvent.tag.id);
console.log('UID',tagId);
var message = this.ndef.textRecord('hello, world', 'en', null);
this.nfc.write([message]).then(
() => console.log('Wrote data to NFC tag'),
error => console.log('Error writing to NFC tag', error)
)
}
ionViewWillLeave() {
console.log('Leaving view. Removing NDEF subscription');
// unsubscribe so tags don't scan on other pages
// unsubscribing calls ble.removeNdefListener
this.subscription.unsubscribe();
}
}
Hey @doncoleman, thank you for answering, I don't haveadb logcat
, but I tried your code, and work fine, thank you for everything. I'll close this problem.
let flags = this.nfc.FLAG_READER_NFC_A | this.nfc.FLAG_READER_NFC_V;
this.nfc.readerMode(flags).subscribe(
tag => {
alert(JSON.stringify(tag))
// this.nfc.connect('android.nfc.tech.MifareClassic', 3000).then(
// () => {
// this.nfc.addNdefListener(() => {
// //this.writetag();
// this.presentAlert('ok');
// //this.displayNdef;
// }, (err) => {
// this.presentAlert('ko' + err);
// }).subscribe( () => {
// alert('debut');
// this.writetag();
// alert("fin");
// // alert(event.tag.id);
// }
// , err => alert(err));
this.subscription = this.nfc.addNdefListener(
() => alert('Added NDEF Listener'),
error => alert('Error adding NDEF listener')
).subscribe(this.onNfcEvent.bind(this));
});
// },
// (error) => alert('connection failed' + error)
// )
err => alert('Error reading tag' + err)
this.nfc.close;
}
read work but write d'ont work watever i did . who could help please ?!
Hello Don, I updated the plugin to the latest version 1.0.3, but i have a problem, my app reads and write to an NFC tag. Reading and writing works correctly, but when I exit the read/write page and goes back on it to write to an NFC tag, the app closes automatically.
My code:
init() {
I am using:
cli packages: (/usr/local/lib/node_modules)
global packages:
local packages: