• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> Blue Bamboo P25 Printer Android Demo Application With Source Code

Blue Bamboo P25 Printer Android Demo Application With Source Code

February 28, 2014 by Lorensius Londa 22 Comments

Blue Bamboo P25 printer is a lightweight and portable thermal printer that provides low cost printing in mobile and wireless environments. It supports mobile application printing over Bluetooth, USB or Serial port (RS-232). It also has a good connectivity with Android device, makes printing from Android becomes very easy and fast.

P25 Printer Android

Blue Bamboo also provides a sample Android project to show how to build an application that supports printing using P25 printer over Bluetooth. The source code itself is quite old so i made a slight modification with fresh UI and easy to read source code so developers can easily implement the codes into their projects.

P25 Android Application Demo

This sample code shows how to print text, image, barcode and receipt into P25 printer using Bluetooth connectivity. The source code is available on Github repo, you can find it at the bottom of this post.

Print Text

private void printText(String text) {
	byte[] line 	= Printer.printfont(text + "\n\n", FontDefine.FONT_32PX, FontDefine.Align_CENTER, (byte) 0x1A, PocketPos.LANGUAGE_ENGLISH);
	byte[] senddata = PocketPos.FramePack(PocketPos.FRAME_TOF_PRINT, line, 0, line.length);

	sendData(senddata);
}

Use Printer.printFont(String content, byte fonttype, byte fontalign,byte linespace, byte language) method to format and position the text. For font types and text alignment, see FontDefine.java and for language definition, see PocketPos.java. Also call the PocketPos.FramePack(byte typeFrame, byte[] srcBuff, int srcOffset, int srcLen) to create frame data before sending to printer.

See P25 development guide page 60-63 for more information.

Print 1D Barcode

private void print1DBarcode() {
	String content	= "6901234567892";

	byte[] formats	= {(byte) 0x1d, (byte) 0x6b, (byte) 0x02, (byte) 0x0d};
	byte[] contents	= content.getBytes();

	byte[] bytes	= new byte[formats.length + contents.length];

	System.arraycopy(formats, 0, bytes, 0, formats.length);
	System.arraycopy(contents, 0, bytes, formats.length, contents.length);

	sendData(bytes);

	byte[] newline 	= Printer.printfont("\n\n",FontDefine.FONT_32PX,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);

	sendData(newline);
}

1D barcode printing format is: 1d 6b 02 0d [barcode data]. So to print “6901234567892” barcode data, the bytes would be:

{(byte) 0x1d, (byte) 0x6b, (byte) 0x02, (byte) 0x0d}
+
“6901234567892”.getBytes()

See P25 development guide page 59-60 for more information.

Print 2D Barcode

private void print2DBarcode() {
	String content 	= "Lorenz Blog - www.londatiga.net";

	byte[] formats	= {(byte) 0x1d, (byte) 0x6b, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
						  (byte) 0x00, (byte) 0x1f};
	byte[] contents	= content.getBytes();
	byte[] bytes	= new byte[formats.length + contents.length];

	System.arraycopy(formats, 0, bytes, 0, formats.length);
	System.arraycopy(contents, 0, bytes, formats.length, contents.length);

	sendData(bytes);

	byte[] newline 	= Printer.printfont("\n\n",FontDefine.FONT_32PX,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);

	sendData(newline);
}

2D barcode printing format is: 1d 6b 10 00 00 00 00 00 1f + barcode data. So to print “Lorenz Blog – www.londatiga.net” text, the bytes would be:

{(byte) 0x1d, (byte) 0x6b, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x1f}
+
“Lorenz Blog – www.londatiga.net”.getBytes()

See P25 development guide page 60 for more information

Print Image

private void printImage() {
	try {
		byte[] formats	= { (byte)0x1B, (byte)0x58, (byte)0x31, (byte)0x24, (byte)0x2D };

		//bluebamboo logo
		byte[] image = {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00, ....... (byte)0x00};

		sendData(bytes);

		byte[] newline 	= Printer.printfont("\n\n",FontDefine.FONT_32PX,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);

		sendData(newline);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

Image printing format is: 1B 58 31 xx yy + {image data in bitmap monochrome format} where xx is width in pixels/8 and yy is height of image in pixels.

So to print 200×45 pixels of image, the format would be :

{ (byte)0x1B, (byte)0x58, (byte)0x31, (byte)0x24, (byte)0x2D } + { bytes of bitmap monochrome image}

Width = 200pixels/8bit = 25 in decimals = 0x24 in hexa
Height= 45 pixels in decimal = 0x2D in hexa

See http://bluebamboo.helpserve.com/index.php?/Knowledgebase/Article/View/48 for more information on how to print image and P25 development guide page 18-13 and 63.

Print Sample Receipt

private void printStruk() {
		String titleStr	= "STRUK PEMBAYARAN TAGIHAN LISTRIK" + "\n\n";

		StringBuilder contentSb	= new StringBuilder();

		contentSb.append("IDPEL     : 435353535435353" + "\n");
		contentSb.append("NAMA      : LORENSIUS WLT" + "\n");
		contentSb.append("TRF/DAYA  : 50/12244 VA" + "\n");
		contentSb.append("BL/TH     : 02/14" + "\n");
		contentSb.append("ST/MTR    : 0293232" + "\n");
		contentSb.append("RP TAG    : Rp. 100.000" + "\n");
		contentSb.append("JPA REF   :" + "\n");

		StringBuilder content2Sb = new StringBuilder();

		content2Sb.append("ADM BANK  : Rp. 1.600" + "\n");
		content2Sb.append("RP BAYAR  : Rp. 101.600,00" + "\n");

		String jpaRef	= "XXXX-XXXX-XXXX-XXXX" + "\n";
		String message	= "PLN menyatakan struk ini sebagai bukti pembayaran yang sah." + "\n";
		String message2	= "Rincian tagihan dapat diakses di www.pln.co.id Informasi Hubungi Call Center: "
						+ "123 Atau Hub PLN Terdekat: 444" + "\n";

		long milis		= System.currentTimeMillis();
		String date		= DateUtil.timeMilisToString(milis, "dd-MM-yy / HH:mm")  + "\n\n";

		byte[] titleByte	= Printer.printfont(titleStr, FontDefine.FONT_24PX,FontDefine.Align_CENTER,
								(byte)0x1A, PocketPos.LANGUAGE_ENGLISH);

		byte[] content1Byte	= Printer.printfont(contentSb.toString(), FontDefine.FONT_24PX,FontDefine.Align_LEFT,
								(byte)0x1A, PocketPos.LANGUAGE_ENGLISH);

		byte[] refByte		= Printer.printfont(jpaRef, FontDefine.FONT_24PX,FontDefine.Align_CENTER,  (byte)0x1A,
								PocketPos.LANGUAGE_ENGLISH);

		byte[] messageByte	= Printer.printfont(message, FontDefine.FONT_24PX,FontDefine.Align_CENTER,  (byte)0x1A,
								PocketPos.LANGUAGE_ENGLISH);

		byte[] content2Byte	= Printer.printfont(content2Sb.toString(), FontDefine.FONT_24PX,FontDefine.Align_LEFT,
								(byte)0x1A, PocketPos.LANGUAGE_ENGLISH);

		byte[] message2Byte	= Printer.printfont(message2, FontDefine.FONT_24PX,FontDefine.Align_CENTER,  (byte)0x1A,
								PocketPos.LANGUAGE_ENGLISH);

		byte[] dateByte		= Printer.printfont(date, FontDefine.FONT_24PX,FontDefine.Align_LEFT, (byte)0x1A,
								PocketPos.LANGUAGE_ENGLISH);

		byte[] totalByte	= new byte[titleByte.length + content1Byte.length + refByte.length + messageByte.length +
		                	           content2Byte.length + message2Byte.length + dateByte.length];

		int offset = 0;
		System.arraycopy(titleByte, 0, totalByte, offset, titleByte.length);
		offset += titleByte.length;

		System.arraycopy(content1Byte, 0, totalByte, offset, content1Byte.length);
		offset += content1Byte.length;

		System.arraycopy(refByte, 0, totalByte, offset, refByte.length);
		offset += refByte.length;

		System.arraycopy(messageByte, 0, totalByte, offset, messageByte.length);
		offset += messageByte.length;

		System.arraycopy(content2Byte, 0, totalByte, offset, content2Byte.length);
		offset += content2Byte.length;

		System.arraycopy(message2Byte, 0, totalByte, offset, message2Byte.length);
		offset += message2Byte.length;

		System.arraycopy(dateByte, 0, totalByte, offset, dateByte.length);

		byte[] senddata = PocketPos.FramePack(PocketPos.FRAME_TOF_PRINT, totalByte, 0, totalByte.length);

		sendData(senddata);
}

Print Demo Page

private void printDemoContent(){

		/*********** print head*******/
		String receiptHead = "************************"
				+ "   P25/M Test Print"+"\n"
				+ "************************"
				+ "\n";

		long milis		= System.currentTimeMillis();

		String date		= DateUtil.timeMilisToString(milis, "MMM dd, yyyy");
		String time		= DateUtil.timeMilisToString(milis, "hh:mm a");

		String hwDevice	= Build.MANUFACTURER;
		String hwModel	= Build.MODEL;
		String osVer	= Build.VERSION.RELEASE;
		String sdkVer	= String.valueOf(Build.VERSION.SDK_INT);

		StringBuffer receiptHeadBuffer = new StringBuffer(100);

		receiptHeadBuffer.append(receiptHead);
		receiptHeadBuffer.append(Util.nameLeftValueRightJustify(date, time, DataConstants.RECEIPT_WIDTH) + "\n");

		receiptHeadBuffer.append(Util.nameLeftValueRightJustify("Device:", hwDevice, DataConstants.RECEIPT_WIDTH) + "\n");

		receiptHeadBuffer.append(Util.nameLeftValueRightJustify("Model:",  hwModel, DataConstants.RECEIPT_WIDTH) + "\n");
		receiptHeadBuffer.append(Util.nameLeftValueRightJustify("OS ver:", osVer, DataConstants.RECEIPT_WIDTH) + "\n");
		receiptHeadBuffer.append(Util.nameLeftValueRightJustify("SDK:", sdkVer, DataConstants.RECEIPT_WIDTH));
		receiptHead = receiptHeadBuffer.toString();

		byte[] header = Printer.printfont(receiptHead + "\n", FontDefine.FONT_32PX,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);

		/*********** print English text*******/
		StringBuffer sb = new StringBuffer();
		for(int i=1; i<128; i++)
			sb.append((char)i);
		String content = sb.toString().trim();

		byte[] englishchartext24 			= Printer.printfont(content + "\n",FontDefine.FONT_24PX,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);
		byte[] englishchartext32			= Printer.printfont(content + "\n",FontDefine.FONT_32PX,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);
		byte[] englishchartext24underline	= Printer.printfont(content + "\n",FontDefine.FONT_24PX_UNDERLINE,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);

		//2D Bar Code
		byte[] barcode = StringUtil.hexStringToBytes("1d 6b 02 0d 36 39 30 31 32 33 34 35 36 37 38 39 32");

		/*********** print Tail*******/
		String receiptTail =  "Test Completed" + "\n"
				+ "************************" + "\n";

		String receiptWeb =  "** www.londatiga.net ** " + "\n\n\n";

		byte[] foot = Printer.printfont(receiptTail,FontDefine.FONT_32PX,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);
		byte[] web	= Printer.printfont(receiptWeb,FontDefine.FONT_32PX,FontDefine.Align_CENTER,(byte)0x1A,PocketPos.LANGUAGE_ENGLISH);

		byte[] totladata =  new byte[header.length + englishchartext24.length + englishchartext32.length + englishchartext24underline.length +
		                              + barcode.length
		                             + foot.length + web.length
		                             ];
	 	int offset = 0;
		System.arraycopy(header, 0, totladata, offset, header.length);
		offset += header.length;

		System.arraycopy(englishchartext24, 0, totladata, offset, englishchartext24.length);
		offset+= englishchartext24.length;

		System.arraycopy(englishchartext32, 0, totladata, offset, englishchartext32.length);
		offset+=englishchartext32.length;

		System.arraycopy(englishchartext24underline, 0, totladata, offset, englishchartext24underline.length);
		offset+=englishchartext24underline.length;

		System.arraycopy(barcode, 0, totladata, offset, barcode.length);
		offset+=barcode.length;

		System.arraycopy(foot, 0, totladata, offset, foot.length);
		offset+=foot.length;

		System.arraycopy(web, 0, totladata, offset, web.length);
		offset+=web.length;

		byte[] senddata = PocketPos.FramePack(PocketPos.FRAME_TOF_PRINT, totladata, 0, totladata.length);

		sendData(senddata);
}

All bytes data to be printed is sent to printer using sendData method from P25Connector class (P25Connector.java)

private void sendData(byte[] bytes) {
	try {
		mConnector.sendData(bytes);
	} catch (P25ConnectionException e) {
		e.printStackTrace();
	}
}

P25Connector is a wrapper class to handle bluetooth operation (connect, disconnect, and bluetooth socket operation)

Demo video

Real life application video

[ad#ad-720×90]

Download source code from Github repo

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. Sample Code to Get MD5 Checksum of a File in Android
  2. How to Use Foursquare API on Android Application
  3. How to Setup Android Application Development on Eclipse
  4. Simple JSON RPC Client for Android

Filed Under: Android, Featured Articles Tagged With: blue bambbo, bluetooth, demo application, p25, printer, source code

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. alfonso vergara says

    May 6, 2014 at 1:51 pm

    This is awesome! Is it possible to use it alongside a credit card reader application like square:

    https://squareup.com/?priorityCode=3969702399&cid=aff_cj_6150547

    Thanks! You’re awesome!

    Reply
  2. Azhar says

    June 7, 2014 at 7:07 pm

    Slmt malam.Pak bisa tlg dibantu cara cek mac address nya. Printer sy Blue Bamboo P25. Pin nya 19734637. No serinya P25_062135_01. Sy sangat perlu sekali pak mac address. Sblm dan sesudahnya sy mengucapkan banyak terima kasih. Kl bisa di email saja pak ke : asharyunus2112@gmail.com.

    Reply
  3. phal says

    June 30, 2014 at 11:05 pm

    Hi have gotten a hand of your tutorial. But can’t print any image I pass unto Method getBytesFromAssets in the FileOperation Class.
    The picture is located within the assets folder and still doesn’t work.

    Reply
  4. Arya Patel says

    July 4, 2014 at 6:35 pm

    please send me demo for Map view inside scroll view & Print recipients for printer for
    android

    Reply
  5. moiky says

    August 28, 2014 at 4:09 pm

    Great one Loresius,
    From the source code when i printtext, reciept, barcode, or demo there extra strings printed before the actual print, do you know how they can be removed. eg printext > string (“Test”) will Print 1D456Test instead of just Test

    Reply
  6. ziahaqi says

    October 13, 2014 at 11:29 am

    tutorialnya bagus mas, oh iya, saya untuk ngeprint image dengan contoh gambar sendiri dengan extensi bitmap monochrome dengan ukuruan seperti yang dijelaskan diatas, tapi masih hancur ya kira2 sebabnya apa yak..? trimakasih

    Reply
  7. luis says

    October 15, 2014 at 12:54 am

    shows the p200 blue bamboo tutorial, please

    Reply
  8. Rofid says

    November 21, 2014 at 2:55 pm

    Om Boleh di kirimin sorce code lenkap nggak ?? Buat Tugas nih ,,, Izin untuk di ATM dong kalau boleh rofidhilmi59@gmail.com. Terimakasih

    Reply
  9. Ferri Sutanto says

    December 3, 2014 at 8:24 pm

    Hi

    Do you have library to convert jpg image to image data in bitmap monochrome format?

    Thanks

    Reply
  10. Andy says

    December 4, 2014 at 10:17 pm

    Hello,

    Sebelumnya saya ucapkan terkma kasih atas artkel blognya mengenai printer bluebamboo yg sdh diupload di github. Yang ingin saya tanyakan, apakah memungkinkan sdk tersebut digunakan untuk PHP web base system ?

    Kasusnya begini: di tempat tersbut sudah memilikki aplikasi php web base , jadi rencananya, aplikasi web base tersebut mau dibuka di tablet dan kemudian print menggunskan portable pos bluebamboo. Apakah memungkinkan ?

    Terima kasih sebelumnya

    Reply
  11. jafar says

    December 22, 2014 at 4:40 pm

    540919

    Reply
  12. mesut says

    January 15, 2015 at 9:32 pm

    hi i want to develop a app which use zebra imz 320 pinter with bluetooth on android. your project(p25demo) is very helpfull for me. how can i set paper size? can u help me?

    Reply
  13. kevin says

    February 11, 2015 at 12:09 am

    thanks for the tutorial. In your image printing, you said the image is 200px wide, but 0x24 is not 25 pixels (rather 36 px). I think you want 0x19.

    Reply
  14. Innocent HARELIMANA says

    February 25, 2015 at 6:33 pm

    Great post! It’s helping me a lot. Just bought BlueBamboo P10 bluetooth printer and thanks to this post I am able to print!

    Now Dear Lorensius, how do you generate that hex string of the monochrome image?

    Last, I printed the image once and on subsquent printing wierd string are being printed out instead of the image.

    I’d appreciate your help on the above.

    Thanks in advance.

    Innocent

    Reply
  15. juanda says

    February 25, 2015 at 8:28 pm

    I have the printer but need to download the software on my phone. Is there a link to a download? I changed phones and need to download again. I use a square but use to use intuit as a merchant account. please advise.

    Reply
  16. juanda says

    February 25, 2015 at 8:30 pm

    need the software to download. I have the printer. changed form iphone to android. used intuit and now using square.

    Reply
  17. José rodrigues says

    March 11, 2015 at 2:17 am

    hi is possible to use asyn task to connect the background and print on posExecute ?

    Reply
  18. Dani Carla says

    April 7, 2015 at 1:36 am

    Which directory should I put these files I downloaded the Github

    Reply
  19. Ankit Jain says

    October 3, 2016 at 10:16 pm

    Hi, Your demo look cool, but have question that is this only for “Blue Bamboo P25 printer” other printer will also work or not because I am not able to get the list of Bluetooth devices.
    Can you please help me.

    Reply
  20. Akshatha says

    October 27, 2016 at 6:48 pm

    I am not able to connect to other bluetooth device from android phone.

    Reply
  21. Zam says

    April 19, 2017 at 9:21 am

    Android Demo Application With Source Code Blue Bamboo P25 Printer Android Demo Application With Source Code.

    Can i get this demo code for Android studio ?

    Reply
  22. Zam says

    April 19, 2017 at 9:24 am

    Android Demo Application With Source Code
    Blue Bamboo P25 Printer Android Demo Application With Source Code.

    am trying with android studio. Can i get the code for that ?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

  • @tricahyono_bowo @infobandung @infobdg Wah kejauhan om 355 days ago
  • Wilujeng enjing sadayana..Mohon info tempat powder coating dan sandblasting yg recommended di Bandung dunk @infobandung @infobdg359 days ago

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in