Skip to content

Instantly share code, notes, and snippets.

@six519
Created June 8, 2024 02:56
Show Gist options
  • Save six519/14f59f2fe5c602e93707d2febb111737 to your computer and use it in GitHub Desktop.
Save six519/14f59f2fe5c602e93707d2febb111737 to your computer and use it in GitHub Desktop.
Print Text On Bluetooth Thermal Printer Using Kotlin
package com.ferdinandsilva.printertest2
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothSocket
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.ferdinandsilva.printertest2.ui.theme.PrinterTest2Theme
import java.io.IOException
import java.util.UUID
object PrinterConstants {
const val PRINTER_ADDRESS = "60:6E:41:62:92:F8"
const val PRINTER_SERVICE = "00001101-0000-1000-8000-00805f9b34fb"
}
var btAdapter: BluetoothAdapter? = null
var btDevice: BluetoothDevice? = null
var btSocket: BluetoothSocket? = null
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
btAdapter = bluetoothManager.adapter
btDevice = btAdapter?.getRemoteDevice(PrinterConstants.PRINTER_ADDRESS)
if (btAdapter == null) {
Toast.makeText(applicationContext, "Bluetooth not available", Toast.LENGTH_LONG).show()
} else {
if (btAdapter!!.isEnabled) {
val printerThread = PrinterThread(btDevice!!)
printerThread.start()
}
}
setContent {
PrinterTest2Theme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
PrintButton()
}
}
}
}
@SuppressLint("MissingPermission")
private class PrinterThread(private val device: BluetoothDevice) : Thread() {
private var thisSocket: BluetoothSocket? = null
init {
var tmp: BluetoothSocket? = null
try {
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(PrinterConstants.PRINTER_SERVICE))
} catch (e: IOException) {
// Handle IOException if needed
}
thisSocket = tmp
}
override fun run() {
btAdapter?.cancelDiscovery()
try {
thisSocket?.connect()
} catch (e: IOException) {
return
}
btSocket = thisSocket!!
}
}
}
@Composable
fun PrintButton() {
Column(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
OutlinedButton(onClick = {
if (btSocket != null) {
try {
val out = btSocket?.outputStream
out?.write(byteArrayOf(27, 97, 1))
out?.write("This is a test print...\n".toByteArray())
} catch (e: IOException) {
}
}
}) {
Text("Test Print")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment