Reputation: 11
I am trying to update an existing app through integrating it in the handheld mobile POS machine which will help the app to print the bill details in hardcopy format. Till now the app was sharing the bill details as a softcopy (PDF and Excel) but now the clients have demanded our organization to get the bill in the hardcopy format.
I made a Kotlin class file for the app to get connect to the POS printer through Bluetooth.
import android.Manifest
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.core.app.ActivityCompat
import com.spoonerpt.rest_services.WebUtils
import java.io.OutputStream
import java.util.UUID
class BluetoothConnect(
private val context: Context,
private val activity: Activity
) {
private val bluetoothAdapter: BluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
private lateinit var bluetoothSocket: BluetoothSocket
private lateinit var outputStream: OutputStream
private val PERMISSIONS = arrayOf(
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN
)
private fun requestPermissions() {
ActivityCompat.requestPermissions(activity, PERMISSIONS, WebUtils.REQUEST_PERMISSION_BLUETOOTH)
}
fun checkPermissionsAtRuntime(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return true
} else {
for (permission in PERMISSIONS) {
if (ActivityCompat.checkSelfPermission(context, permission)
!= PackageManager.PERMISSION_GRANTED
) {
return false
}
}
}
return true
}
fun connect() {
val device: BluetoothDevice = bluetoothAdapter.getRemoteDevice("00:11:22:33:44:55") // Replace with your printer's MAC address
try {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.BLUETOOTH_CONNECT
) != PackageManager.PERMISSION_GRANTED
) {
// Permissions not granted, request permissions
requestPermissions()
return
}
// Fetch UUIDs if not available
if (device.uuids == null) {
device.fetchUuidsWithSdp()
device.uuids?.let {
val uuid: UUID = it[0].uuid
bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid)
bluetoothSocket.connect()
outputStream = bluetoothSocket.outputStream
} ?: run {
Log.e("PrintManager", "UUIDs not found")
}
} else {
val uuid: UUID = device.uuids[0].uuid
bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid)
bluetoothSocket.connect()
outputStream = bluetoothSocket.outputStream
}
} catch (e: Exception) {
Log.e("PrintManager", "Error while connecting to Bluetooth device", e)
}
}
fun printText(text: String) {
outputStream.write(text.toByteArray())
}
fun disconnect() {
outputStream.close()
bluetoothSocket.close()
}
}
And this is the code for implementing this class file in my activity .
// this is initialized above oncreate method.
private lateinit var billInfo : StringBuilder
// billInfo is used to enter the details from the server.
//this generateBill function is the structure for the bill (Bill Format).
fun generateBill(){
val billDetails = StringBuilder()
billDetails.append("Saras Bill Details\n")
billDetails.append(billInfo)
printBill(billDetails.toString())
}
//printBill is the function where we connect to the print roll of the handheld mobile machine.
fun printBill(billDetails:String){
printermanager = BluetoothConnect(this,this)
if(printermanager.checkPermissionsAtRuntime()){
printermanager.connect()
printermanager.printText(billDetails)
printermanager.disconnect()}
else{
printermanager.requestPermissions()
}
}
//(this,this) is the context and activity reference for the Bluetooth Connect class.
// here is the code on how the billInfo variable is collecting the data from the server..
rcdfAssismentresponse =
dataItem.rcdfAssismentitem as java.util.ArrayList<RCDFAssismentItem>
billInfo.append("\n${dataItem.rcdfAssismentitem.get(0).ItemType}\n")
billInfo.append("\n${dataItem.rcdfAssismentitem.get(0).UnitCode}\n")
billInfo.append("\n${dataItem.rcdfAssismentitem.get(0).QtyLTR}\n")
billInfo.append("\n${dataItem.rcdfAssismentitem.get(0).Rate}\n")
The app is still not connecting to the POS machine and also, how do I customize the bill format?
I know that customization or editing the bill format is done through the printText() function in my BluetoothConnect class but I am not able to understand how to do it. Is it something related to ECS/POS commands that the printer understands?
Upvotes: 1
Views: 60