3

I am developing an android app for restaurant . I want to print the bill after ordering from my app . I am using Bluetooth printer for the printing task . Any body having experience in printing data using Bluetooth printer ,please help with some suitable examples .

Anoop ka
  • 143
  • 1
  • 4
  • 8

1 Answers1

4

Yes, I have experience of same application. You need to use StreamConnection class with the InputStream and OutputStream class.

First you need to make connection with Bluetooth Printer using its Bluetooth Address and then you need to send characters to print using OutputStream class.

On the printer side when it will fetch the characters it will directly start printing it.

private StreamConnection bConn = null; 
private DataOutputStream dos = null; 

 try
 {
    bConn = (StreamConnection) Connector.open(PrinterURL);
    dos = (DataOutputStream)bConn.openDataOutputStream();
    dos.writeUTF("\r\n");
    dos.writeUTF("===============================");dos.writeUTF("\r\n");
    dos.writeUTF("          GSECL Bill"); dos.writeUTF("\r\n");
    dos.writeUTF("===============================");dos.writeUTF("\r\n");
  }
  catch ( Exception e ) { System.out.println "Server Error: " + e.toString() );

finally
{
    try
        { 
        dos.close();
    bConn.close();
    }
    catch ( Exception e ) { }
    }
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • 1
    How can i get the Bluetooth Address of bluetooth printer . – Anoop ka Aug 21 '12 at 05:02
  • create a sample project that detects bluetooth devices, and from that, you can find it :) – Lucifer Aug 21 '12 at 05:07
  • sure, i suggest you to ask a new question, so it can be helpful for others as well. – Lucifer Aug 21 '12 at 05:20
  • @Lucifer hello, I want help..i want to use bluetooth printer.. in above code u have mention StreamConnection, bt i could not find it..and 'bConn = (StreamConnection) Connector.open(PrinterURL);' what is Connector here? plz help me.. – Kinjal Shah Jan 01 '13 at 13:26
  • 2
    @KinjalShah, Connector class is used to make HttpConection/StreamConnectoin in J2ME, you can search its description on google.com – Lucifer Jan 01 '13 at 15:09
  • @Lucifer I have followed this link. [http://stackoverflow.com/questions/7145787/bluetooth-printer-issue-in-android] and use snap of it for printing. but problem is only first time its get printed.so can u help me to solve this problem? Thanks.. – Kinjal Shah Jan 02 '13 at 09:47
  • 2
    @KinjalShah sure, do one thing, ask a new fresh question with your code and error code ( if you have any ), this way you may get more response from other developers as well :) – Lucifer Jan 03 '13 at 02:35