Professional Documents
Culture Documents
Android Studio Print To Bluetooth Printer
Android Studio Print To Bluetooth Printer
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 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);
}
if(pairedDevice.size()>0){
for(BluetoothDevice pairedDev:pairedDevice){
beginListenData();
}
}
void beginListenData(){
try{
}
});
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.
<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" />
</manifest>
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.co
m/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 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;
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);
btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
FindBluetoothDevice();
openBluetoothPrinter();
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){
beginListenData();
}
}
void beginListenData(){
try{
}
});
thread.start();
}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();
}
}