ESCPOS-ThermalPrinter-Android
ESCPOS-ThermalPrinter-Android copied to clipboard
Can't print with android pos with built-in printer
please help me sir, i tried to use your library escpos, but when I trying print using a device android with built-in printer always fails, i'm using Android Studio (kotlin), but if I use external thermal printer it work, no issue with that
my device : GC088 details about product : https://en.szgoodchip.com/products/list_4954780.html
this is my function
fun doPrint() {
try {
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.BLUETOOTH
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
context as Activity,
arrayOf(Manifest.permission.BLUETOOTH),
PERMISSION_BLUETOOTH
)
} else {
val bluetoothDevicesList = BluetoothConnections().list
if (bluetoothDevicesList != null) {
for (bluetoothConnection in bluetoothDevicesList) {
val device = bluetoothConnection.device
Log.i("BLUETOOTH DEVICE", device.name)
selectedDevice=bluetoothConnection.connect()
val deviceName = device.name
val majorDeviceClass = device.bluetoothClass.majorDeviceClass
val deviceClass = device.bluetoothClass.deviceClass
Log.e("BluetoothDevice", "Name: $deviceName") // the result is : Inner printer
Log.e("BluetoothDevice", "Major Device Class: $majorDeviceClass") // the result is : 1536
Log.e("BluetoothDevice", "Device Class: $deviceClass") // the result is : 1536
}
}
if (selectedDevice != null) {
val printer = EscPosPrinter(selectedDevice, 203, 48f, 32)
printer.printFormattedText(receipt)
printer.disconnectPrinter()
} else {
Toast.makeText(context, "Printer Not Found!", Toast.LENGTH_SHORT).show()
}
}
} catch (e: java.lang.Exception) {
Toast.makeText(context, "An unexpected error occurred: ${e.message}",Toast.LENGTH_SHORT).show()
// the error message is : An unexpected error occurred: flush is called on null OutputStream
}
}
this is my BluetoothPrintersConnections.java
package com.dantsu.escposprinter.connection.bluetooth;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.util.Log;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.dantsu.escposprinter.exceptions.EscPosConnectionException;
public class BluetoothPrintersConnections extends BluetoothConnections {
/**
* Easy way to get the first bluetooth printer paired / connected.
*
* @return a EscPosPrinterCommands instance
*/
@Nullable
public static BluetoothConnection selectFirstPaired() {
BluetoothPrintersConnections printers = new BluetoothPrintersConnections();
BluetoothConnection[] bluetoothPrinters = printers.getList();
if (bluetoothPrinters != null && bluetoothPrinters.length > 0) {
for (BluetoothConnection printer : bluetoothPrinters) {
try {
return printer.connect();
} catch (EscPosConnectionException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* Get a list of bluetooth printers.
*
* @return an array of EscPosPrinterCommands
*/
@SuppressLint("MissingPermission")
@Nullable
public BluetoothConnection[] getList() {
BluetoothConnection[] bluetoothDevicesList = super.getList();
if (bluetoothDevicesList == null) {
return null;
}
int i = 0;
BluetoothConnection[] printersTmp = new BluetoothConnection[bluetoothDevicesList.length];
for (BluetoothConnection bluetoothConnection : bluetoothDevicesList) {
BluetoothDevice device = bluetoothConnection.getDevice();
printersTmp[i++] = new BluetoothConnection(device);
// default code
// if (majDeviceCl == BluetoothClass.Device.Major.IMAGING && (deviceCl == 1536 || deviceCl == BluetoothClass.Device.Major.IMAGING)) {
// printersTmp[i++] = new BluetoothConnection(device);
// }
int majDeviceCl = device.getBluetoothClass().getMajorDeviceClass(),deviceCl = device.getBluetoothClass().getDeviceClass();
// ricki code
if ((majDeviceCl == BluetoothClass.Device.Major.IMAGING && (deviceCl == 1536 || deviceCl == BluetoothClass.Device.Major.IMAGING)) || device.getName().equals("Inner printer")) {
printersTmp[i++] = new BluetoothConnection(device);
}
// if(TextUtils.equals(device.getName().toUpperCase(), "INNER PRINTER")) {
// printersTmp[i++] = new BluetoothConnection(device);
// } else {
// int majDeviceCl = device.getBluetoothClass().getMajorDeviceClass(),
// deviceCl = device.getBluetoothClass().getDeviceClass();
// if(majDeviceCl == BluetoothClass.Device.Major.IMAGING && (deviceCl == 1536 || deviceCl == BluetoothClass.Device.Major.IMAGING)) {
// printersTmp[i++] = new BluetoothConnection(device);
// }
// }
}
BluetoothConnection[] bluetoothPrinters = new BluetoothConnection[i];
System.arraycopy(printersTmp, 0, bluetoothPrinters, 0, i);
return bluetoothPrinters;
}
}
You need to first enable location, then Bluetooth connection can follow, Bluetooth won't work when location is off. I'm not seeing anywhere in your code, checking location permission. After that, the checking Bluetooth permission can follow. Turn on Bluetooth connection on your device, start the discovery process, once printer is listed it can be connected to..
Maybe just in general, don't use this device. It looks like it runs on Android 5 Lollipop from 2015. For the updates, it's easier to use Table/Mobile + external Printer.