Skip to content

Instantly share code, notes, and snippets.

@mutasimissa
Created November 13, 2023 23:53
Show Gist options
  • Save mutasimissa/72a0d083d05dccaf3d8cffa36eadce89 to your computer and use it in GitHub Desktop.
Save mutasimissa/72a0d083d05dccaf3d8cffa36eadce89 to your computer and use it in GitHub Desktop.
Thermal Printers using Capacitor Ble Plugin
import { BleClient } from "@capacitor-community/bluetooth-le";
import Printer from "thermal-printer-cordova-plugin/www/thermal-printer";
export const Print = async ({ printerFormattedText }) => {
try {
// you need to provide a deviceId, I store some deviceId in the localStorage for example
const printerDeviceID = localStorage.getItem("myDeviceId");
if (!printerDeviceID) {
throw new Error("printer is not connected");
}
await BleClient.initialize();
await BleClient.getConnectedDevices().then((devices) => {
if (devices[0]) {
devices.forEach(
async (device) => await BleClient.disconnect(String(device.deviceId))
);
}
});
await BleClient.connect(printerDeviceID);
// see the used wrapper from https://www.npmjs.com/package/thermal-printer-cordova-plugin
// I only use the Printer helper, you can implement your own or use other helpers
Printer.printFormattedText(
{
type: "bluetooth",
id: printerDeviceID,
mmFeedPaper: 12,
printerWidthMM: 50,
printerNbrCharactersPerLine: 32,
text: printerFormattedText,
},
() => {
Printer.disconnectPrinter({
type: "bluetooth",
id: printerDeviceID,
});
},
(e) => {
throw new Error(e);
}
);
} catch (e) {
return `${e}`;
}
};
import { Print } from "./Print";
export printTestPage = async ()=> {
const printerFormattedText = ""
+ "[L]\n" // new empty line
+ "[C]<u><font size='big'>TEST</font></u>\n" //centered with big font
+ "[L]THIS IS A TEST\n" // left aligned
+ "[C]================================\n"
+ "[R]<b>BEST REGARDS</b>\n" // right aligned, bold
try {
await Print({ printerFormattedText })
}
catch (e) {
console.log(`${e}`}
}
}
@kellyblaire1
Copy link

Thanks a million for this. I'll try it out and revert.

@brandomcombr
Copy link

So, you don't use BleClient.write directly; instead, you use it in conjunction with another plugin for printing? I'll try it here, but I've already seen some issues related to it not working on capacitor 5. If you have any examples with capacitor 5 and can show me, I would greatly appreciate it.

@mutasimissa
Copy link
Author

@brandomcombr I found it much easier to use the wrapper from thermal-printer-cordova-plugin, because bluetooth-le capacitor plugin is not a printing plugin, it focuses on managing ble interfaces in general not ESC-POS printing.
regarding cap5 unfortunately I didn't try anything with cap5 because I'm using flutter for over a year now which set me little bit away from capacitor and ionic.

@saulmonci
Copy link

hey im getting an error with the plugin for printer when i call the method printFormattedText im getting "missing permission for android.permission.DISABLE_KEYGUARD" already put the permission on mainfest file but still there the issue, did you face something like that?

@mutasimissa
Copy link
Author

@saulmonci I'm not sure it's related to any of the plugins used, are you you using something related to unlock the app using the key lock of the native system?

@saulmonci
Copy link

@mutasimissa no nothing realted to unlock the app i just want to send the text to print with the thermal-printer-cordova-plugin pacakge but i get in the console "missing permission for android.permission.DISABLE_KEYGUARD"

@goper-leo
Copy link

@saulmonci Hi did you find a solution? I'm also encounter that error. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment