Cannot use the BarcodeScanner twice


#1

Hi everyone,

Running my app on Ionic view I can’t reuse the barcode scanner process when it’s called the second time (it freeze the app).

Here is my code :

In the controller

    constructor(public navCtrl: NavController) {
        this.scan();
    }

    scan() {
        BarcodeScanner.scan().then((barcodeData) => {
            alert(bardcodeData.text);
        }, (err) => {
            alert(err);
        });
    }

In the view :

<ion-header>
  <ion-navbar>
    <ion-title>
      Scan
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
    <button ion-button full (click)="scan()">Rescan</button>
</ion-content>

Do you know why the barcode scan freeze when it’s used the second time (when I click on the button) ? Did I miss something ?


#2

I have an idea. It might not help but worth the try. Do NOT call native plugins from the constructor. Call the scan() method from ionViewDidEnter lifecycle hook or call it from OnInit (one of the angular2 lifecycle hook).
Ionic lifecycle events: http://ionicframework.com/docs/v2/api/navigation/NavController/#lifecycle-events
Angular2 lifecycle hooks: https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html


#3

Thanks it work like a charm !