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.
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.
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").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With