How to connect bluetooth printer using ionic 3?

牧云@^-^@ 提交于 2021-02-08 10:21:21

问题


I am unable to connect with bluetooth printer using below plugin

$ ionic cordova plugin add de.appplant.cordova.plugin.printer
$ npm install --save @ionic-native/printer

Is there any way to connect bluetooth printer using ionic 3?


回答1:


Here's an example of printing to bluetooth receipt printer with ionic 2+. I wrote this because I was having the same problem.

So I tried using ionic native bluetooth serial connection to 'write' to print and it worked.

It should work on ionic 3 as well:

https://github.com/razmans/ionicBluetoothPrint




回答2:


Follow these 3 steps: 1-Find bluetooth devices, 2-Connect the device with its id, 3-Print

devices = [];
btnFindDevices() {
   this.bluetoothSerial.isEnabled().then(() => {
   this.bluetoothSerial.discoverUnpaired().then((allDevices) => {
   this.devices = allDevices;
   console.log(allDevices);
});
});
}

btnBlueToothConnect() {
   if (this.devices.length > 0) {
   //this code connects device which’s position is 0. Change it whatever you 
   //want.
   this.bluetoothSerial.connect(this.devices[0].id).subscribe((data) => {
   console.log(“Connected”, data);
   }, (error) => {
   console.log(“not Connected”, error);
   });
    }
    else {
    console.log(“Device List did not genereted yet.”);
    }
    }

    btnBlueToothPrint() {
    //Attention… Bluetooth printer prints data when whole line filled. For 
    //example in my case printer is 32 colon,
    //“hello world” has 11 characters. so it prints after 3 times clicked 
    //the print button.
        this.bluetoothSerial.write(‘hello world’).then(() => { console.log(“s”); }, () => { console.log(“f”); });
    }


来源:https://stackoverflow.com/questions/46947164/how-to-connect-bluetooth-printer-using-ionic-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!