从应用程序打印到热敏打印机

时间:2018-03-07 22:06:57

标签: android android-bluetooth thermal-printer bixolon-printer

大家好我正在创建一种从我的应用程序打印发票的方法,但是当我发送字节进行打印时,它不会打印我发送到打印机的所有字节,最后的字节正在被切断所有时间导致发票不完整,这是我目前正在使用的代码:

public void Print(string nombreImpresora, string formatoFactura)
        {
            var listOfDevices = BluetoothAdapter.DefaultAdapter;
            if (listOfDevices == null)
                throw new Exception("No Bluetooth adapter found.");

            if (!listOfDevices.IsEnabled)
                throw new Exception("Bluetooth adapter is not enabled.");

            var device = (from bd in listOfDevices.BondedDevices
                          where bd.Name == nombreImpresora
                          select bd).FirstOrDefault();

            if (device == null)
                throw new Exception("Named device not found.");


            BluetoothSocket socket;
            var uuid = device.GetUuids()?.ElementAt(0);
            if (uuid != null)
            {
                socket = device.CreateInsecureRfcommSocketToServiceRecord(uuid.Uuid);
            }
            else
            {
                socket = device.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
            }

            if (socket.IsConnected)
            {
                return;
            }
            socket.Connect();

            byte[] completeBuffer = Encoding.ASCII.GetBytes(formatoFactura);
            Toast.MakeText(Forms.Context, Convert.ToString(completeBuffer.Length), ToastLength.Short).Show();
            var bufferSize = 256;
            int completedBufferLength = completeBuffer.Length;
            List<byte[]> bufferList = new List<byte[]>();

            for (int i = 0; i < completedBufferLength; i = i + bufferSize)
            {
                byte[] val = new byte[bufferSize];

                if (completedBufferLength < i + bufferSize)
                {
                    bufferSize = completedBufferLength - i;
                }

                Array.Copy(completeBuffer, i, val, 0, bufferSize);
                bufferList.Add(val);
            }

            for (int j = 0; j < bufferList.Count; j++)
            {
                socket.OutputStream.Write(bufferList[j], 0, bufferList[j].Length);
            }

            socket.Close();
            socket.Dispose();
        }

我发送一个字符串并将其转换为上述方法中的字节,该字符串是我使用我的应用的另一个页面的发票数据制作的自定义发票。 我正在使用的打印机是Bixolon SPP-R310,此时我不知道它是否真的与打印机相关。 ¿任何人都可以帮我这个吗? 提前谢谢

1 个答案:

答案 0 :(得分:0)

首先尝试使用&#34; \ n&#34;在字符串的末尾添加一些空行。如果输出仍然不完整,请将device.CreateInsecureRfcommSocketToServiceRecord更改为device.createRfcommSocketToServiceRecord

相关问题