Android – Send Print Command to Bluetooth Printer from Mobile
Bluetooth Adapter
Represents the local device Bluetooth adapter. The BluetoothAdapter lets you perform fundamental Bluetooth tasks, such as initiate device discovery, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for connection requests from other devices, and start a scan for Bluetooth LE devices.
To get a BluetoothAdapter representing the local Bluetooth adapter, when running on JELLY_BEAN_MR1 and below, call the static getDefaultAdapter() method; when running on JELLY_BEAN_MR2 and higher, call getAdapter(). Fundamentally, this is your starting point for all Bluetooth actions. Once you have the local adapter, you can get a set of BluetoothDevice objects representing all paired devices with getBondedDevices(); start device discovery with startDiscovery(); or create a BluetoothServerSocket to listen for incoming connection requests with listenUsingRfcommWithServiceRecord(String, UUID); or start a scan for Bluetooth LE devices with startLeScan(LeScanCallback).
In this tutorial we will learn how to connect Bluetooth printer with android mobile app and send a print command successfully. Below example shows you how to implement this.
Step by Step Tutorial
Step 1: Open Android Studio.
Step 2: Create a new project as follows:
If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under ‘Quick Start’ on the right of the dialog.
Otherwise, click File in the Android Studio menu bar, then New, New Project.
Step 3: Enter your app name, company domain, and project location, as prompted. Then click Next.
Step 4: Select the form factors you need for your app. If you’re not sure what you need, just select Phone and Tablet. Then click Next.
Step 5: Select Empty Activity in the ‘Add an activity to Mobile’ dialog. Then click Next.
Step 6: Enter the activity name, layout name and title as prompted. The default values are fine. Then click Finish.
After that wait for few seconds. Android Studio starts Gradle and builds of your project.
Step 7: Open Android Manifest .xml file. and write below mentioned code to get permission to access Bluetooth adapter.
<uses-permission android:name="android.permission.BLUETOOTH"/>
Step 8: Add some Text view (label), Edit text (text box) and Buttons in page activity layout. see following code:-
<LinearLayout android:layout_width="368dp" android:layout_height="495dp" android:orientation="vertical" tools:layout_editor_absoluteY="8dp" tools:layout_editor_absoluteX="8dp"> <TextView android:id="@+id/lbl" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Attach Printer" android:padding="5dp"/> <TextView android:id="@+id/lblPrinterName" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Printer Name"/> <EditText android:id="@+id/txtText" android:layout_width="match_parent" android:padding="5dp" android:layout_height="150dp" /> <Button android:id="@+id/btnConnect" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Connect Printer"/> <Button android:id="@+id/btnDisconnect" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Disconnect Printer"/> <Button android:id="@+id/btnPrint" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Print Document"/> </LinearLayout>
Step 9: Write code to find Bluetooth adapter in Main Activity Java file.
void FindBluetoothDevice(){ try{ bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(bluetoothAdapter==null){ lblPrinterName.setText("No Bluetooth Adapter found"); } if(bluetoothAdapter.isEnabled()){ Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBT,0); } Set<BluetoothDevice> pairedDevice = bluetoothAdapter.getBondedDevices(); if(pairedDevice.size()>0){ for(BluetoothDevice pairedDev:pairedDevice){ // My Bluetoth printer name is BTP_F09F1A if(pairedDev.getName().equals("BTP_F09F1A")){ bluetoothDevice=pairedDev; lblPrinterName.setText("Bluetooth Printer Attached: "+pairedDev.getName()); break; } } } lblPrinterName.setText("Bluetooth Printer Attached"); }catch(Exception ex){ ex.printStackTrace(); } }
Step 10: Write code to open Bluetooth device or printer.
void openBluetoothPrinter() throws IOException{ try{ //Standard uuid from string // UUID uuidSting = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); bluetoothSocket=bluetoothDevice.createRfcommSocketToServiceRecord(uuidSting); bluetoothSocket.connect(); outputStream=bluetoothSocket.getOutputStream(); inputStream=bluetoothSocket.getInputStream(); beginListenData(); }catch (Exception ex){ } } void beginListenData(){ try{ final Handler handler =new Handler(); final byte delimiter=10; stopWorker =false; readBufferPosition=0; readBuffer = new byte[1024]; thread=new Thread(new Runnable() { @Override public void run() { while (!Thread.currentThread().isInterrupted() && !stopWorker){ try{ int byteAvailable = inputStream.available(); if(byteAvailable>0){ byte[] packetByte = new byte[byteAvailable]; inputStream.read(packetByte); for(int i=0; i<byteAvailable; i++){ byte b = packetByte[i]; if(b==delimiter){ byte[] encodedByte = new byte[readBufferPosition]; System.arraycopy( readBuffer,0, encodedByte,0, encodedByte.length ); final String data = new String(encodedByte,"US-ASCII"); readBufferPosition=0; handler.post(new Runnable() { @Override public void run() { lblPrinterName.setText(data); } }); }else{ readBuffer[readBufferPosition++]=b; } } } }catch(Exception ex){ stopWorker=true; } } } }); thread.start(); }catch (Exception ex){ ex.printStackTrace(); } }
Step 10: At last send command to print text to Bluetooth Printer and disconnect Bluetooth device
void printData() throws IOException{ try{ String msg = textBox.getText().toString(); msg+="\n"; outputStream.write(msg.getBytes()); lblPrinterName.setText("Printing Text..."); }catch (Exception ex){ ex.printStackTrace(); } } // Disconnect Printer // void disconnectBT() throws IOException{ try { stopWorker=true; outputStream.close(); inputStream.close(); bluetoothSocket.close(); lblPrinterName.setText("Printer Disconnected."); }catch (Exception ex){ ex.printStackTrace(); } }
Step 10: Now run your code and you will get output.
Full Source Code
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.suraj.bluetoothprint"> <uses-permission android:name="android.permission.BLUETOOTH"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.suraj.bluetoothprint.MainActivity"> <LinearLayout android:layout_width="368dp" android:layout_height="495dp" android:orientation="vertical" tools:layout_editor_absoluteY="8dp" tools:layout_editor_absoluteX="8dp"> <TextView android:id="@+id/lbl" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Attach Printer" android:padding="5dp"/> <TextView android:id="@+id/lblPrinterName" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Printer Name"/> <EditText android:id="@+id/txtText" android:layout_width="match_parent" android:padding="5dp" android:layout_height="150dp" /> <Button android:id="@+id/btnConnect" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Connect Printer"/> <Button android:id="@+id/btnDisconnect" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Disconnect Printer"/> <Button android:id="@+id/btnPrint" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="Print Document"/> </LinearLayout> </android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.suraj.bluetoothprint; import android.content.Intent; import android.support.annotation.WorkerThread; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; // Import neccessor namespace import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Set; import java.util.UUID; import android.os.Handler; import java.util.concurrent.RunnableFuture; import java.util.logging.LogRecord; public class MainActivity extends AppCompatActivity { BluetoothAdapter bluetoothAdapter; BluetoothSocket bluetoothSocket; BluetoothDevice bluetoothDevice; OutputStream outputStream; InputStream inputStream; Thread thread; byte[] readBuffer; int readBufferPosition; volatile boolean stopWorker; TextView lblPrinterName; EditText textBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create object of controls Button btnConnect = (Button) findViewById(R.id.btnConnect); Button btnDisconnect = (Button) findViewById(R.id.btnDisconnect); Button btnPrint = (Button) findViewById(R.id.btnPrint); textBox = (EditText) findViewById(R.id.txtText); lblPrinterName = (TextView) findViewById(R.id.lblPrinterName); btnConnect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try{ FindBluetoothDevice(); openBluetoothPrinter(); }catch (Exception ex){ ex.printStackTrace(); } } }); btnDisconnect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try{ disconnectBT(); }catch (Exception ex){ ex.printStackTrace(); } } }); btnPrint.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try{ printData(); }catch (Exception ex){ ex.printStackTrace(); } } }); } void FindBluetoothDevice(){ try{ bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if(bluetoothAdapter==null){ lblPrinterName.setText("No Bluetooth Adapter found"); } if(bluetoothAdapter.isEnabled()){ Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBT,0); } Set<BluetoothDevice> pairedDevice = bluetoothAdapter.getBondedDevices(); if(pairedDevice.size()>0){ for(BluetoothDevice pairedDev:pairedDevice){ // My Bluetoth printer name is BTP_F09F1A if(pairedDev.getName().equals("BTP_F09F1A")){ bluetoothDevice=pairedDev; lblPrinterName.setText("Bluetooth Printer Attached: "+pairedDev.getName()); break; } } } lblPrinterName.setText("Bluetooth Printer Attached"); }catch(Exception ex){ ex.printStackTrace(); } } // Open Bluetooth Printer void openBluetoothPrinter() throws IOException{ try{ //Standard uuid from string // UUID uuidSting = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); bluetoothSocket=bluetoothDevice.createRfcommSocketToServiceRecord(uuidSting); bluetoothSocket.connect(); outputStream=bluetoothSocket.getOutputStream(); inputStream=bluetoothSocket.getInputStream(); beginListenData(); }catch (Exception ex){ } } void beginListenData(){ try{ final Handler handler =new Handler(); final byte delimiter=10; stopWorker =false; readBufferPosition=0; readBuffer = new byte[1024]; thread=new Thread(new Runnable() { @Override public void run() { while (!Thread.currentThread().isInterrupted() && !stopWorker){ try{ int byteAvailable = inputStream.available(); if(byteAvailable>0){ byte[] packetByte = new byte[byteAvailable]; inputStream.read(packetByte); for(int i=0; i<byteAvailable; i++){ byte b = packetByte[i]; if(b==delimiter){ byte[] encodedByte = new byte[readBufferPosition]; System.arraycopy( readBuffer,0, encodedByte,0, encodedByte.length ); final String data = new String(encodedByte,"US-ASCII"); readBufferPosition=0; handler.post(new Runnable() { @Override public void run() { lblPrinterName.setText(data); } }); }else{ readBuffer[readBufferPosition++]=b; } } } }catch(Exception ex){ stopWorker=true; } } } }); thread.start(); }catch (Exception ex){ ex.printStackTrace(); } } // Printing Text to Bluetooth Printer // void printData() throws IOException{ try{ String msg = textBox.getText().toString(); msg+="\n"; outputStream.write(msg.getBytes()); lblPrinterName.setText("Printing Text..."); }catch (Exception ex){ ex.printStackTrace(); } } // Disconnect Printer // void disconnectBT() throws IOException{ try { stopWorker=true; outputStream.close(); inputStream.close(); bluetoothSocket.close(); lblPrinterName.setText("Printer Disconnected."); }catch (Exception ex){ ex.printStackTrace(); } } }
3 thoughts on “Android – Send Print Command to Bluetooth Printer from Mobile”
hallo sir, I want to print some text with image which is reside on my android phone to Bluetooth printer but text is successfully printed and image is not printed on paper. Can you help me?
printer type Zjiang zj-5802
Thank you for wonderful tutorial .
Is the printing image is same as this. where we convert image to byte array and write to the outputstream ?