Coder Social home page Coder Social logo

martinselva / thermalprinter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from isradeleon/thermalprinter

0.0 0.0 0.0 420 KB

Android library that simplifies communication with thermal printers via Bluetooth

License: MIT License

Java 19.11% Kotlin 80.89%

thermalprinter's Introduction

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:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Finally add the dependency to your app build.gradle:

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

Usage

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

override fun onCreate(savedInstanceState: Bundle?) {
    ...

    startActivityForResult(
        Intent(this, ConnectBluetoothActivity::class.java),
        ConnectBluetoothActivity.CONNECT_BLUETOOTH
    )
}

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

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if(resultCode == Activity.RESULT_OK && requestCode == ConnectBluetoothActivity.CONNECT_BLUETOOTH){
        // ThermalPrinter is ready
        ThermalPrinter.instance
            .write("Hello world", PrintAlignment.CENTER, PrintFont.LARGE)
            .fillLineWith('-')
            .write("Let's eat","some tacos")
            .write("Price","0.5 USD")
            .writeImage(BitmapFactory.decodeResource(getResources(), R.drawable.panda))
            .print()
    }
}

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.

thermalprinter's People

Contributors

isradeleon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.