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

Nosotros begin by creating an Android project with an Empty Activity. Later on the project is created nosotros need to edit the
app/build.gradle
to add the required dependencies and the repository from which it will be downloaded.

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

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

            <?xml version="ane.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 store receipt to the printer. The steps are quite simple.

After added the
uses-permission
in the
AndroidManigest.xml
nosotros too demand to check permission in the awarding, you’ll practice it like this.

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

Open up the connection to Bluetooth printer past calling the
selectFirstPaired()
method of the
BluetoothPrintersConnections
grade. This will requite usa an instance of
BluetoothConnection. If the connectedness is skilful nosotros create an instance of
EscPosPrinter
past passing some parameters like the
connection, printer dpi, width in millimeter and the printer’s number of grapheme per line.

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

The next step is to prepare the text to be printed and chosen the
printFormattedText()
of the
printer
object and pass the text to be printed.

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

Here is the full code snippet for our application.

            bundle 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 coffee.text.NumberFormat; import coffee.text.SimpleDateFormat; import java.util.Date; import coffee.util.Locale;  public class MainActivity extends AppCompatActivity {     public static terminal int PERMISSION_BLUETOOTH = one;      private final Locale locale = new Locale("id", "ID");     private final DateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a", locale);     private concluding NumberFormat nf = NumberFormat.getCurrencyInstance(locale);      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }      public void doPrint(View view) {         try {             if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {                 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, MainActivity.PERMISSION_BLUETOOTH);             } else {                 BluetoothConnection connection = BluetoothPrintersConnections.selectFirstPaired();                 if (connection != null) {                     EscPosPrinter printer = new EscPosPrinter(connection, 203, 48f, 32);                     last String text = "[C]<img>" + PrinterTextParserImg.bitmapToHexadecimalString(printer,                             this.getApplicationContext().getResources().getDrawableForDensity(R.drawable.logo,                                     DisplayMetrics.DENSITY_LOW, getTheme())) + "</img>\n" +                             "[50]\n" +                             "[Fifty]" + df.format(new Date()) + "\n" +                             "[C]================================\n" +                             "[L]<b>Effective Java</b>\n" +                             "[50]    i pcs[R]" + nf.format(25000) + "\due north" +                             "[L]<b>Headfirst Android Evolution</b>\n" +                             "[L]    1 pcs[R]" + nf.format(45000) + "\due north" +                             "[L]<b>The Martian</b>\n" +                             "[L]    1 pcs[R]" + nf.format(20000) + "\n" +                             "[C]--------------------------------\northward" +                             "[L]Total[R]" + nf.format(90000) + "\north" +                             "[L]DISCOUNT 15%[R]" + nf.format(13500) + "\n" +                             "[Fifty]Taxation x%[R]" + nf.format(7650) + "\n" +                             "[L]<b>M Total[R]" + nf.format(84150) + "</b>\n" +                             "[C]--------------------------------\northward" +                             "[C]<barcode blazon='ean13' height='10'>202105160005</barcode>\n" +                             "[C]--------------------------------\due north" +                             "[C]Thank you For Shopping\north" +                             "[C]https://kodejava.org\n" +                             "[Fifty]\n" +                             "[Fifty]<qrcode>https://kodejava.org</qrcode>\n";                      printer.printFormattedText(text);                 } else {                     Toast.makeText(this, "No printer was connected!", Toast.LENGTH_SHORT).evidence();                 }             }         } grab (Exception e) {             Log.e("APP", "Tin can't impress", eastward);         }     } }
            
          

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

You lot can find the complete source code in the post-obit repository
Android Bluetooth Thermal Printer Example. For more information on formatted text syntax guideline you lot can visit the
project documentation website.