5

I am trying print from Bluetooth printer (INTERMEC PB51), using Xamarin native android.

I have written the code based on the below link.

How can I print an image on a Bluetooth printer in Android?

My code is below.

private static byte[] SELECT_BIT_IMAGE_MODE = { 0x1B, 0x2A, 33, (byte)255, 0 };

Making bitmap as below.

 Bitmap sigImage = BitmapFactory.DecodeResource(Resources, Resource.Drawable.icn_logo_jpg);

Creating Blutooth Socket.

  BluetoothSocket socket = null;
            BufferedReader inReader = null;
            BufferedWriter outReader = null;
            string bt_printer = address; //AdminSettings.PrinterMACAddr;
            if (string.IsNullOrEmpty(bt_printer)) bt_printer = "00:13:7B:49:D1:8C";
            BluetoothDevice mmDevice = BluetoothAdapter.DefaultAdapter.GetRemoteDevice(bt_printer);
            UUID applicationUUID = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
            socket = mmDevice.CreateRfcommSocketToServiceRecord(applicationUUID);
            socket.Connect();

Calling method

 PrintImage(bitMap, socket);

Created method for printing it

 public void PrintImage(Bitmap bitmap, BluetoothSocket _socket)
    {
        try
        {
            if (!_socket.IsConnected)
            {
                _socket.Connect();
            }

            MemoryStream stream = new MemoryStream();

            //IMAGE
            byte[] imageData = ImageToByte2(bitmap);
            stream.Write(imageData, 0, imageData.Length);
            stream.Write(SELECT_BIT_IMAGE_MODE, 0, SELECT_BIT_IMAGE_MODE.Length);
            var bytes = stream.ToArray();
            _socket.OutputStream.Write(bytes, 0, bytes.Length);
            // Java.Lang.Thread.Sleep(2000);

            //END IMAGE
            Java.Lang.Thread.Sleep(2000);
        }
        catch (Exception ex)
        {
            throw new Exception("Unable to print. Please re-configure the printer and try again!");
        }
    }


 public static byte[] ImageToByte2(Bitmap bitmap)
    {
        MemoryStream stream = new MemoryStream();
        bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
        byte[] bitmapData = stream.ToArray();
        return bitmapData;
    }

But getting logo printed as below image.

enter image description here

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
Rakesh
  • 2,730
  • 10
  • 39
  • 65

1 Answers1

1
  1. The Intermec PB51 can be setup with several different printer languages; IPL, Fingerprint, Direct Protocol, ZSim, DSim, CSim and ESC/P. So first you have to know which printer language you are dealing with. { 0x1B, 0x2A, 33, (byte)255, 0 } is a commnd in ESC/P, so the printer must be in ESC/P mode.

  2. Looks like you are sending PNG image data to the printer. I have only used the Intermec PB51 in ESC/P mode, and in ESC/P the image has to be converted to a 1-bit image byte array (one bit per printer "pixel").

emilsteen
  • 496
  • 4
  • 14
  • can you mention the four different printer languages ? – Ryan Motal Jul 16 '20 at 08:35
  • @ryan-motal Updated the answer with the printer languages i found in the manual. – emilsteen Jul 17 '20 at 10:25
  • thanks for the update @emilsteen, my printer also uses ESC/P model, did you convert your bitmap to hex before byte array? can you teach me your methodology in this case? Thanks in advance. – Ryan Motal Jul 18 '20 at 03:21
  • @ryan-motal You take the bits from the image and convert them into an bit array. If your image is grey (every second dot is set) you get the bit array 0b01010101 which is the byte to send to the printer i.e. 0x55. Then how to get the image to an bit array is the tricky part. I'm using ImageSharp where you can read each pixel, but there is probably a better way. – emilsteen Jul 21 '20 at 06:53
  • did you upload your source code to github? and if so, can i see it? that'll help me a lot. i tried a lot of methods, nothings seems to work. – Ryan Motal Jul 21 '20 at 07:47
  • @ryan-motal It's proprietary software unfortunately. Cannot share it. But if your code is on GitHub I'll try to find time to look at it. – emilsteen Jul 21 '20 at 20:13
  • thanks @emilsteen you can visit https://github.com/motalr/XFBluetoothPrinter/tree/master at your convenience – Ryan Motal Jul 22 '20 at 03:12
  • @ryan-motal I had a look at the project. Ignoring that the code is incredibly inefficient and quite messy and I had to do a clean-up before it even compiled, I don't see any obvious errors. But the real issue must be that it is using Shiny.BluetoothLE. The Intermec PB51 is a standard Bluetooth device and not a Bluetooth LE device, so Shiny.BluetoothLE will never be able to connect to it. Did you connect to the device before trying to print graphics to it? – emilsteen Jul 23 '20 at 16:11
  • thanks for checking it out, yes i did connect it first before sending the converted byte array, i tested it with printing text first *apologies for the mess haha – Ryan Motal Jul 24 '20 at 00:58
  • @ryan-motal That was strange, because I could not connect to my PB51 with your app. – emilsteen Aug 04 '20 at 15:41