如何在ionic 2/3中调用Cordova Brother标签打印机插件?

时间:2018-07-07 09:07:36

标签: cordova ionic3

我正在使用插件 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);
     });
  }
}

然后在运行/构建后: 错误参考错误:未定义BrotherPrinter enter image description here

请帮助,如何定义/调用此插件,导致它通常无法正常工作。

2 个答案:

答案 0 :(得分:1)

根据plugin.xml file,全局对象似乎是:

moauris@AUR00101:~$ /pp/nmon/nmon -s 60 -c 1440 -f -m /mnt/c/Perf_data/

尝试:

cordova.plugins.brotherPrinter

在您的declare var cordova; 函数中,

print

答案 1 :(得分:0)

我只是在插件中添加了一个用于打印数量的参数。这适用于QL-810W Brother打印机。

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