微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用蓝牙和 ESC/POS 在热敏打印机上打印文本

如何解决使用蓝牙和 ESC/POS 在热敏打印机上打印文本

我正在尝试使用 ESC/POS 命令在我的蓝牙热敏打印机上打印文本,但我无法实现。我有一个连接到蓝牙的类:

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;
            }

一个要打印的类,称为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,bytesPrint.Length);
                
                socket.Close();
            }catch(Exception e)
            {
                socket.Close();
            }

在某些情况下它在 byte[] 中,但它打印正确(没有 ESC/POS 命令)。但我需要对齐文本,更改字体大小,字体系列并打印!但是我找不到一些带有 ESC/POS 命令及其用法的库或类。我不想使用 AppendFormat(),因为它与我将打印的纸张尺寸相关。

有人知道怎么做吗(请举例说明)?

解决方法

使用 reference 将字节数组发送到蓝牙流。不使用 ESC/P 就无法真正打印,它只是隐藏在 SDK 抽象层之下。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。