Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use library for ionic bluetooth printer #10

Open
alejandrojcc opened this issue May 12, 2019 · 1 comment
Open

Use library for ionic bluetooth printer #10

alejandrojcc opened this issue May 12, 2019 · 1 comment

Comments

@alejandrojcc
Copy link

alejandrojcc commented May 12, 2019

I have adapted your code to my code to be able to print a qr by a bluetooth printer using esc / pos command on ionic 4

prepareToPrint2() {
    const data = this.qr('We can put all kinds of cool things in these...', QRErrorCorrectLevel.M, 8).buffer;
    console.log(data);
    this.mountAlertBt(data);
  }

  public qr(code: string, errorCorrect: QRErrorCorrectLevel, size: 1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16): any {
    const GS = 0x1D;
    this.write(GS);
    this.write('(k');
    this.buffer.writeUInt16LE(code.length + 3);
    this.write(new Uint8Array([49, 80, 48]));
    this.write(code);

    this.write(GS);
    this.write('(k');
    this.write(new Uint8Array([3, 0, 49, 69]));
    this.write(errorCorrect);

    this.write(GS);
    this.write('(k');
    this.write(new Uint8Array([3, 0, 49, 67]));
    this.write(size);

    this.write(GS);
    this.write('(k');
    this.write(new Uint8Array([3, 0, 49, 81, 48]));
    return this;
}
public write(value: string | Uint8Array | number, encoding?: string): any {
  this.buffer = new MutableBuffer();
  if (typeof value === 'number') {
      this.buffer.writeUInt8(value);
  } else if (typeof value === 'string') {
      this.buffer.write(iconv.encode(value, encoding || 'ascii'));
  } else {
      this.buffer.write(value);
  }
  return this;
}

This is the method that sends the data to the ionic library

async print(device, data) {
    console.log('Data a imprimir');
    console.log(data);
    console.log(JSON.stringify(device));
    this.presentLoading('Printing....');
    this.printer.connectBluetooth(device).subscribe(
      (status) => {
        console.log(status);
        //this.printer.printData(noSpecialChars(data))
        this.printer.printData(data)
          .then(async printStatus => {
            console.log('Impresión correcta');
            console.log(printStatus);
          })
          .catch((error) => {
            console.log('Impresión fallida');
            console.log(JSON.stringify(error));
          });
      },
      (error) => {
      },
    );
  }

import { BluetoothSerial } from '@ionic-native/bluetooth-serial/ngx';

Print but not the QR code only many characters in Japanese

I was able to print pure text using the hexadecimal code directly
For example

TEXT_FORMAT: {
       TXT_NORMAL: '\ x1b \ x21 \ x00', // Normal text
       TXT_2HEIGHT: '\ x1b \ x21 \ x10', // Double height text
       TXT_2WIDTH: '\ x1b \ x21 \ x20'}

do you have any idea

@haavardlian
Copy link
Owner

I don't really see where you get the data you send over to the printer. If the data you are trying to write is what is returned from the Printer.qr method that will not work as the Printer object itself is returned.

You need to call all the printer methods you want (only qr in your case here I guess) and then call flush which will give you a UInt8Array that you can write to your bluetooth device.

If you want to use this library the easiest way to print to a new device is to create a new sub class of Adapter and then pass that as the parameter to the constructor for the Printer object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants