Created
December 27, 2017 11:24
-
-
Save cab404/50576f01695aa0f4958030bc3b0a0a40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.todaydelivery.android | |
import android.bluetooth.BluetoothAdapter | |
import android.bluetooth.BluetoothDevice | |
import android.bluetooth.BluetoothSocket | |
import ru.todaydelivery.android.Printer3CottBT.PRINTER_UUID | |
import java.io.Closeable | |
import java.io.OutputStream | |
import java.nio.charset.Charset | |
import java.util.* | |
import java.util.concurrent.Executors | |
/** | |
* Well, sorry for no comments here! | |
* Still you can send me your question to me@cab404.ru! | |
* <p/> | |
* Created at 15:49 on 14/11/17 | |
* | |
* @author cab404 | |
*/ | |
/** you can do ternary like `true Q 1 I 0`*/ | |
infix fun <A> Boolean.Q(t: A?): A? = if (this) t else null | |
infix fun <A> A?.I(t: A): A = this ?: t | |
object Printer3CottBT { | |
val PRINTER_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") | |
val adapter = BluetoothAdapter.getDefaultAdapter() | |
fun start() { | |
} | |
fun devices(): MutableSet<BluetoothDevice> = adapter.bondedDevices | |
fun connect(device: BluetoothDevice): Printer3Cott { | |
return Printer3Cott(device) | |
} | |
} | |
class Printer3Cott(val device: BluetoothDevice) : Closeable { | |
private val queue = Executors.newSingleThreadExecutor() | |
fun Iterable<Int>.barr() = map(Int::toByte).toByteArray() | |
fun OutputStream.write(vararg bytes: Int) = write(bytes.asIterable().barr()) | |
var socket: BluetoothSocket? = null | |
fun reconnect() = device.createRfcommSocketToServiceRecord(PRINTER_UUID).apply { connect() } | |
operator fun invoke(exec: Printer3Cott.() -> Unit) { | |
queue.execute { | |
socket = socket?.let { | |
if (it.isConnected) it else null | |
} ?: reconnect() | |
exec() | |
} | |
} | |
object PrintMode { | |
const val FONT_B = 1 | |
const val BOLD = 8 | |
const val HIGH = 16 | |
const val WIDE = 32 | |
const val UNDERLINE = 128 | |
} | |
object TextAlign { | |
const val LEFT = 0 | |
const val MIDDLE = 1 | |
const val RIGHT = 2 | |
} | |
object CharsetTable { | |
const val PC437 = 0 | |
const val KATAKANA = 1 | |
const val PC850 = 2 | |
const val PC860 = 3 | |
const val PC863 = 4 | |
const val PC865 = 5 | |
const val WEST_EUROPE = 6 | |
const val GREEK = 7 | |
const val HEBREW = 8 | |
const val PC755 = 9 | |
const val IRAN = 10 | |
const val WPC1252 = 16 | |
const val PC866 = 17 | |
const val PC852 = 18 | |
const val PC858 = 19 | |
const val IRAN_II = 20 | |
const val LATVIAN = 21 | |
} | |
fun setCharTable(charset: Int) = | |
socket?.outputStream?.write(27, 116, charset) | |
fun println(text: String, feedLines: Int = 0, charset: String = "IBM866") = | |
socket?.outputStream?.write(text.toByteArray(Charset.forName(charset)) + listOf(27, 100, feedLines).barr()) | |
fun print(text: String, charset: String = "IBM866") = | |
socket?.outputStream?.write(text.toByteArray(Charset.forName(charset)) + listOf(10, 0).barr()) | |
fun setPrintMode(flags: Int) = | |
socket?.outputStream?.write(27, 33, flags) | |
fun setInverted(enabled: Boolean) = | |
socket?.outputStream?.write(27, 123, enabled Q 1 I 0) | |
fun setTextAlign(align: Int) = | |
socket?.outputStream?.write(27, 97, align) | |
fun setBarcodeHeight(pixels: Int) = | |
socket?.outputStream?.write(29, 104, pixels) | |
fun beep(on: Int, off: Int = 0, pin: Int = 0) = | |
socket?.outputStream?.write(27, 112, pin, on, off) | |
fun resetPrinter() = | |
socket?.outputStream?.write(27, 64) | |
fun flush() = | |
socket?.outputStream?.flush() | |
override fun close() { | |
socket?.close() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment