0

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

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,此时我不知道这是否真的是与打印机相关的问题。¿ 任何人都可以帮我解决这个问题吗?提前致谢

4

1 回答 1

0

首先尝试使用“\n”在字符串末尾添加一些空行。如果输出仍然不完整,请更改device.CreateInsecureRfcommSocketToServiceRecorddevice.createRfcommSocketToServiceRecord.

于 2018-03-07T22:26:27.440 回答