In this example we are going to create a simple Android application to print texts to a Bluetooth thermal printer. We’ll be using the
Android library for ESC/POS Thermal Printer
to develop this case.

We begin by creating an Android project with an Empty Action. Later on the project is created we need to edit the
app/build.gradle
to add together the required dependencies and the repository from which it will exist downloaded.

            ... ...  dependencies {     ...     ...     implementation 'com.github.dantsu:escpos-thermalprinter-android:two.0.11' }  allprojects {     repositories {         maven { url 'https://jitpack.io' }     } }
            
          

Next, add the permission to access Bluetooth in the
AndriodManifest.xml
file.

            <?xml version="1.0" encoding="utf-viii"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"           bundle="org.kodejava.android">      <uses-permission android:name="android.permission.BLUETOOTH" />      ...     ... </manifest>
            
          

Let’due south now bound to the lawmaking snippet that will actually print our store receipt to the printer. The steps are quite simple.

Later added the
uses-permission
in the
AndroidManigest.xml
we also need to check permission in the application, you’ll do it like this.

            if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, MainActivity.PERMISSION_BLUETOOTH); }
            
          

Open up the connection to Bluetooth printer by calling the
selectFirstPaired()
method of the
BluetoothPrintersConnections
class. This will give u.s. an example of
BluetoothConnection. If the connection is expert we create an example of
EscPosPrinter
by passing some parameters like the
connection, printer dpi, width in millimeter and the printer’south number of character per line.

            BluetoothConnection connexion = BluetoothPrintersConnections.selectFirstPaired(); EscPosPrinter printer = new EscPosPrinter(connection, 203, 48f, 32);
            
          

The next pace is to prepare the text to exist printed and called the
printFormattedText()
of the
printer
object and laissez passer the text to be printed.

            String text = "[C]Hello Globe!\northward"; printer.printFormattedText(text);
            
          

Here is the full lawmaking snippet for our application.

            package org.kodejava.android;  import android.Manifest; import android.content.pm.PackageManager; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.core.app.ActivityCompat; import androidx.cadre.content.ContextCompat; import com.dantsu.escposprinter.EscPosPrinter; import com.dantsu.escposprinter.connection.bluetooth.BluetoothConnection; import com.dantsu.escposprinter.connexion.bluetooth.BluetoothPrintersConnections; import com.dantsu.escposprinter.textparser.PrinterTextParserImg;  import java.text.DateFormat; import java.text.NumberFormat; import coffee.text.SimpleDateFormat; import java.util.Appointment; import java.util.Locale;  public class MainActivity extends AppCompatActivity {     public static final int PERMISSION_BLUETOOTH = 1;      private final Locale locale = new Locale("id", "ID");     private final DateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a", locale);     private last NumberFormat nf = NumberFormat.getCurrencyInstance(locale);      @Override     protected void onCreate(Package savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }      public void doPrint(View view) {         attempt {             if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, MainActivity.PERMISSION_BLUETOOTH);             } else {                 BluetoothConnection connectedness = BluetoothPrintersConnections.selectFirstPaired();                 if (connexion != nada) {                     EscPosPrinter printer = new EscPosPrinter(connection, 203, 48f, 32);                     final Cord text = "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer,                             this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo,                                     DisplayMetrics.DENSITY_LOW, getTheme())) + "</img>\n" +                             "[Fifty]\due north" +                             "[L]" + df.format(new Appointment()) + "\n" +                             "[C]================================\north" +                             "[L]<b>Effective Coffee</b>\north" +                             "[L]    ane pcs[R]" + nf.format(25000) + "\due north" +                             "[L]<b>Headfirst Android Development</b>\n" +                             "[L]    1 pcs[R]" + nf.format(45000) + "\n" +                             "[L]<b>The Martian</b>\n" +                             "[L]    1 pcs[R]" + nf.format(20000) + "\due north" +                             "[C]--------------------------------\northward" +                             "[L]Full[R]" + nf.format(90000) + "\n" +                             "[50]DISCOUNT 15%[R]" + nf.format(13500) + "\n" +                             "[50]TAX x%[R]" + nf.format(7650) + "\n" +                             "[L]<b>Chiliad Full[R]" + nf.format(84150) + "</b>\due north" +                             "[C]--------------------------------\n" +                             "[C]<barcode blazon='ean13' height='10'>202105160005</barcode>\n" +                             "[C]--------------------------------\n" +                             "[C]Cheers For Shopping\n" +                             "[C]https://kodejava.org\due north" +                             "[L]\northward" +                             "[L]<qrcode>https://kodejava.org</qrcode>\north";                      printer.printFormattedText(text);                 } else {                     Toast.makeText(this, "No printer was connected!", Toast.LENGTH_SHORT).show();                 }             }         } grab (Exception east) {             Log.e("APP", "Can't impress", e);         }     } }
            
          

The following prototype is the upshot of our code snippet printed on 48 mm thermal printer.

You can notice the complete source code in the following repository
Android Bluetooth Thermal Printer Case. For more than information on formatted text syntax guideline you can visit the
projection documentation website.