@fedejm/capacitor-esc-pos-printer 0.1.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/CapacitorEscPosPrinter.podspec +17 -0
- package/README.md +203 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +21 -0
- package/android/src/main/java/com/getcapacitor/community/escposprinter/EscPosPrinterPlugin.java +353 -0
- package/android/src/main/java/com/getcapacitor/community/escposprinter/printers/BasePrinter.java +84 -0
- package/android/src/main/java/com/getcapacitor/community/escposprinter/printers/BluetoothLEPrinter.java +14 -0
- package/android/src/main/java/com/getcapacitor/community/escposprinter/printers/BluetoothPrinter.java +80 -0
- package/android/src/main/java/com/getcapacitor/community/escposprinter/printers/NetworkPrinter.java +12 -0
- package/android/src/main/java/com/getcapacitor/community/escposprinter/printers/UsbPrinter.java +14 -0
- package/android/src/main/java/com/getcapacitor/community/escposprinter/printers/constants/PrinterErrorCode.java +8 -0
- package/android/src/main/java/com/getcapacitor/community/escposprinter/printers/exceptions/PrinterException.java +19 -0
- package/dist/docs.json +281 -0
- package/dist/esm/definitions.d.ts +37 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/enums/index.d.ts +2 -0
- package/dist/esm/enums/index.js +3 -0
- package/dist/esm/enums/index.js.map +1 -0
- package/dist/esm/enums/printer-connection-type.d.ts +3 -0
- package/dist/esm/enums/printer-connection-type.js +5 -0
- package/dist/esm/enums/printer-connection-type.js.map +1 -0
- package/dist/esm/enums/printer-error-code.d.ts +6 -0
- package/dist/esm/enums/printer-error-code.js +8 -0
- package/dist/esm/enums/printer-error-code.js.map +1 -0
- package/dist/esm/errors/index.d.ts +1 -0
- package/dist/esm/errors/index.js +2 -0
- package/dist/esm/errors/index.js.map +1 -0
- package/dist/esm/errors/printer-error.d.ts +5 -0
- package/dist/esm/errors/printer-error.js +7 -0
- package/dist/esm/errors/printer-error.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/plugin.d.ts +2 -0
- package/dist/esm/plugin.js +5 -0
- package/dist/esm/plugin.js.map +1 -0
- package/dist/esm/printers/base-printer.d.ts +13 -0
- package/dist/esm/printers/base-printer.js +97 -0
- package/dist/esm/printers/base-printer.js.map +1 -0
- package/dist/esm/printers/bluetooth-printer.d.ts +4 -0
- package/dist/esm/printers/bluetooth-printer.js +12 -0
- package/dist/esm/printers/bluetooth-printer.js.map +1 -0
- package/dist/esm/printers/capacitor-linked-printer.d.ts +9 -0
- package/dist/esm/printers/capacitor-linked-printer.js +30 -0
- package/dist/esm/printers/capacitor-linked-printer.js.map +1 -0
- package/dist/esm/printers/index.d.ts +2 -0
- package/dist/esm/printers/index.js +3 -0
- package/dist/esm/printers/index.js.map +1 -0
- package/dist/esm/web.d.ts +13 -0
- package/dist/esm/web.js +37 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +207 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +210 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +84 -0
|
@@ -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 = 'CapacitorEscPosPrinter'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['repository']['url']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '14.0'
|
|
15
|
+
s.dependency 'Capacitor'
|
|
16
|
+
s.swift_version = '5.1'
|
|
17
|
+
end
|
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# capacitor-esc-pos-printer
|
|
2
|
+
|
|
3
|
+
CapacitorJS wrapper for ESC POS (native) printers.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install capacitor-esc-pos-printer
|
|
9
|
+
npx cap sync
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## API
|
|
13
|
+
|
|
14
|
+
<docgen-index>
|
|
15
|
+
|
|
16
|
+
* [`requestBluetoothEnable()`](#requestbluetoothenable)
|
|
17
|
+
* [`getBluetoothPrinterDevices()`](#getbluetoothprinterdevices)
|
|
18
|
+
* [`createPrinter(...)`](#createprinter)
|
|
19
|
+
* [`disposePrinter(...)`](#disposeprinter)
|
|
20
|
+
* [`isPrinterConnected(...)`](#isprinterconnected)
|
|
21
|
+
* [`connectPrinter(...)`](#connectprinter)
|
|
22
|
+
* [`disconnectPrinter(...)`](#disconnectprinter)
|
|
23
|
+
* [`sendToPrinter(...)`](#sendtoprinter)
|
|
24
|
+
* [`readFromPrinter(...)`](#readfromprinter)
|
|
25
|
+
* [Interfaces](#interfaces)
|
|
26
|
+
* [Enums](#enums)
|
|
27
|
+
|
|
28
|
+
</docgen-index>
|
|
29
|
+
|
|
30
|
+
<docgen-api>
|
|
31
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
32
|
+
|
|
33
|
+
### requestBluetoothEnable()
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
requestBluetoothEnable() => Promise<ValueResult<boolean>>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Returns:** <code>Promise<<a href="#valueresult">ValueResult</a><boolean>></code>
|
|
40
|
+
|
|
41
|
+
--------------------
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### getBluetoothPrinterDevices()
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
getBluetoothPrinterDevices() => Promise<BluetoothDevicesResult>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**Returns:** <code>Promise<<a href="#bluetoothdevicesresult">BluetoothDevicesResult</a>></code>
|
|
51
|
+
|
|
52
|
+
--------------------
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
### createPrinter(...)
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
createPrinter(options: CreatePrinterOptions) => Promise<ValueResult<string>>
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
| Param | Type |
|
|
62
|
+
| ------------- | --------------------------------------------------------------------- |
|
|
63
|
+
| **`options`** | <code><a href="#createprinteroptions">CreatePrinterOptions</a></code> |
|
|
64
|
+
|
|
65
|
+
**Returns:** <code>Promise<<a href="#valueresult">ValueResult</a><string>></code>
|
|
66
|
+
|
|
67
|
+
--------------------
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
### disposePrinter(...)
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
disposePrinter(options: WithHashKey) => Promise<ValueResult<boolean>>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
| Param | Type |
|
|
77
|
+
| ------------- | --------------------------------------------------- |
|
|
78
|
+
| **`options`** | <code><a href="#withhashkey">WithHashKey</a></code> |
|
|
79
|
+
|
|
80
|
+
**Returns:** <code>Promise<<a href="#valueresult">ValueResult</a><boolean>></code>
|
|
81
|
+
|
|
82
|
+
--------------------
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
### isPrinterConnected(...)
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
isPrinterConnected(options: WithHashKey) => Promise<ValueResult<boolean>>
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
| Param | Type |
|
|
92
|
+
| ------------- | --------------------------------------------------- |
|
|
93
|
+
| **`options`** | <code><a href="#withhashkey">WithHashKey</a></code> |
|
|
94
|
+
|
|
95
|
+
**Returns:** <code>Promise<<a href="#valueresult">ValueResult</a><boolean>></code>
|
|
96
|
+
|
|
97
|
+
--------------------
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
### connectPrinter(...)
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
connectPrinter(options: WithHashKey) => Promise<void>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
| Param | Type |
|
|
107
|
+
| ------------- | --------------------------------------------------- |
|
|
108
|
+
| **`options`** | <code><a href="#withhashkey">WithHashKey</a></code> |
|
|
109
|
+
|
|
110
|
+
--------------------
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
### disconnectPrinter(...)
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
disconnectPrinter(options: WithHashKey) => Promise<void>
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
| Param | Type |
|
|
120
|
+
| ------------- | --------------------------------------------------- |
|
|
121
|
+
| **`options`** | <code><a href="#withhashkey">WithHashKey</a></code> |
|
|
122
|
+
|
|
123
|
+
--------------------
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
### sendToPrinter(...)
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
sendToPrinter(options: SendToPrinterOptions) => Promise<void>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
| Param | Type |
|
|
133
|
+
| ------------- | --------------------------------------------------------------------- |
|
|
134
|
+
| **`options`** | <code><a href="#sendtoprinteroptions">SendToPrinterOptions</a></code> |
|
|
135
|
+
|
|
136
|
+
--------------------
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
### readFromPrinter(...)
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
readFromPrinter(options: WithHashKey) => Promise<ValueResult<number[]>>
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
| Param | Type |
|
|
146
|
+
| ------------- | --------------------------------------------------- |
|
|
147
|
+
| **`options`** | <code><a href="#withhashkey">WithHashKey</a></code> |
|
|
148
|
+
|
|
149
|
+
**Returns:** <code>Promise<<a href="#valueresult">ValueResult</a><number[]>></code>
|
|
150
|
+
|
|
151
|
+
--------------------
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
### Interfaces
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
#### ValueResult
|
|
158
|
+
|
|
159
|
+
| Prop | Type |
|
|
160
|
+
| ----------- | -------------- |
|
|
161
|
+
| **`value`** | <code>T</code> |
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
#### BluetoothDevicesResult
|
|
165
|
+
|
|
166
|
+
| Prop | Type |
|
|
167
|
+
| ------------- | ------------------------------------------------------------------------------------------------------------------- |
|
|
168
|
+
| **`devices`** | <code>{ address: string; alias?: string; name: string; bondState: number; type: number; uuids: string[]; }[]</code> |
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
#### CreatePrinterOptions
|
|
172
|
+
|
|
173
|
+
| Prop | Type |
|
|
174
|
+
| -------------------- | ----------------------------------------------------------------------- |
|
|
175
|
+
| **`connectionType`** | <code><a href="#printerconnectiontype">PrinterConnectionType</a></code> |
|
|
176
|
+
| **`address`** | <code>string</code> |
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
#### WithHashKey
|
|
180
|
+
|
|
181
|
+
| Prop | Type |
|
|
182
|
+
| ------------- | ------------------- |
|
|
183
|
+
| **`hashKey`** | <code>string</code> |
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
#### SendToPrinterOptions
|
|
187
|
+
|
|
188
|
+
| Prop | Type |
|
|
189
|
+
| ----------------- | --------------------- |
|
|
190
|
+
| **`data`** | <code>number[]</code> |
|
|
191
|
+
| **`waitingTime`** | <code>number</code> |
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
### Enums
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
#### PrinterConnectionType
|
|
198
|
+
|
|
199
|
+
| Members | Value |
|
|
200
|
+
| --------------- | ------------------------ |
|
|
201
|
+
| **`Bluetooth`** | <code>'bluetooth'</code> |
|
|
202
|
+
|
|
203
|
+
</docgen-api>
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.0'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.2.1'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.6.1'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
dependencies {
|
|
14
|
+
classpath 'com.android.tools.build:gradle:8.7.2'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
apply plugin: 'com.android.library'
|
|
19
|
+
|
|
20
|
+
android {
|
|
21
|
+
namespace "com.getcapacitor.community.escposprinter"
|
|
22
|
+
compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
|
|
25
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 35
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.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_21
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
repositories {
|
|
46
|
+
google()
|
|
47
|
+
mavenCentral()
|
|
48
|
+
}
|
|
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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
|
2
|
+
xmlns:tools="http://schemas.android.com/tools">
|
|
3
|
+
<uses-permission android:name="android.permission.BLUETOOTH"
|
|
4
|
+
android:maxSdkVersion="30" />
|
|
5
|
+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
|
|
6
|
+
android:maxSdkVersion="30" />
|
|
7
|
+
<!-- Uncomment these lines when will use startDiscovery().
|
|
8
|
+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
|
|
9
|
+
android:maxSdkVersion="30" />
|
|
10
|
+
-->
|
|
11
|
+
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
|
|
12
|
+
android:usesPermissionFlags="neverForLocation"
|
|
13
|
+
tools:targetApi="s" />
|
|
14
|
+
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
|
|
15
|
+
tools:targetApi="s" />
|
|
16
|
+
|
|
17
|
+
<uses-feature android:name="android.hardware.bluetooth"
|
|
18
|
+
android:required="false" />
|
|
19
|
+
<uses-feature android:name="android.hardware.bluetooth_le"
|
|
20
|
+
android:required="false" />
|
|
21
|
+
</manifest>
|
package/android/src/main/java/com/getcapacitor/community/escposprinter/EscPosPrinterPlugin.java
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
package com.getcapacitor.community.escposprinter;
|
|
2
|
+
|
|
3
|
+
import android.Manifest;
|
|
4
|
+
import android.annotation.SuppressLint;
|
|
5
|
+
import android.app.Activity;
|
|
6
|
+
import android.bluetooth.BluetoothAdapter;
|
|
7
|
+
import android.bluetooth.BluetoothClass;
|
|
8
|
+
import android.bluetooth.BluetoothManager;
|
|
9
|
+
import android.content.Intent;
|
|
10
|
+
import android.content.pm.PackageManager;
|
|
11
|
+
import android.os.Build;
|
|
12
|
+
|
|
13
|
+
import androidx.activity.result.ActivityResult;
|
|
14
|
+
|
|
15
|
+
import com.getcapacitor.JSArray;
|
|
16
|
+
import com.getcapacitor.JSObject;
|
|
17
|
+
import com.getcapacitor.PermissionState;
|
|
18
|
+
import com.getcapacitor.Plugin;
|
|
19
|
+
import com.getcapacitor.PluginCall;
|
|
20
|
+
import com.getcapacitor.PluginMethod;
|
|
21
|
+
import com.getcapacitor.annotation.ActivityCallback;
|
|
22
|
+
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
23
|
+
import com.getcapacitor.annotation.Permission;
|
|
24
|
+
import com.getcapacitor.annotation.PermissionCallback;
|
|
25
|
+
import com.getcapacitor.community.escposprinter.printers.BasePrinter;
|
|
26
|
+
import com.getcapacitor.community.escposprinter.printers.BluetoothPrinter;
|
|
27
|
+
import com.getcapacitor.community.escposprinter.printers.exceptions.PrinterException;
|
|
28
|
+
|
|
29
|
+
import org.json.JSONException;
|
|
30
|
+
|
|
31
|
+
import java.lang.reflect.InvocationTargetException;
|
|
32
|
+
import java.util.HashMap;
|
|
33
|
+
import java.util.UUID;
|
|
34
|
+
|
|
35
|
+
@SuppressWarnings("unused")
|
|
36
|
+
@CapacitorPlugin(
|
|
37
|
+
name = "EscPosPrinter",
|
|
38
|
+
permissions = {
|
|
39
|
+
/*@Permission(
|
|
40
|
+
alias = "bluetooth-legacy",
|
|
41
|
+
strings = {
|
|
42
|
+
Manifest.permission.BLUETOOTH,
|
|
43
|
+
Manifest.permission.BLUETOOTH_ADMIN
|
|
44
|
+
}
|
|
45
|
+
),*/
|
|
46
|
+
@Permission(
|
|
47
|
+
alias = "bluetooth",
|
|
48
|
+
strings = {
|
|
49
|
+
Manifest.permission.BLUETOOTH_CONNECT,
|
|
50
|
+
Manifest.permission.BLUETOOTH_SCAN
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
public class EscPosPrinterPlugin extends Plugin {
|
|
56
|
+
private BluetoothAdapter bluetoothAdapter;
|
|
57
|
+
private HashMap<String, BasePrinter> printersMap = new HashMap<>();
|
|
58
|
+
|
|
59
|
+
@SuppressWarnings("unused")
|
|
60
|
+
@PluginMethod
|
|
61
|
+
public void requestBluetoothEnable(PluginCall call) {
|
|
62
|
+
if (!assertBluetoothAdapter(call)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (bluetoothAdapter.isEnabled()) {
|
|
66
|
+
var data = new JSObject();
|
|
67
|
+
data.put("value", true);
|
|
68
|
+
call.resolve(data);
|
|
69
|
+
} else {
|
|
70
|
+
if (!assertBluetoothPermission(call)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
var enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
74
|
+
startActivityForResult(call, enableBluetoothIntent, "requestBluetoothEnableResultCallback");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@ActivityCallback
|
|
79
|
+
private void requestBluetoothEnableResultCallback(PluginCall call, ActivityResult result) {
|
|
80
|
+
if (call == null) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
var resultCode = result.getResultCode();
|
|
84
|
+
var data = new JSObject();
|
|
85
|
+
data.put("value", resultCode == Activity.RESULT_OK);
|
|
86
|
+
call.resolve(data);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@SuppressWarnings("unused")
|
|
90
|
+
@SuppressLint("MissingPermission")
|
|
91
|
+
@PluginMethod
|
|
92
|
+
public void getBluetoothPrinterDevices(PluginCall call) {
|
|
93
|
+
if (!assertBluetoothAdapter(call) || !assertBluetoothEnabled(call) || !assertBluetoothPermission(call)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
var devicesArray = new JSArray();
|
|
98
|
+
var bondedDevices = bluetoothAdapter.getBondedDevices();
|
|
99
|
+
if (bondedDevices != null) {
|
|
100
|
+
for (var device : bondedDevices) {
|
|
101
|
+
var bluetoothClass = device.getBluetoothClass();
|
|
102
|
+
var majorClassType = bluetoothClass.getMajorDeviceClass();
|
|
103
|
+
var deviceType = bluetoothClass.getDeviceClass();
|
|
104
|
+
|
|
105
|
+
// From https://inthehand.github.io/html/T_InTheHand_Net_Bluetooth_DeviceClass.htm
|
|
106
|
+
// 1664 - Imaging printer
|
|
107
|
+
var isPrinterDevice = majorClassType == BluetoothClass.Device.Major.IMAGING && (deviceType == 1664 || deviceType == BluetoothClass.Device.Major.IMAGING);
|
|
108
|
+
if (!isPrinterDevice) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
var uuidsArray = new JSArray();
|
|
113
|
+
var servicesUuids = device.getUuids();
|
|
114
|
+
if (servicesUuids != null) {
|
|
115
|
+
for (var uuid : servicesUuids) {
|
|
116
|
+
uuidsArray.put(uuid.toString());
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
var deviceObject = new JSObject();
|
|
121
|
+
devicesArray.put(deviceObject);
|
|
122
|
+
|
|
123
|
+
deviceObject.put("address", device.getAddress());
|
|
124
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
125
|
+
deviceObject.put("alias", device.getAlias());
|
|
126
|
+
}
|
|
127
|
+
deviceObject.put("name", device.getName());
|
|
128
|
+
deviceObject.put("bondState", device.getBondState());
|
|
129
|
+
deviceObject.put("type", device.getType());
|
|
130
|
+
deviceObject.put("uuids", uuidsArray);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
var data = new JSObject();
|
|
135
|
+
data.put("devices", devicesArray);
|
|
136
|
+
call.resolve(data);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@SuppressWarnings("unused")
|
|
140
|
+
@PluginMethod
|
|
141
|
+
public void createPrinter(PluginCall call) {
|
|
142
|
+
var hashKey = UUID.randomUUID().toString();
|
|
143
|
+
var address = call.getString("address");
|
|
144
|
+
var connectionType = call.getString("connectionType", "bluetooth");
|
|
145
|
+
|
|
146
|
+
BasePrinter printer;
|
|
147
|
+
|
|
148
|
+
switch (connectionType) {
|
|
149
|
+
case "bluetooth": {
|
|
150
|
+
if (!assertBluetoothAdapter(call)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// var useLowEnergy = call.getBoolean("le", true) && getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
|
|
155
|
+
// var secure = call.getBoolean("secure", true);
|
|
156
|
+
|
|
157
|
+
printer = new BluetoothPrinter(bluetoothAdapter, address);
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
default: {
|
|
161
|
+
call.reject("Connection type not known.");
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
printersMap.put(hashKey, printer);
|
|
167
|
+
|
|
168
|
+
var data = new JSObject();
|
|
169
|
+
data.put("value", hashKey);
|
|
170
|
+
call.resolve(data);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
@SuppressWarnings("unused")
|
|
174
|
+
@PluginMethod
|
|
175
|
+
public void disposePrinter(PluginCall call) {
|
|
176
|
+
var hashKey = call.getString("hashKey");
|
|
177
|
+
|
|
178
|
+
var hasPrinter = printersMap.containsKey(hashKey);
|
|
179
|
+
if (hasPrinter) {
|
|
180
|
+
printersMap.remove(hashKey);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
var data = new JSObject();
|
|
184
|
+
data.put("value", hasPrinter);
|
|
185
|
+
call.resolve(data);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
@SuppressWarnings("unused")
|
|
189
|
+
@PluginMethod
|
|
190
|
+
public void isPrinterConnected(PluginCall call) {
|
|
191
|
+
var printer = getGuardedPrinterByHash(call);
|
|
192
|
+
if (printer == null) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
var data = new JSObject();
|
|
197
|
+
data.put("value", printer.isConnected());
|
|
198
|
+
call.resolve(data);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
@SuppressWarnings("unused")
|
|
202
|
+
@PluginMethod
|
|
203
|
+
public void connectPrinter(PluginCall call) {
|
|
204
|
+
var printer = getGuardedPrinterByHash(call);
|
|
205
|
+
if (printer == null) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
if (printer instanceof BluetoothPrinter && !assertBluetoothEnabled(call) || !assertBluetoothPermission(call)) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
printer.connect();
|
|
215
|
+
|
|
216
|
+
call.resolve();
|
|
217
|
+
} catch (PrinterException e) {
|
|
218
|
+
rejectWithPrinterException(call, e);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
@SuppressWarnings("unused")
|
|
223
|
+
@PluginMethod
|
|
224
|
+
public void disconnectPrinter(PluginCall call) {
|
|
225
|
+
var printer = getGuardedPrinterByHash(call);
|
|
226
|
+
if (printer == null) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
printer.disconnect();
|
|
231
|
+
|
|
232
|
+
call.resolve();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
@SuppressWarnings("unused")
|
|
236
|
+
@PluginMethod
|
|
237
|
+
public void sendToPrinter(PluginCall call) {
|
|
238
|
+
var printer = getGuardedPrinterByHash(call);
|
|
239
|
+
if (printer == null) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
var waitingTime = call.getInt("waitingTime", 0);
|
|
244
|
+
var data = call.getArray("data");
|
|
245
|
+
|
|
246
|
+
byte[] bytesArray = new byte[data.length()];
|
|
247
|
+
for (var i = 0; i < bytesArray.length; i++) {
|
|
248
|
+
bytesArray[i] = (byte)data.optInt(i);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
printer.send(bytesArray, waitingTime);
|
|
253
|
+
|
|
254
|
+
call.resolve();
|
|
255
|
+
} catch (PrinterException e) {
|
|
256
|
+
rejectWithPrinterException(call, e);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
@SuppressWarnings("unused")
|
|
261
|
+
@PluginMethod
|
|
262
|
+
public void readFromPrinter(PluginCall call) {
|
|
263
|
+
var printer = getGuardedPrinterByHash(call);
|
|
264
|
+
if (printer == null) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// TODO: read like https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/io/InputStream.java
|
|
269
|
+
|
|
270
|
+
try {
|
|
271
|
+
var bytes = printer.read();
|
|
272
|
+
var bytesArray = new JSArray();
|
|
273
|
+
for (var i = 0; i < bytes.length; i++) {
|
|
274
|
+
try {
|
|
275
|
+
bytesArray.put(i, bytes[i]);
|
|
276
|
+
} catch (JSONException e) {
|
|
277
|
+
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
var data = new JSObject();
|
|
282
|
+
data.put("value", bytesArray);
|
|
283
|
+
call.resolve(data);
|
|
284
|
+
} catch (PrinterException e) {
|
|
285
|
+
rejectWithPrinterException(call, e);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private boolean assertBluetoothAdapter(PluginCall call) {
|
|
290
|
+
if (bluetoothAdapter == null) {
|
|
291
|
+
var bluetoothAvailable = getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);
|
|
292
|
+
if (bluetoothAvailable) {
|
|
293
|
+
var bluetoothManager = getContext().getSystemService(BluetoothManager.class);
|
|
294
|
+
bluetoothAdapter = bluetoothManager.getAdapter();
|
|
295
|
+
}
|
|
296
|
+
if (bluetoothAdapter == null) {
|
|
297
|
+
call.reject("Bluetooth is not available.");
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private boolean assertBluetoothEnabled(PluginCall call) {
|
|
305
|
+
if (!bluetoothAdapter.isEnabled()) {
|
|
306
|
+
call.reject("Bluetooth is not enabled.");
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private boolean assertBluetoothPermission(PluginCall call) {
|
|
313
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && getPermissionState("bluetooth") != PermissionState.GRANTED) {
|
|
314
|
+
requestPermissionForAlias("bluetooth", call, "bluetoothPermissionCallback");
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
@PermissionCallback
|
|
321
|
+
private void bluetoothPermissionCallback(PluginCall call) {
|
|
322
|
+
if (call == null) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (getPermissionState("bluetooth") == PermissionState.GRANTED) {
|
|
326
|
+
var methodName = call.getMethodName();
|
|
327
|
+
try {
|
|
328
|
+
var method = this.getClass().getMethod(methodName, PluginCall.class);
|
|
329
|
+
method.invoke(this, call);
|
|
330
|
+
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
|
331
|
+
|
|
332
|
+
}
|
|
333
|
+
} else {
|
|
334
|
+
call.reject("Bluetooth not granted.");
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
private BasePrinter getGuardedPrinterByHash(PluginCall call) {
|
|
339
|
+
var hashKey = call.getString("hashKey");
|
|
340
|
+
var printer = printersMap.get(hashKey);
|
|
341
|
+
if (printer == null) {
|
|
342
|
+
call.reject("Printer with " + hashKey + " hash not found.");
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
return printer;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
private void rejectWithPrinterException(PluginCall call, PrinterException e) {
|
|
349
|
+
var data = new JSObject();
|
|
350
|
+
data.put("code", e.getErrorCode());
|
|
351
|
+
call.reject(e.getMessage(), data);
|
|
352
|
+
}
|
|
353
|
+
}
|