Question cordova integration


#1

Hey @ all,

I have a question. How can I include this cordova plugin inside my Ionic 3 application?

The installation itself is ok - I had to clone it local and install it by just using “cordova plugin add....” and so on. But how do I have to include the exports file? With the normal Ionic native modules you do for example somthing like this:

import { BLE } from ‘@ionic-native/ble’;

But in this case I don’t have something like this. The package include also a file called “BluetoothPrinter.js” which is located in the path “./node_modules/BluetoothPrinter/www/BluetoothPritner.js”. This file contains the exports of the corodova module…

var exec = require('cordova/exec');

var BTPrinter = {
    list: function (fnSuccess, fnError) {
        exec(fnSuccess, fnError, "BluetoothPrinter", "list", []);
    },
    connect: function (fnSuccess, fnError, name) {
        exec(fnSuccess, fnError, "BluetoothPrinter", "connect", [name]);
    },
    disconnect: function (fnSuccess, fnError) {
        exec(fnSuccess, fnError, "BluetoothPrinter", "disconnect", []);
    },
    print: function (fnSuccess, fnError, str) {
        exec(fnSuccess, fnError, "BluetoothPrinter", "print", [str]);
    },
    printText: function (fnSuccess, fnError, str) {
        exec(fnSuccess, fnError, "BluetoothPrinter", "printText", [str]);
    },
    printImage: function (fnSuccess, fnError, str) {
        exec(fnSuccess, fnError, "BluetoothPrinter", "printImage", [str]);
    },
    printPOSCommand: function (fnSuccess, fnError, str) {
        exec(fnSuccess, fnError, "BluetoothPrinter", "printPOSCommand", [str]);
    },
    printQRCode: function (fnSuccess, fnError, str) {
        exec(fnSuccess, fnError, "BluetoothPrinter", "printQRCode", [str]);
    }
};

module.exports = BTPrinter;

Thanks in advance,
Oliver


#2

Here is an example for Ionic, but it’s still not working - BTPrinter is alsways undefined and causes a crash.

Code example:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

declare let BTPrinter: any;

@Component({
	selector: 'page-home',
	templateUrl: 'home.html'
})
export class HomePage {
	constructor(public navCtrl: NavController) {
	}

	public print(): void {
		BTPrinter.list((data) => {
			console.log('Printers list:', data);
		}, (err) => {
			console.error(`Error: ${err}`);
		});
	}
}