Rifai
Rifai

Reputation: 194

How to call cordova brother label printer plugin in ionic 2/3?

i'm using plugin https://github.com/gordol/cordova-brother-label-printer

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

declare let BrotherPrinter:any;

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

  code:any={text:''};

  constructor(
    public navCtrl: NavController,
    public barcodeScanner:BarcodeScanner,

  ) {

  }

  print(){
    BrotherPrinter.findBluetoothPrinters(function(data){
        console.log("Success");
        console.log(data)
    },function(err){
        console.log("Error");
        console.log(err)
    });
  }
  
  scan(){
    this.barcodeScanner.scan().then(barcodeData => {
      console.log('Barcode data', barcodeData);
      this.code = barcodeData;
     }).catch(err => {
         console.log('Error', err);
     });
  }
}

then after run/build : ERROR ReferenceError: BrotherPrinter is not defined enter image description here

please help, how to define/call this plugin, cause it doesn't work usually.

Upvotes: 1

Views: 1363

Answers (2)

Chetan Verve
Chetan Verve

Reputation: 9

I just added one parameter in plugin for number of prints. This is work for QL-810W Brother Printer.

printFromBrotherPrinter(base64String, noOfPrintLocal) {
let that = this;

return new Promise((resolve, reject) => {

  cordova.plugins.brotherPrinter.findPrinters((success) => {
    that.printerResponse = success;
    if (that.printerResponse.length > 0) {

      var printer = {
        "model": that.printerResponse[0].model,
        "port": that.printerResponse[0].port,
        "modelName": that.printerResponse[0].modelName,
        "ipAddress": that.printerResponse[0].ipAddress,
        "macAddress": that.printerResponse[0].macAddress,
        "nodeName": that.printerResponse[0].nodeName,
        "location": ""
      };

      cordova.plugins.brotherPrinter.setPrinter(printer, function (success) {
        var dataArray = {
          "base64String": base64String,
          "numberOfCopies": noOfPrintLocal
        };

        cordova.plugins.brotherPrinter.printViaSDK(dataArray, function (printResult) {
          //var printResult = callback;
          if (printResult == null) {
            // iOS result
            resolve(1);
          }
          else {
            if (printResult.result != null && printResult.result !== undefined) {
              if (printResult.result == 'Succeeded') {
                // Print Success
                resolve(1);
              }
              else {
                resolve(2);
              }
            }
            else {
              resolve(2);
            }
          }
        });
      }, function (error) {
        reject(2);
      });
    } else {
      console.log("No printer found");
      reject(2);
    }
  }, (error) => {
    console.log("Find Printer Error: " + JSON.stringify(error));
    reject(2);
  });
});

Upvotes: 0

Suraj Rao
Suraj Rao

Reputation: 29625

According to the plugin.xml file, the global object seems to be:

cordova.plugins.brotherPrinter

Try:

declare var cordova;

And in your print function,

cordova.plugins.brotherPrinter.findBluetoothPrinters((data)=>{
    console.log("Success");
    console.log(data)
},(err)=>{
    console.log("Error");
    console.log(err)
});

Upvotes: 1

Related Questions