Print data from android app using bluetooth printe

2020-07-25 10:35发布

问题:

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 .

回答1:

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 ) { }
    }