@ruhiverse/thermal-printer-plugin 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +281 -0
- package/ThermalPrinterPlugin.podspec +17 -0
- package/android/build.gradle +59 -0
- package/android/src/main/AndroidManifest.xml +7 -0
- package/android/src/main/java/com/ruhiverse/thermalprinter/AsyncBluetoothEscPosPrint.java +45 -0
- package/android/src/main/java/com/ruhiverse/thermalprinter/AsyncEscPosPrint.java +214 -0
- package/android/src/main/java/com/ruhiverse/thermalprinter/AsyncEscPosPrinter.java +35 -0
- package/android/src/main/java/com/ruhiverse/thermalprinter/AsyncUsbEscPosPrint.java +13 -0
- package/android/src/main/java/com/ruhiverse/thermalprinter/ThermalPrinterPlugin.java +306 -0
- package/dist/docs.json +112 -0
- package/dist/esm/definitions.d.ts +43 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/plugin.cjs.js +10 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +13 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Info.plist +15 -0
- package/ios/Plugin/ThermalPrinterPlugin.m +10 -0
- package/ios/Plugin/ThermalPrinterPlugin.swift +80 -0
- package/package.json +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Ruhiverse
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# @ruhiverse/thermal-printer-plugin
|
|
2
|
+
|
|
3
|
+
Capacitor plugin for thermal printing via USB and Bluetooth connections. Supports ESC/POS formatted text printing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ruhiverse/thermal-printer-plugin
|
|
9
|
+
npx cap sync
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
### Import the plugin
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { ThermalPrinter } from '@ruhiverse/thermal-printer-plugin';
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Print via USB
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { ThermalPrinter } from '@ruhiverse/thermal-printer-plugin';
|
|
24
|
+
|
|
25
|
+
// Print text using USB connection
|
|
26
|
+
await ThermalPrinter.printByUsb({
|
|
27
|
+
textToPrint: '[C]<b>Hello World</b>\n[C]This is a test print',
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Print via Bluetooth
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { ThermalPrinter } from '@ruhiverse/thermal-printer-plugin';
|
|
35
|
+
|
|
36
|
+
// Print text using Bluetooth connection
|
|
37
|
+
await ThermalPrinter.printByBluetooth({
|
|
38
|
+
textToPrint: '[C]<b>Hello World</b>\n[C]This is a test print',
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### ESC/POS Formatting
|
|
43
|
+
|
|
44
|
+
The plugin supports ESC/POS formatting commands. Here are some examples:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
// Center align text
|
|
48
|
+
'[C]Centered Text';
|
|
49
|
+
|
|
50
|
+
// Bold text
|
|
51
|
+
'<b>Bold Text</b>';
|
|
52
|
+
|
|
53
|
+
// Multiple lines
|
|
54
|
+
'Line 1\nLine 2\nLine 3';
|
|
55
|
+
|
|
56
|
+
// Combined formatting
|
|
57
|
+
'[C]<b>Centered Bold Text</b>\n[L]Left aligned text';
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Common formatting tags:
|
|
61
|
+
|
|
62
|
+
- `[C]` - Center align
|
|
63
|
+
- `[L]` - Left align
|
|
64
|
+
- `[R]` - Right align
|
|
65
|
+
- `<b>text</b>` - Bold
|
|
66
|
+
- `<u>text</u>` - Underline
|
|
67
|
+
- `<font size='big'>text</font>` - Big font
|
|
68
|
+
|
|
69
|
+
### Printer Selection
|
|
70
|
+
|
|
71
|
+
You can list available printers and select one by its address (Bluetooth) or name (USB).
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// List Bluetooth printers
|
|
75
|
+
const { printers } = await ThermalPrinter.listBluetoothPrinters();
|
|
76
|
+
console.log('Available Bluetooth printers:', printers);
|
|
77
|
+
|
|
78
|
+
// Print to a specific Bluetooth printer
|
|
79
|
+
await ThermalPrinter.printByBluetooth({
|
|
80
|
+
textToPrint: '[C]Hello from specific printer',
|
|
81
|
+
address: printers[0].address, // MAC address
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// List USB printers
|
|
85
|
+
const { printers: usbPrinters } = await ThermalPrinter.listUsbPrinters();
|
|
86
|
+
console.log('Available USB printers:', usbPrinters);
|
|
87
|
+
|
|
88
|
+
// Print to a specific USB printer
|
|
89
|
+
await ThermalPrinter.printByUsb({
|
|
90
|
+
textToPrint: '[C]Hello from specific USB printer',
|
|
91
|
+
name: usbPrinters[0].name, // Device name
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## API
|
|
96
|
+
|
|
97
|
+
### printByUsb(printObject)
|
|
98
|
+
|
|
99
|
+
Print text using USB connection.
|
|
100
|
+
|
|
101
|
+
| Param | Type | Description |
|
|
102
|
+
| --------------- | ------------- | ----------------------------------- |
|
|
103
|
+
| **printObject** | `PrintObject` | Object containing options for print |
|
|
104
|
+
|
|
105
|
+
**Returns:** `Promise<void>`
|
|
106
|
+
|
|
107
|
+
### printByBluetooth(printObject)
|
|
108
|
+
|
|
109
|
+
Print text using Bluetooth connection.
|
|
110
|
+
|
|
111
|
+
| Param | Type | Description |
|
|
112
|
+
| --------------- | ------------- | ----------------------------------- |
|
|
113
|
+
| **printObject** | `PrintObject` | Object containing options for print |
|
|
114
|
+
|
|
115
|
+
**Returns:** `Promise<void>`
|
|
116
|
+
|
|
117
|
+
### listBluetoothPrinters()
|
|
118
|
+
|
|
119
|
+
Returns a list of paired Bluetooth printers.
|
|
120
|
+
|
|
121
|
+
**Returns:** `Promise<{ printers: { name: string, address: string }[] }>`
|
|
122
|
+
|
|
123
|
+
### listUsbPrinters()
|
|
124
|
+
|
|
125
|
+
Returns a list of connected USB printers.
|
|
126
|
+
|
|
127
|
+
**Returns:** `Promise<{ printers: { name: string, address: string }[] }>`
|
|
128
|
+
|
|
129
|
+
### PrintObject
|
|
130
|
+
|
|
131
|
+
| Prop | Type | Description |
|
|
132
|
+
| --------------- | -------- | ---------------------------------------------------------------- |
|
|
133
|
+
| **textToPrint** | `string` | Text to print. Supports ESC/POS formatting commands. |
|
|
134
|
+
| **address** | `string` | (Optional) Bluetooth MAC address of the printer (for Bluetooth). |
|
|
135
|
+
| **name** | `string` | (Optional) USB device name of the printer (for USB). |
|
|
136
|
+
|
|
137
|
+
## Platform Support
|
|
138
|
+
|
|
139
|
+
- ✅ Android (USB & Bluetooth)
|
|
140
|
+
- ✅ iOS (USB & Bluetooth - basic implementation)
|
|
141
|
+
- ❌ Web (not supported)
|
|
142
|
+
|
|
143
|
+
## Android Setup
|
|
144
|
+
|
|
145
|
+
### Permissions
|
|
146
|
+
|
|
147
|
+
The plugin automatically requests the following permissions:
|
|
148
|
+
|
|
149
|
+
- `BLUETOOTH`
|
|
150
|
+
- `BLUETOOTH_ADMIN`
|
|
151
|
+
- `BLUETOOTH_CONNECT` (Android 12+)
|
|
152
|
+
- `BLUETOOTH_SCAN` (Android 12+)
|
|
153
|
+
- `USB_PERMISSION`
|
|
154
|
+
|
|
155
|
+
### USB Printing
|
|
156
|
+
|
|
157
|
+
1. Connect your thermal printer via USB
|
|
158
|
+
2. Grant USB permission when prompted
|
|
159
|
+
3. The plugin will automatically detect and connect to the printer
|
|
160
|
+
|
|
161
|
+
### Bluetooth Printing
|
|
162
|
+
|
|
163
|
+
1. Pair your thermal printer with the device via Bluetooth settings
|
|
164
|
+
2. The plugin will automatically find and connect to paired printers
|
|
165
|
+
|
|
166
|
+
## iOS Setup
|
|
167
|
+
|
|
168
|
+
### Info.plist Configuration
|
|
169
|
+
|
|
170
|
+
Add the following to your app's `Info.plist`:
|
|
171
|
+
|
|
172
|
+
```xml
|
|
173
|
+
<key>NSBluetoothAlwaysUsageDescription</key>
|
|
174
|
+
<string>This app needs Bluetooth access to connect to thermal printers</string>
|
|
175
|
+
<key>NSBluetoothPeripheralUsageDescription</key>
|
|
176
|
+
<string>This app needs Bluetooth access to connect to thermal printers</string>
|
|
177
|
+
<key>UISupportedExternalAccessoryProtocols</key>
|
|
178
|
+
<array>
|
|
179
|
+
<string>com.epson.escpos</string>
|
|
180
|
+
<string>com.starmicronics.starprnt</string>
|
|
181
|
+
</array>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
**Note:** iOS implementation may require additional setup based on your printer model. USB printing on iOS requires MFi (Made for iPhone) certification.
|
|
185
|
+
|
|
186
|
+
## Local Testing
|
|
187
|
+
|
|
188
|
+
### Testing Locally Before Publishing to npm
|
|
189
|
+
|
|
190
|
+
1. **Link the plugin locally:**
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
cd /Users/apple/Desktop/ionic/workspace/plugin/thermal-printer-plugin
|
|
194
|
+
npm link
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
2. **In your Ionic/Capacitor project (e.g., milkwala project):**
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
cd /path/to/milkwala/project
|
|
201
|
+
npm link @ruhiverse/thermal-printer-plugin
|
|
202
|
+
npx cap sync
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
3. **Build and test:**
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
# For Android
|
|
209
|
+
npx cap run android
|
|
210
|
+
|
|
211
|
+
# For iOS
|
|
212
|
+
npx cap run ios
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Alternative: Using file path
|
|
216
|
+
|
|
217
|
+
You can also install directly from the local path:
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
cd /path/to/milkwala/project
|
|
221
|
+
npm install /Users/apple/Desktop/ionic/workspace/plugin/thermal-printer-plugin
|
|
222
|
+
npx cap sync
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Publishing to npm
|
|
226
|
+
|
|
227
|
+
1. **Login to npm (if not already logged in):**
|
|
228
|
+
|
|
229
|
+
```bash
|
|
230
|
+
npm login
|
|
231
|
+
# Use your ruhiverse npm account credentials
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
2. **Build the plugin:**
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
npm run build
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
3. **Publish to npm:**
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
npm publish --access public
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
4. **After publishing, install in your project:**
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
npm install @ruhiverse/thermal-printer-plugin
|
|
250
|
+
npx cap sync
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Development
|
|
254
|
+
|
|
255
|
+
### Build
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
npm run build
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Lint
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
npm run lint
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### Format
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
npm run fmt
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
## License
|
|
274
|
+
|
|
275
|
+
MIT
|
|
276
|
+
|
|
277
|
+
## Author
|
|
278
|
+
|
|
279
|
+
**Paresh Gami**
|
|
280
|
+
Company: Ruhiverse
|
|
281
|
+
Website: https://ruhiverse.com
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'ThermalPrinterPlugin'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['homepage']
|
|
11
|
+
s.author = package['author']['name']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '13.0'
|
|
15
|
+
s.dependency 'Capacitor'
|
|
16
|
+
s.swift_version = '5.1'
|
|
17
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.4.2'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.3'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.4.0'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
dependencies {
|
|
14
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
apply plugin: 'com.android.library'
|
|
19
|
+
|
|
20
|
+
android {
|
|
21
|
+
namespace "com.ruhiverse.thermalprinter"
|
|
22
|
+
compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
25
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.0.0"
|
|
28
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
29
|
+
}
|
|
30
|
+
buildTypes {
|
|
31
|
+
release {
|
|
32
|
+
minifyEnabled false
|
|
33
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
lintOptions {
|
|
37
|
+
abortOnError false
|
|
38
|
+
}
|
|
39
|
+
compileOptions {
|
|
40
|
+
sourceCompatibility JavaVersion.VERSION_11
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_11
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
repositories {
|
|
46
|
+
google()
|
|
47
|
+
mavenCentral()
|
|
48
|
+
maven { url 'https://jitpack.io' }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
dependencies {
|
|
52
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
53
|
+
implementation project(':capacitor-android')
|
|
54
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
55
|
+
testImplementation "junit:junit:$junitVersion"
|
|
56
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
57
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
58
|
+
implementation 'com.github.DantSu:ESCPOS-ThermalPrinter-Android:3.3.0'
|
|
59
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
2
|
+
<uses-permission android:name="android.permission.BLUETOOTH" />
|
|
3
|
+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
|
|
4
|
+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
|
|
5
|
+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
|
|
6
|
+
<uses-permission android:name="android.permission.USB_PERMISSION" />
|
|
7
|
+
</manifest>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package com.ruhiverse.thermalprinter;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import com.dantsu.escposprinter.connection.DeviceConnection;
|
|
5
|
+
import com.dantsu.escposprinter.connection.bluetooth.BluetoothPrintersConnections;
|
|
6
|
+
import com.dantsu.escposprinter.exceptions.EscPosConnectionException;
|
|
7
|
+
|
|
8
|
+
public class AsyncBluetoothEscPosPrint extends AsyncEscPosPrint {
|
|
9
|
+
public AsyncBluetoothEscPosPrint(Context context) {
|
|
10
|
+
super(context);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public AsyncBluetoothEscPosPrint(Context context, OnPrintFinished onPrintFinished) {
|
|
14
|
+
super(context, onPrintFinished);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
protected PrinterStatus doInBackground(AsyncEscPosPrinter... printersData) {
|
|
18
|
+
if (printersData.length == 0) {
|
|
19
|
+
return new PrinterStatus(null, AsyncEscPosPrint.FINISH_NO_PRINTER);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
AsyncEscPosPrinter printerData = printersData[0];
|
|
23
|
+
DeviceConnection deviceConnection = printerData.getPrinterConnection();
|
|
24
|
+
|
|
25
|
+
this.publishProgress(AsyncEscPosPrint.PROGRESS_CONNECTING);
|
|
26
|
+
|
|
27
|
+
if (deviceConnection == null) {
|
|
28
|
+
printersData[0] = new AsyncEscPosPrinter(
|
|
29
|
+
BluetoothPrintersConnections.selectFirstPaired(),
|
|
30
|
+
printerData.getPrinterDpi(),
|
|
31
|
+
printerData.getPrinterWidthMM(),
|
|
32
|
+
printerData.getPrinterNbrCharactersPerLine()
|
|
33
|
+
);
|
|
34
|
+
printersData[0].setTextsToPrint(printerData.getTextsToPrint());
|
|
35
|
+
} else {
|
|
36
|
+
try {
|
|
37
|
+
deviceConnection.connect();
|
|
38
|
+
} catch (EscPosConnectionException e) {
|
|
39
|
+
e.printStackTrace();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return super.doInBackground(printersData);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
package com.ruhiverse.thermalprinter;
|
|
2
|
+
|
|
3
|
+
import android.app.AlertDialog;
|
|
4
|
+
import android.app.ProgressDialog;
|
|
5
|
+
import android.content.Context;
|
|
6
|
+
import android.os.AsyncTask;
|
|
7
|
+
|
|
8
|
+
import com.dantsu.escposprinter.EscPosCharsetEncoding;
|
|
9
|
+
import com.dantsu.escposprinter.EscPosPrinter;
|
|
10
|
+
import com.dantsu.escposprinter.connection.DeviceConnection;
|
|
11
|
+
import com.dantsu.escposprinter.exceptions.EscPosBarcodeException;
|
|
12
|
+
import com.dantsu.escposprinter.exceptions.EscPosConnectionException;
|
|
13
|
+
import com.dantsu.escposprinter.exceptions.EscPosEncodingException;
|
|
14
|
+
import com.dantsu.escposprinter.exceptions.EscPosParserException;
|
|
15
|
+
|
|
16
|
+
import java.lang.ref.WeakReference;
|
|
17
|
+
|
|
18
|
+
public abstract class AsyncEscPosPrint extends AsyncTask<AsyncEscPosPrinter, Integer, AsyncEscPosPrint.PrinterStatus> {
|
|
19
|
+
public final static int FINISH_SUCCESS = 1;
|
|
20
|
+
public final static int FINISH_NO_PRINTER = 2;
|
|
21
|
+
public final static int FINISH_PRINTER_DISCONNECTED = 3;
|
|
22
|
+
public final static int FINISH_PARSER_ERROR = 4;
|
|
23
|
+
public final static int FINISH_ENCODING_ERROR = 5;
|
|
24
|
+
public final static int FINISH_BARCODE_ERROR = 6;
|
|
25
|
+
|
|
26
|
+
protected final static int PROGRESS_CONNECTING = 1;
|
|
27
|
+
protected final static int PROGRESS_CONNECTED = 2;
|
|
28
|
+
protected final static int PROGRESS_PRINTING = 3;
|
|
29
|
+
protected final static int PROGRESS_PRINTED = 4;
|
|
30
|
+
|
|
31
|
+
protected ProgressDialog dialog;
|
|
32
|
+
protected WeakReference<Context> weakContext;
|
|
33
|
+
protected OnPrintFinished onPrintFinished;
|
|
34
|
+
|
|
35
|
+
public AsyncEscPosPrint(Context context) {
|
|
36
|
+
this(context, null);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public AsyncEscPosPrint(Context context, OnPrintFinished onPrintFinished) {
|
|
40
|
+
this.weakContext = new WeakReference<>(context);
|
|
41
|
+
this.onPrintFinished = onPrintFinished;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected PrinterStatus doInBackground(AsyncEscPosPrinter... printersData) {
|
|
45
|
+
if (printersData.length == 0) {
|
|
46
|
+
return new PrinterStatus(null, AsyncEscPosPrint.FINISH_NO_PRINTER);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.publishProgress(AsyncEscPosPrint.PROGRESS_CONNECTING);
|
|
50
|
+
|
|
51
|
+
AsyncEscPosPrinter printerData = printersData[0];
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
DeviceConnection deviceConnection = printerData.getPrinterConnection();
|
|
55
|
+
|
|
56
|
+
if(deviceConnection == null) {
|
|
57
|
+
return new PrinterStatus(null, AsyncEscPosPrint.FINISH_NO_PRINTER);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
EscPosPrinter printer = new EscPosPrinter(
|
|
61
|
+
deviceConnection,
|
|
62
|
+
printerData.getPrinterDpi(),
|
|
63
|
+
printerData.getPrinterWidthMM(),
|
|
64
|
+
printerData.getPrinterNbrCharactersPerLine(),
|
|
65
|
+
new EscPosCharsetEncoding("windows-1252", 16)
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
this.publishProgress(AsyncEscPosPrint.PROGRESS_PRINTING);
|
|
69
|
+
|
|
70
|
+
String[] textsToPrint = printerData.getTextsToPrint();
|
|
71
|
+
|
|
72
|
+
for(String textToPrint : textsToPrint) {
|
|
73
|
+
printer.printFormattedTextAndCut(textToPrint);
|
|
74
|
+
Thread.sleep(500);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
this.publishProgress(AsyncEscPosPrint.PROGRESS_PRINTED);
|
|
78
|
+
|
|
79
|
+
} catch (EscPosConnectionException e) {
|
|
80
|
+
e.printStackTrace();
|
|
81
|
+
return new PrinterStatus(printerData, AsyncEscPosPrint.FINISH_PRINTER_DISCONNECTED);
|
|
82
|
+
} catch (EscPosParserException e) {
|
|
83
|
+
e.printStackTrace();
|
|
84
|
+
return new PrinterStatus(printerData, AsyncEscPosPrint.FINISH_PARSER_ERROR);
|
|
85
|
+
} catch (EscPosEncodingException e) {
|
|
86
|
+
e.printStackTrace();
|
|
87
|
+
return new PrinterStatus(printerData, AsyncEscPosPrint.FINISH_ENCODING_ERROR);
|
|
88
|
+
} catch (EscPosBarcodeException e) {
|
|
89
|
+
e.printStackTrace();
|
|
90
|
+
return new PrinterStatus(printerData, AsyncEscPosPrint.FINISH_BARCODE_ERROR);
|
|
91
|
+
} catch (InterruptedException e) {
|
|
92
|
+
e.printStackTrace();
|
|
93
|
+
}
|
|
94
|
+
return new PrinterStatus(printerData, AsyncEscPosPrint.FINISH_SUCCESS);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected void onPreExecute() {
|
|
98
|
+
if (this.dialog == null) {
|
|
99
|
+
Context context = weakContext.get();
|
|
100
|
+
|
|
101
|
+
if (context == null) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
this.dialog = new ProgressDialog(context);
|
|
106
|
+
this.dialog.setTitle("Printing in progress...");
|
|
107
|
+
this.dialog.setMessage("...");
|
|
108
|
+
this.dialog.setProgressNumberFormat("%1d / %2d");
|
|
109
|
+
this.dialog.setCancelable(false);
|
|
110
|
+
this.dialog.setIndeterminate(false);
|
|
111
|
+
this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
|
112
|
+
this.dialog.show();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
protected void onProgressUpdate(Integer... progress) {
|
|
117
|
+
switch (progress[0]) {
|
|
118
|
+
case AsyncEscPosPrint.PROGRESS_CONNECTING:
|
|
119
|
+
this.dialog.setMessage("Connecting printer...");
|
|
120
|
+
break;
|
|
121
|
+
case AsyncEscPosPrint.PROGRESS_CONNECTED:
|
|
122
|
+
this.dialog.setMessage("Printer is connected...");
|
|
123
|
+
break;
|
|
124
|
+
case AsyncEscPosPrint.PROGRESS_PRINTING:
|
|
125
|
+
this.dialog.setMessage("Printer is printing...");
|
|
126
|
+
break;
|
|
127
|
+
case AsyncEscPosPrint.PROGRESS_PRINTED:
|
|
128
|
+
this.dialog.setMessage("Printer has finished...");
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
this.dialog.setProgress(progress[0]);
|
|
132
|
+
this.dialog.setMax(4);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
protected void onPostExecute(PrinterStatus result) {
|
|
136
|
+
this.dialog.dismiss();
|
|
137
|
+
this.dialog = null;
|
|
138
|
+
|
|
139
|
+
Context context = weakContext.get();
|
|
140
|
+
|
|
141
|
+
if (context == null) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
switch (result.getPrinterStatus()) {
|
|
146
|
+
case AsyncEscPosPrint.FINISH_SUCCESS:
|
|
147
|
+
new AlertDialog.Builder(context)
|
|
148
|
+
.setTitle("Success")
|
|
149
|
+
.setMessage("Print completed successfully!")
|
|
150
|
+
.show();
|
|
151
|
+
break;
|
|
152
|
+
case AsyncEscPosPrint.FINISH_NO_PRINTER:
|
|
153
|
+
new AlertDialog.Builder(context)
|
|
154
|
+
.setTitle("No printer")
|
|
155
|
+
.setMessage("The application can't find any printer connected.")
|
|
156
|
+
.show();
|
|
157
|
+
break;
|
|
158
|
+
case AsyncEscPosPrint.FINISH_PRINTER_DISCONNECTED:
|
|
159
|
+
new AlertDialog.Builder(context)
|
|
160
|
+
.setTitle("Broken connection")
|
|
161
|
+
.setMessage("Unable to connect the printer.")
|
|
162
|
+
.show();
|
|
163
|
+
break;
|
|
164
|
+
case AsyncEscPosPrint.FINISH_PARSER_ERROR:
|
|
165
|
+
new AlertDialog.Builder(context)
|
|
166
|
+
.setTitle("Invalid formatted text")
|
|
167
|
+
.setMessage("It seems to be an invalid syntax problem.")
|
|
168
|
+
.show();
|
|
169
|
+
break;
|
|
170
|
+
case AsyncEscPosPrint.FINISH_ENCODING_ERROR:
|
|
171
|
+
new AlertDialog.Builder(context)
|
|
172
|
+
.setTitle("Bad selected encoding")
|
|
173
|
+
.setMessage("The selected encoding character returning an error.")
|
|
174
|
+
.show();
|
|
175
|
+
break;
|
|
176
|
+
case AsyncEscPosPrint.FINISH_BARCODE_ERROR:
|
|
177
|
+
new AlertDialog.Builder(context)
|
|
178
|
+
.setTitle("Invalid barcode")
|
|
179
|
+
.setMessage("Data send to be converted to barcode or QR code seems to be invalid.")
|
|
180
|
+
.show();
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
if(this.onPrintFinished != null) {
|
|
184
|
+
if (result.getPrinterStatus() == AsyncEscPosPrint.FINISH_SUCCESS) {
|
|
185
|
+
this.onPrintFinished.onSuccess(result.getAsyncEscPosPrinter());
|
|
186
|
+
} else {
|
|
187
|
+
this.onPrintFinished.onError(result.getAsyncEscPosPrinter(), result.getPrinterStatus());
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public static class PrinterStatus {
|
|
193
|
+
private AsyncEscPosPrinter asyncEscPosPrinter;
|
|
194
|
+
private int printerStatus;
|
|
195
|
+
|
|
196
|
+
public PrinterStatus (AsyncEscPosPrinter asyncEscPosPrinter, int printerStatus) {
|
|
197
|
+
this.asyncEscPosPrinter = asyncEscPosPrinter;
|
|
198
|
+
this.printerStatus = printerStatus;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public AsyncEscPosPrinter getAsyncEscPosPrinter() {
|
|
202
|
+
return asyncEscPosPrinter;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public int getPrinterStatus() {
|
|
206
|
+
return printerStatus;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
public static abstract class OnPrintFinished {
|
|
211
|
+
public abstract void onError(AsyncEscPosPrinter asyncEscPosPrinter, int codeException);
|
|
212
|
+
public abstract void onSuccess(AsyncEscPosPrinter asyncEscPosPrinter);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package com.ruhiverse.thermalprinter;
|
|
2
|
+
|
|
3
|
+
import com.dantsu.escposprinter.EscPosPrinterSize;
|
|
4
|
+
import com.dantsu.escposprinter.connection.DeviceConnection;
|
|
5
|
+
|
|
6
|
+
public class AsyncEscPosPrinter extends EscPosPrinterSize {
|
|
7
|
+
private DeviceConnection printerConnection;
|
|
8
|
+
private String[] textsToPrint = new String[0];
|
|
9
|
+
|
|
10
|
+
public AsyncEscPosPrinter(DeviceConnection printerConnection, int printerDpi, float printerWidthMM, int printerNbrCharactersPerLine) {
|
|
11
|
+
super(printerDpi, printerWidthMM, printerNbrCharactersPerLine);
|
|
12
|
+
this.printerConnection = printerConnection;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public DeviceConnection getPrinterConnection() {
|
|
16
|
+
return this.printerConnection;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public AsyncEscPosPrinter setTextsToPrint(String[] textsToPrint) {
|
|
20
|
+
this.textsToPrint = textsToPrint;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public AsyncEscPosPrinter addTextToPrint(String textToPrint) {
|
|
25
|
+
String[] tmp = new String[this.textsToPrint.length + 1];
|
|
26
|
+
System.arraycopy(this.textsToPrint, 0, tmp, 0, this.textsToPrint.length);
|
|
27
|
+
tmp[this.textsToPrint.length] = textToPrint;
|
|
28
|
+
this.textsToPrint = tmp;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public String[] getTextsToPrint() {
|
|
33
|
+
return this.textsToPrint;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
package com.ruhiverse.thermalprinter;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
|
|
5
|
+
public class AsyncUsbEscPosPrint extends AsyncEscPosPrint {
|
|
6
|
+
public AsyncUsbEscPosPrint(Context context) {
|
|
7
|
+
super(context);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public AsyncUsbEscPosPrint(Context context, OnPrintFinished onPrintFinished) {
|
|
11
|
+
super(context, onPrintFinished);
|
|
12
|
+
}
|
|
13
|
+
}
|