How to Connect your activity with the POS Printer

I am a fresher , and I am currently trying to update an existing app of my organization through integrating it in the handheld mobile POS machine which will help the app to print the bill details in hardcopy format although till now the app was sharing the bill details as a softcopy (PDF and Excel Format) 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 Detailsn")
        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?

New contributor

Sati Anshul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật