techie
techie

Reputation: 463

Printing Text on Thermal Printer using Bluetooth and ESC/POS

I'm trying to print texts on my bluetooth thermal printer, using ESC/POS commands, but I could not achieve it. I have a class that connects to bluetooth:

try
            {
                BluetoothDevice printer = null;

                if (devices == null)
                    devices = BluetoothAdapter.DefaultAdapter.BondedDevices;

                List<BluetoothDevice> devicesBonded = new List<BluetoothDevice>();

                foreach (var device in devices)
                {
                    if (device.Name != name)
                        continue;

                    printer = device;
                    break;
                }

                socket = printer.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));
                socket.Connect();

                if (!socket.IsConnected)
                    return false;

                return true;
            }
            catch (Exception)
            {
                return false;
            }

and a class to print, called Print(byte[] bytesPrint):

try
            {
                string textPrint = System.Text.Encoding.UTF8.GetString(bytesPrint);

                bytesPrint = Encoding.UTF8.GetBytes(textPrint);

                var teste = socket.RemoteDevice;

                var outStream = (OutputStreamInvoker) socket.OutputStream;

                await outStream.WriteAsync(bytesPrint, 0, bytesPrint.Length);
                
                socket.Close();
            }catch(Exception e)
            {
                socket.Close();
            }

It's in byte[] to some cases, but it's printing correctly (with no ESC/POS commands). But I need to align the text, change font size, font family and print it! However I couldn't find some library or class with ESC/POS commands and the usage of it. I don't want to use AppendFormat() because it's too relative depending the size of paper that I will print.

Does anyone know how to do (with examples please)?

Upvotes: 1

Views: 2704

Answers (1)

vesperto
vesperto

Reputation: 883

Use the reference to send byte arrays to the bluetooth stream. You can't really print without using ESC/P, it's just hidden under layers of SDK abstractions.

Upvotes: 0

Related Questions