issue printing from app to thermal printer

1k views Asked by At

Hi guys i'm creating a method to print an invoice from my app, but when i send the bytes to print it doesn't print all of the bytes i'm sending to the printer, the last bytes are getting cut all time resulting in an incomplete invoice, this is the code that i'm using at the moment:

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

i'm sending a string and converting it to bytes in the method above, the string is a custom invoice that i made with the invoice data from another page of my app. The printer that i'm using is Bixolon SPP-R310, at this point i don't know if it is a printer related issue really. ¿Can anyone help me with this pls? thanks in advance

1

There are 1 answers

2
Sotiris S. Magionas On

First of all try to append some empty lines at the end of your string using "\n". If the output is still incomplete change device.CreateInsecureRfcommSocketToServiceRecord to device.createRfcommSocketToServiceRecord.