'Android Bluetooth Printing

I am writing an application which sends data to bluetooth printer. Can anyone help me ? how can I use android Bluetooth Stack for printing? or is there any external api or sdk to use?

Here is my code for searching bluetooth...

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
registerReceiver(ActionFoundReceiver,
        new IntentFilter(BluetoothDevice.ACTION_FOUND));

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            btArrayAdapter.add(device.getName() + "\n"
                    + device.getAddress());
            btArrayAdapter.notifyDataSetChanged();
        }
    }
};

and here is my code for sending data to printer..

BluetoothDevice mDevice = bluetoothAdapter.getRemoteDevice("00:15:FF:F2:56:A4");
Method m = mDevice.getClass().getMethod("createRfcommSocket",
        new Class[] { int.class });
mBTsocket = (BluetoothSocket) m.invoke(mDevice, 1);
System.out.println("Connecting.....");
mBTsocket.connect();
System.out.println("Connected");
OutputStream os = mBTsocket.getOutputStream();
os.flush();
os.write(Receipt.getBytes());
// mBTsocket.close();

When I write socket.close() , data is not getting print to printer as socket connection getting closed before printing data..and if I didn't write socket.close() then data is getting printed only once.. I would not be able to print data second time until I restart bluetooth of my phone.

can any one have solution for it??? or is there any other way to get rid of this printing??



Solution 1:[1]

I got the solution of my problem...

if i want to print data more than one time then you dont need to create new Socket Connection with the device... instead call outputstream.write(bytes) method.

and in the end if you want to disconnect device then call mBTScoket.close() method to disconnect device.

Solution 2:[2]

You can use printooth library for any printer, printooth is simple and well documented, https://github.com/mazenrashed/Printooth

var printables = ArrayList<Printable>()
var printable = Printable.PrintableBuilder()  
    .setText("Hello World") //The text you want to print
    .setAlignment(DefaultPrinter.ALLIGMENT_CENTER)
    .setEmphasizedMode(DefaultPrinter.EMPHASISED_MODE_BOLD) //Bold or normal  
    .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL)
    .setUnderlined(DefaultPrinter.UNDELINED_MODE_ON) // Underline on/off
    .setCharacterCode(DefaultPrinter.CHARACTER_CODE_USA_CP437) // Character code to support languages
    .setLineSpacing(DefaultPrinter.LINE_SPACING_60)
    .setNewLinesAfter(1) // To provide n lines after sentence
    .build()
printables.add(printable)
BluetoothPrinter.printer().print(printables)

Solution 3:[3]

If you have made connection to the devices and paired it.

So for printing, printer wants the byte. SO I have createed a mothod.

Simply call this method and pass the String inside it to get printed.

String str = new String("This is the text sending to the printer");

private void printData() {
    // TODO Auto-generated method stub

    String newline = "\n";
    try {
        out.write(str.getBytes(),0,str.getBytes().length);
        Log.i("Log", "One line printed");
    } catch (IOException e) {
        Toast.makeText(BluetoothDemo.this, "catch 1", Toast.LENGTH_LONG).show();
        e.printStackTrace();
        Log.i("Log", "unable to write ");
        flagCheck = false;
    }
    try {
        out.write(newline.getBytes(),0,newline.getBytes().length);
    } catch (IOException e) {        
        Log.i("Log", "Unable to write the new line::");
        e.printStackTrace();
        flagCheck = false;
    }
    flagCheck = true;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nirav Bhandari
Solution 2 Mazen Rashed
Solution 3 Narendra Pal