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

We brainstorm by creating an Android projection with an Empty Activeness. After the project is created we demand to edit the
app/build.gradle
to add the required dependencies and the repository from which information technology will be downloaded.

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

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

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

Let’s at present jump to the lawmaking snippet that volition actually print our shop receipt to the printer. The steps are quite elementary.

Afterwards added the
uses-permission
in the
AndroidManigest.xml
we also demand to check permission in the application, y’all’ll exercise 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
grade. This volition give u.s. an instance of
BluetoothConnection. If the connection is good we create an instance of
EscPosPrinter
by passing some parameters like the
connection, printer dpi, width in millimeter and the printer’southward number of graphic symbol per line.

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

The adjacent step is to prepare the text to be printed and called the
printFormattedText()
of the
printer
object and laissez passer the text to be printed.

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

Here is the full code 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.core.content.ContextCompat; import com.dantsu.escposprinter.EscPosPrinter; import com.dantsu.escposprinter.connection.bluetooth.BluetoothConnection; import com.dantsu.escposprinter.connection.bluetooth.BluetoothPrintersConnections; import com.dantsu.escposprinter.textparser.PrinterTextParserImg;  import java.text.DateFormat; import java.text.NumberFormat; import coffee.text.SimpleDateFormat; import java.util.Date; import coffee.util.Locale;  public class MainActivity extends AppCompatActivity {     public static final int PERMISSION_BLUETOOTH = 1;      private terminal Locale locale = new Locale("id", "ID");     private final DateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a", locale);     private terminal NumberFormat nf = NumberFormat.getCurrencyInstance(locale);      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }      public void doPrint(View view) {         endeavour {             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 (connection != nada) {                     EscPosPrinter printer = new EscPosPrinter(connexion, 203, 48f, 32);                     last Cord text = "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer,                             this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo,                                     DisplayMetrics.DENSITY_LOW, getTheme())) + "</img>\n" +                             "[L]\north" +                             "[L]" + df.format(new Date()) + "\n" +                             "[C]================================\n" +                             "[L]<b>Effective Java</b>\n" +                             "[L]    1 pcs[R]" + nf.format(25000) + "\n" +                             "[L]<b>Headfirst Android Development</b>\n" +                             "[Fifty]    1 pcs[R]" + nf.format(45000) + "\n" +                             "[L]<b>The Martian</b>\northward" +                             "[Fifty]    1 pcs[R]" + nf.format(20000) + "\due north" +                             "[C]--------------------------------\due north" +                             "[Fifty]Total[R]" + nf.format(90000) + "\north" +                             "[L]Disbelieve 15%[R]" + nf.format(13500) + "\n" +                             "[L]Revenue enhancement 10%[R]" + nf.format(7650) + "\n" +                             "[L]<b>Thou TOTAL[R]" + nf.format(84150) + "</b>\n" +                             "[C]--------------------------------\north" +                             "[C]<barcode type='ean13' meridian='x'>202105160005</barcode>\n" +                             "[C]--------------------------------\n" +                             "[C]Thanks For Shopping\n" +                             "[C]https://kodejava.org\n" +                             "[L]\n" +                             "[L]<qrcode>https://kodejava.org</qrcode>\n";                      printer.printFormattedText(text);                 } else {                     Toast.makeText(this, "No printer was connected!", Toast.LENGTH_SHORT).prove();                 }             }         } grab (Exception e) {             Log.east("APP", "Tin't print", e);         }     } }
            
          

The post-obit paradigm is the upshot of our lawmaking snippet printed on 48 mm thermal printer.

You can discover the consummate source code in the following repository
Android Bluetooth Thermal Printer Example. For more information on formatted text syntax guideline you can visit the
projection documentation website.