|
1 |
| -# Universal-Bluetooth-Printer |
| 1 | +# Universal Bluetooth Printer |
| 2 | +[](https://jitpack.io/#mazenrashed/Universal-Bluetooth-Printer) |
| 3 | +UBP aim is to provide a simple abstraction for use the bluetooth printers regardless of its brand. |
| 4 | + |
| 5 | +### Add the JitPack repository to your build file |
| 6 | +```groovy |
| 7 | +allprojects { |
| 8 | + repositories { |
| 9 | + ... |
| 10 | + maven { url 'https://jitpack.io' } |
| 11 | + } |
| 12 | +} |
| 13 | +``` |
| 14 | +### Add dependency |
| 15 | +```groovy |
| 16 | +dependencies { |
| 17 | + implementation 'com.github.mazenrashed:Universal-Bluetooth-Printer:1.0.0' |
| 18 | +} |
| 19 | +``` |
| 20 | +### Add persessions to manifest |
| 21 | +```groovy |
| 22 | +<uses-permission android:name="android.permission.BLUETOOTH" /> |
| 23 | +<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> |
| 24 | +``` |
| 25 | +### Initialize UBP |
| 26 | +Should be initialized once in `Application.onCreate()`: |
| 27 | +```kotlin |
| 28 | +UBP.init(context); |
| 29 | +``` |
| 30 | +### Scan and pair printer |
| 31 | +UBP is providing a scanning activity to make pairing process easy. Just start `ScanningActivity` and you will skip the process of pairing and saving printer. |
| 32 | +```kotlin |
| 33 | +startActivityForResult(Intent(this, ScanningActivity::class.java), ScanningActivity.SCANNING_FOR_PRINTER) |
| 34 | +``` |
| 35 | +When the printer is being ready: |
| 36 | +```kotlin |
| 37 | +override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
| 38 | + super.onActivityResult(requestCode, resultCode, data) |
| 39 | + if (requestCode == ScanningActivity.SCANNING_FOR_PRINTER && resultCode == Activity.RESULT_OK) |
| 40 | + //Printer is ready now |
| 41 | +} |
| 42 | +``` |
| 43 | +If you want to make your own user interface, you can pass your paired printer to UBP like this: |
| 44 | +```kotlin |
| 45 | +UBP.setPrinter(printerName, printerAddress) |
| 46 | +``` |
| 47 | +Check if UBP has saved printer: |
| 48 | +```kotlin |
| 49 | +UBP.hasPairedPrinter() |
| 50 | +``` |
| 51 | +To get current saved printer: |
| 52 | +```kotlin |
| 53 | +UBP.getPairedPrinter() |
| 54 | +``` |
| 55 | +To remove current saved printer: |
| 56 | +```kotlin |
| 57 | +UBP.removeCurrentPrinter() |
| 58 | +``` |
| 59 | +### Printing |
| 60 | +UBP provide a simple builder to design your paper. |
| 61 | +To print `Hello World` simply, write this code: |
| 62 | +```kotlin |
| 63 | +var printables = ArrayList<Printable>() |
| 64 | +var printable = Printable.PrintableBuilder() |
| 65 | + .setText("Hello World") |
| 66 | +printables.add(printable) |
| 67 | +BluetoothPrinter.printer(this).print(printables) |
| 68 | +``` |
| 69 | +Use all builder responsibilities: |
| 70 | +```kotlin |
| 71 | +var printables = ArrayList<Printable>() |
| 72 | +var printable = Printable.PrintableBuilder() |
| 73 | + .setText("Hello World") //The text you want to print |
| 74 | + .setAlignment(DefaultPrinter.ALLIGMENT_CENTER) |
| 75 | + .setEmphasizedMode(DefaultPrinter.EMPHASISED_MODE_BOLD) //Bold or normal |
| 76 | + .setFontSize(DefaultPrinter.FONT_SIZE_NORMAL) |
| 77 | + .setUnderlined(DefaultPrinter.UNDELINED_MODE_ON) // Underline on/off |
| 78 | + .setCharacterCode(DefaultPrinter.CHARACTER_CODE_USA_CP437) // Character code to support languages |
| 79 | + .setLineSpacing(DefaultPrinter.LINE_SPACING_60) |
| 80 | + .setNewLinesAfter(1) // To provide n lines after sentence |
| 81 | + .build() |
| 82 | +printables.add(printable) |
| 83 | +BluetoothPrinter.printer(this).print(printables) |
| 84 | +``` |
| 85 | +### Use more than printer in the same time: |
| 86 | +```kotlin |
| 87 | +var printer1 = PairedPrinter(name, address) |
| 88 | +var printer2 = PairedPrinter(name, address) |
| 89 | +BluetoothPrinter.printer(printer1, this).print(printables) |
| 90 | +BluetoothPrinter.printer(printer2, this).print(printables) |
| 91 | +``` |
| 92 | +### If you have a printer with deferent commands |
| 93 | + |
| 94 | +Create a class from type `Printer` and override the initializers method, then return your printer commands from the printers command sheet ( You can find it on the Internet ), lets take an example: |
| 95 | + ```kotlin |
| 96 | + open class SomePrinter : Printer() { |
| 97 | + |
| 98 | + override fun initLineSpacingCommand(): ByteArray = byteArrayOf(0x1B, 0x33) |
| 99 | + |
| 100 | + override fun initInitPrinterCommand(): ByteArray = byteArrayOf(0x1b, 0x40) |
| 101 | + |
| 102 | + override fun initJustificationCommand(): ByteArray = byteArrayOf(27, 97) |
| 103 | + |
| 104 | + override fun initFontSizeCommand(): ByteArray = byteArrayOf(29, 33) |
| 105 | + |
| 106 | + override fun initEmphasizedModeCommand(): ByteArray = byteArrayOf(27, 69) |
| 107 | + |
| 108 | + override fun initUnderlineModeCommand(): ByteArray = byteArrayOf(27, 45) |
| 109 | + |
| 110 | + override fun initCharacterCodeCommand(): ByteArray = byteArrayOf(27, 116) |
| 111 | + |
| 112 | + override fun initFeedLineCommand(): ByteArray = byteArrayOf(27, 100) |
| 113 | +} |
| 114 | +``` |
| 115 | +Then pass your printer class to UBP: |
| 116 | +```kotlin |
| 117 | +BluetoothPrinter.printer(SomePrinter(), this).print(printables) |
| 118 | +``` |
| 119 | + |
| 120 | +### Proguard config |
| 121 | +```` |
| 122 | +-keep class * implements java.io.Serializable { *; } |
| 123 | +```` |
0 commit comments