项目作者: isradeleon

项目描述 :
Android library that simplifies communication with thermal printers via Bluetooth
高级语言: Kotlin
项目地址: git://github.com/isradeleon/ThermalPrinter.git
创建时间: 2019-11-21T18:27:16Z
项目社区:https://github.com/isradeleon/ThermalPrinter

开源协议:MIT License

下载


ThermalPrinter

Release
Languages
Languages
Support

Android library for communication with thermal printers via Bluetooth.
This library is only compatible with androidx.



Installation

Add jitpack.io to your root build.gradle:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Finally add the dependency to your app build.gradle:

  1. dependencies {
  2. implementation "com.github.isradeleon:ThermalPrinter:1.0.3"
  3. }

Usage

This library includes the code for the bluetooth connection. All you need to do is first to
start the ConnectBluetoothActivity:

  1. override fun onCreate(savedInstanceState: Bundle?) {
  2. ...
  3. startActivityForResult(
  4. Intent(this, ConnectBluetoothActivity::class.java),
  5. ConnectBluetoothActivity.CONNECT_BLUETOOTH
  6. )
  7. }

You’ll get Activity.RESULT_OK if the device was successfully connected via bluetooth:

  1. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  2. super.onActivityResult(requestCode, resultCode, data)
  3. if(resultCode == Activity.RESULT_OK && requestCode == ConnectBluetoothActivity.CONNECT_BLUETOOTH){
  4. // ThermalPrinter is ready
  5. ThermalPrinter.instance
  6. .write("Hello world", PrintAlignment.CENTER, PrintFont.LARGE)
  7. .fillLineWith('-')
  8. .write("Let's eat","some tacos")
  9. .write("Price","0.5 USD")
  10. .writeImage(BitmapFactory.decodeResource(getResources(), R.drawable.panda))
  11. .print()
  12. }
  13. }

If you want to set or modify the BluetoothSocket by yourself, you can do it with the BluetoothConnection class.
ThermalPrinter uses this socket to send all its commands to the printer.

ThermalPrinter methods

Method Description
print() Prints all the written content
write(text: String) Writes text to thermal printer
write(text: String, alignment: PrintAlignment) Writes text and sets the text alignment
write(text: String, alignment: PrintAlignment, font: PrintFont) Writes text, sets text alignment and font size
write(key: String, value: String) Writes a line with key value pair
write(key: String, value: String, separator: Char) Writes a line with key value pair and fills space with separator
writeImage(bitmap: Bitmap) Writes an image to thermal printer. Image must be black and white format
newLine() Writes a new line to thermal printer
fillLineWith(char: Char) Writes a line filled with the specified char

Result



Credits

This library uses Thermal Printer in Android
code for image printing.

License

This library is licensed under MIT license. View license.