Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 957
Nirav Bhandari Avatar asked Jan 01 '13 14:01

Nirav Bhandari


People also ask

How do I print from my Android via Bluetooth?

Connecting the Printer to Your Android DeviceGo to Settings > Wireless & Network and turn on Bluetooth. Wait while your device scans for the printer. The printer is displayed when your device finds it. Tap the printer.

Can you print wirelessly from an Android phone?

Open the Settings app on your Android phone and type “Printing” in the search box. Tap on the Printing option that shows up. Alternatively, you can find this setting in Settings > Connected devices > Connection preferences > Printing. Tap on Cloud Print.

Can I Bluetooth My phone to printer?

Connecting via Bluetooth First, turn on your printer's Bluetooth scanning function. Each brand (and even model) does this a little differently, so check the instruction manual. Once it's scanning, go to your phone's settings, tap Connected devices and select Pair new device.


2 Answers

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.

like image 52
Nirav Bhandari Avatar answered Sep 27 '22 23:09

Nirav Bhandari


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)
like image 25
Mazen Rashed Avatar answered Sep 27 '22 22:09

Mazen Rashed