@autorunify/capacitor-bleprinter 0.0.1

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.
Files changed (32) hide show
  1. package/AutorunifyCapacitorBleprinter.podspec +17 -0
  2. package/Package.swift +28 -0
  3. package/README.md +342 -0
  4. package/android/build.gradle +67 -0
  5. package/android/libs/LPAPI-2024-10-21-R.jar +0 -0
  6. package/android/src/main/AndroidManifest.xml +21 -0
  7. package/android/src/main/java/com/autorunify/capacitor/bleprinter/AsyncManager.kt +118 -0
  8. package/android/src/main/java/com/autorunify/capacitor/bleprinter/BleDevice.kt +45 -0
  9. package/android/src/main/java/com/autorunify/capacitor/bleprinter/BleManager.kt +214 -0
  10. package/android/src/main/java/com/autorunify/capacitor/bleprinter/BlePrinter.kt +29 -0
  11. package/android/src/main/java/com/autorunify/capacitor/bleprinter/BlePrinterPlugin.kt +272 -0
  12. package/android/src/main/java/com/autorunify/capacitor/bleprinter/DotHanTechPrinter.kt +120 -0
  13. package/android/src/main/java/com/autorunify/capacitor/bleprinter/FormatManager.kt +13 -0
  14. package/android/src/main/res/.gitkeep +0 -0
  15. package/dist/docs.json +1320 -0
  16. package/dist/esm/definitions.d.ts +56 -0
  17. package/dist/esm/definitions.js +2 -0
  18. package/dist/esm/definitions.js.map +1 -0
  19. package/dist/esm/index.d.ts +4 -0
  20. package/dist/esm/index.js +7 -0
  21. package/dist/esm/index.js.map +1 -0
  22. package/dist/esm/web.d.ts +14 -0
  23. package/dist/esm/web.js +14 -0
  24. package/dist/esm/web.js.map +1 -0
  25. package/dist/plugin.cjs.js +28 -0
  26. package/dist/plugin.cjs.js.map +1 -0
  27. package/dist/plugin.js +31 -0
  28. package/dist/plugin.js.map +1 -0
  29. package/ios/Sources/BlePrinterPlugin/BlePrinter.swift +8 -0
  30. package/ios/Sources/BlePrinterPlugin/BlePrinterPlugin.swift +23 -0
  31. package/ios/Tests/BlePrinterPluginTests/BlePrinterTests.swift +15 -0
  32. package/package.json +81 -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 = 'AutorunifyCapacitorBleprinter'
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 = '15.0'
15
+ s.dependency 'Capacitor'
16
+ s.swift_version = '5.1'
17
+ end
package/Package.swift ADDED
@@ -0,0 +1,28 @@
1
+ // swift-tools-version: 5.9
2
+ import PackageDescription
3
+
4
+ let package = Package(
5
+ name: "AutorunifyCapacitorBleprinter",
6
+ platforms: [.iOS(.v15)],
7
+ products: [
8
+ .library(
9
+ name: "AutorunifyCapacitorBleprinter",
10
+ targets: ["BlePrinterPlugin"])
11
+ ],
12
+ dependencies: [
13
+ .package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0")
14
+ ],
15
+ targets: [
16
+ .target(
17
+ name: "BlePrinterPlugin",
18
+ dependencies: [
19
+ .product(name: "Capacitor", package: "capacitor-swift-pm"),
20
+ .product(name: "Cordova", package: "capacitor-swift-pm")
21
+ ],
22
+ path: "ios/Sources/BlePrinterPlugin"),
23
+ .testTarget(
24
+ name: "BlePrinterPluginTests",
25
+ dependencies: ["BlePrinterPlugin"],
26
+ path: "ios/Tests/BlePrinterPluginTests")
27
+ ]
28
+ )
package/README.md ADDED
@@ -0,0 +1,342 @@
1
+ # @autorunify/capacitor-bleprinter
2
+
3
+ ble printer plugin
4
+
5
+ ## Install
6
+
7
+ To use npm
8
+
9
+ ```bash
10
+ npm install @autorunify/capacitor-bleprinter
11
+ ````
12
+
13
+ To use yarn
14
+
15
+ ```bash
16
+ yarn add @autorunify/capacitor-bleprinter
17
+ ```
18
+
19
+ Sync native files
20
+
21
+ ```bash
22
+ npx cap sync
23
+ ```
24
+
25
+ ## API
26
+
27
+ <docgen-index>
28
+
29
+ * [`checkPermissions()`](#checkpermissions)
30
+ * [`requestPermissions()`](#requestpermissions)
31
+ * [`isBluetoothEnabled()`](#isbluetoothenabled)
32
+ * [`init(...)`](#init)
33
+ * [`kill()`](#kill)
34
+ * [`devices(...)`](#devices)
35
+ * [`isConnected()`](#isconnected)
36
+ * [`connect(...)`](#connect)
37
+ * [`disconnect(...)`](#disconnect)
38
+ * [`printImage(...)`](#printimage)
39
+ * [`addListener('state', ...)`](#addlistenerstate-)
40
+ * [Interfaces](#interfaces)
41
+ * [Type Aliases](#type-aliases)
42
+
43
+ </docgen-index>
44
+
45
+ <docgen-api>
46
+ <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
47
+
48
+ ### checkPermissions()
49
+
50
+ ```typescript
51
+ checkPermissions() => Promise<Permissions>
52
+ ```
53
+
54
+ **Returns:** <code>Promise&lt;<a href="#permissions">Permissions</a>&gt;</code>
55
+
56
+ --------------------
57
+
58
+
59
+ ### requestPermissions()
60
+
61
+ ```typescript
62
+ requestPermissions() => Promise<Permissions>
63
+ ```
64
+
65
+ **Returns:** <code>Promise&lt;<a href="#permissions">Permissions</a>&gt;</code>
66
+
67
+ --------------------
68
+
69
+
70
+ ### isBluetoothEnabled()
71
+
72
+ ```typescript
73
+ isBluetoothEnabled() => Promise<BluetoothState>
74
+ ```
75
+
76
+ **Returns:** <code>Promise&lt;<a href="#bluetoothstate">BluetoothState</a>&gt;</code>
77
+
78
+ --------------------
79
+
80
+
81
+ ### init(...)
82
+
83
+ ```typescript
84
+ init(options: InitOptions) => Promise<void>
85
+ ```
86
+
87
+ | Param | Type |
88
+ | ------------- | --------------------------------------------------- |
89
+ | **`options`** | <code><a href="#initoptions">InitOptions</a></code> |
90
+
91
+ --------------------
92
+
93
+
94
+ ### kill()
95
+
96
+ ```typescript
97
+ kill() => Promise<void>
98
+ ```
99
+
100
+ --------------------
101
+
102
+
103
+ ### devices(...)
104
+
105
+ ```typescript
106
+ devices(options?: ScanOptions | undefined) => Promise<ScanResults>
107
+ ```
108
+
109
+ | Param | Type |
110
+ | ------------- | --------------------------------------------------- |
111
+ | **`options`** | <code><a href="#scanoptions">ScanOptions</a></code> |
112
+
113
+ **Returns:** <code>Promise&lt;<a href="#scanresults">ScanResults</a>&gt;</code>
114
+
115
+ --------------------
116
+
117
+
118
+ ### isConnected()
119
+
120
+ ```typescript
121
+ isConnected() => Promise<ConnectionState>
122
+ ```
123
+
124
+ **Returns:** <code>Promise&lt;<a href="#connectionstate">ConnectionState</a>&gt;</code>
125
+
126
+ --------------------
127
+
128
+
129
+ ### connect(...)
130
+
131
+ ```typescript
132
+ connect(options: ConnectOptions) => Promise<void>
133
+ ```
134
+
135
+ | Param | Type |
136
+ | ------------- | --------------------------------------------------------- |
137
+ | **`options`** | <code><a href="#connectoptions">ConnectOptions</a></code> |
138
+
139
+ --------------------
140
+
141
+
142
+ ### disconnect(...)
143
+
144
+ ```typescript
145
+ disconnect(options?: TimeoutOptions | undefined) => Promise<void>
146
+ ```
147
+
148
+ | Param | Type |
149
+ | ------------- | --------------------------------------------------------- |
150
+ | **`options`** | <code><a href="#timeoutoptions">TimeoutOptions</a></code> |
151
+
152
+ --------------------
153
+
154
+
155
+ ### printImage(...)
156
+
157
+ ```typescript
158
+ printImage(options: PrintImageOptions) => Promise<PrintImageResults>
159
+ ```
160
+
161
+ | Param | Type |
162
+ | ------------- | --------------------------------------------------------------- |
163
+ | **`options`** | <code><a href="#printimageoptions">PrintImageOptions</a></code> |
164
+
165
+ **Returns:** <code>Promise&lt;<a href="#printimageresults">PrintImageResults</a>&gt;</code>
166
+
167
+ --------------------
168
+
169
+
170
+ ### addListener('state', ...)
171
+
172
+ ```typescript
173
+ addListener(event: 'state', callback: (e: OnStateChangeEvent) => void) => Promise<PluginListenerHandle>
174
+ ```
175
+
176
+ | Param | Type |
177
+ | -------------- | --------------------------------------------------------------------------------- |
178
+ | **`event`** | <code>'state'</code> |
179
+ | **`callback`** | <code>(e: <a href="#onstatechangeevent">OnStateChangeEvent</a>) =&gt; void</code> |
180
+
181
+ **Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt;</code>
182
+
183
+ --------------------
184
+
185
+
186
+ ### Interfaces
187
+
188
+
189
+ #### BluetoothState
190
+
191
+ | Prop | Type |
192
+ | ------------- | -------------------- |
193
+ | **`enabled`** | <code>boolean</code> |
194
+
195
+
196
+ #### InitOptions
197
+
198
+ | Prop | Type |
199
+ | ------------------ | ------------------------- |
200
+ | **`manufacturer`** | <code>'DotHanTech'</code> |
201
+
202
+
203
+ #### ScanResults
204
+
205
+ | Prop | Type |
206
+ | ------------- | --------------------------------------------------------------------------------- |
207
+ | **`devices`** | <code><a href="#array">Array</a>&lt;<a href="#bledevice">BleDevice</a>&gt;</code> |
208
+
209
+
210
+ #### Array
211
+
212
+ | Prop | Type | Description |
213
+ | ------------ | ------------------- | ------------------------------------------------------------------------------------------------------ |
214
+ | **`length`** | <code>number</code> | Gets or sets the length of the array. This is a number one higher than the highest index in the array. |
215
+
216
+ | Method | Signature | Description |
217
+ | ------------------ | ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
218
+ | **toString** | () =&gt; string | Returns a string representation of an array. |
219
+ | **toLocaleString** | () =&gt; string | Returns a string representation of an array. The elements are converted to string using their toLocalString methods. |
220
+ | **pop** | () =&gt; T \| undefined | Removes the last element from an array and returns it. If the array is empty, undefined is returned and the array is not modified. |
221
+ | **push** | (...items: T[]) =&gt; number | Appends new elements to the end of an array, and returns the new length of the array. |
222
+ | **concat** | (...items: <a href="#concatarray">ConcatArray</a>&lt;T&gt;[]) =&gt; T[] | Combines two or more arrays. This method returns a new array without modifying any existing arrays. |
223
+ | **concat** | (...items: (T \| <a href="#concatarray">ConcatArray</a>&lt;T&gt;)[]) =&gt; T[] | Combines two or more arrays. This method returns a new array without modifying any existing arrays. |
224
+ | **join** | (separator?: string \| undefined) =&gt; string | Adds all the elements of an array into a string, separated by the specified separator string. |
225
+ | **reverse** | () =&gt; T[] | Reverses the elements in an array in place. This method mutates the array and returns a reference to the same array. |
226
+ | **shift** | () =&gt; T \| undefined | Removes the first element from an array and returns it. If the array is empty, undefined is returned and the array is not modified. |
227
+ | **slice** | (start?: number \| undefined, end?: number \| undefined) =&gt; T[] | Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array. |
228
+ | **sort** | (compareFn?: ((a: T, b: T) =&gt; number) \| undefined) =&gt; this | Sorts an array in place. This method mutates the array and returns a reference to the same array. |
229
+ | **splice** | (start: number, deleteCount?: number \| undefined) =&gt; T[] | Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. |
230
+ | **splice** | (start: number, deleteCount: number, ...items: T[]) =&gt; T[] | Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. |
231
+ | **unshift** | (...items: T[]) =&gt; number | Inserts new elements at the start of an array, and returns the new length of the array. |
232
+ | **indexOf** | (searchElement: T, fromIndex?: number \| undefined) =&gt; number | Returns the index of the first occurrence of a value in an array, or -1 if it is not present. |
233
+ | **lastIndexOf** | (searchElement: T, fromIndex?: number \| undefined) =&gt; number | Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present. |
234
+ | **every** | &lt;S extends T&gt;(predicate: (value: T, index: number, array: T[]) =&gt; value is S, thisArg?: any) =&gt; this is S[] | Determines whether all the members of an array satisfy the specified test. |
235
+ | **every** | (predicate: (value: T, index: number, array: T[]) =&gt; unknown, thisArg?: any) =&gt; boolean | Determines whether all the members of an array satisfy the specified test. |
236
+ | **some** | (predicate: (value: T, index: number, array: T[]) =&gt; unknown, thisArg?: any) =&gt; boolean | Determines whether the specified callback function returns true for any element of an array. |
237
+ | **forEach** | (callbackfn: (value: T, index: number, array: T[]) =&gt; void, thisArg?: any) =&gt; void | Performs the specified action for each element in an array. |
238
+ | **map** | &lt;U&gt;(callbackfn: (value: T, index: number, array: T[]) =&gt; U, thisArg?: any) =&gt; U[] | Calls a defined callback function on each element of an array, and returns an array that contains the results. |
239
+ | **filter** | &lt;S extends T&gt;(predicate: (value: T, index: number, array: T[]) =&gt; value is S, thisArg?: any) =&gt; S[] | Returns the elements of an array that meet the condition specified in a callback function. |
240
+ | **filter** | (predicate: (value: T, index: number, array: T[]) =&gt; unknown, thisArg?: any) =&gt; T[] | Returns the elements of an array that meet the condition specified in a callback function. |
241
+ | **reduce** | (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) =&gt; T) =&gt; T | Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. |
242
+ | **reduce** | (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) =&gt; T, initialValue: T) =&gt; T | |
243
+ | **reduce** | &lt;U&gt;(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) =&gt; U, initialValue: U) =&gt; U | Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. |
244
+ | **reduceRight** | (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) =&gt; T) =&gt; T | Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. |
245
+ | **reduceRight** | (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) =&gt; T, initialValue: T) =&gt; T | |
246
+ | **reduceRight** | &lt;U&gt;(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) =&gt; U, initialValue: U) =&gt; U | Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. |
247
+
248
+
249
+ #### ConcatArray
250
+
251
+ | Prop | Type |
252
+ | ------------ | ------------------- |
253
+ | **`length`** | <code>number</code> |
254
+
255
+ | Method | Signature |
256
+ | --------- | ------------------------------------------------------------------ |
257
+ | **join** | (separator?: string \| undefined) =&gt; string |
258
+ | **slice** | (start?: number \| undefined, end?: number \| undefined) =&gt; T[] |
259
+
260
+
261
+ #### BleDevice
262
+
263
+ | Prop | Type |
264
+ | ------------- | ------------------- |
265
+ | **`address`** | <code>string</code> |
266
+ | **`name`** | <code>string</code> |
267
+ | **`rssi`** | <code>number</code> |
268
+
269
+
270
+ #### ScanOptions
271
+
272
+
273
+ #### ConnectionState
274
+
275
+ | Prop | Type |
276
+ | --------------- | -------------------- |
277
+ | **`connected`** | <code>boolean</code> |
278
+
279
+
280
+ #### ConnectOptions
281
+
282
+ | Prop | Type |
283
+ | ------------- | ------------------- |
284
+ | **`address`** | <code>string</code> |
285
+
286
+
287
+ #### TimeoutOptions
288
+
289
+ | Prop | Type |
290
+ | ------------- | ------------------- |
291
+ | **`timeout`** | <code>number</code> |
292
+
293
+
294
+ #### PrintImageResults
295
+
296
+ | Prop | Type |
297
+ | ----------- | ---------------------------------- |
298
+ | **`state`** | <code>'success' \| 'failed'</code> |
299
+
300
+
301
+ #### PrintImageOptions
302
+
303
+ | Prop | Type |
304
+ | --------------- | --------------------------- |
305
+ | **`width`** | <code>number</code> |
306
+ | **`height`** | <code>number</code> |
307
+ | **`imageData`** | <code>ImageDataArray</code> |
308
+
309
+
310
+ #### PluginListenerHandle
311
+
312
+ | Prop | Type |
313
+ | ------------ | ----------------------------------------- |
314
+ | **`remove`** | <code>() =&gt; Promise&lt;void&gt;</code> |
315
+
316
+
317
+ #### OnStateChangeEvent
318
+
319
+ | Prop | Type |
320
+ | ------------ | ------------------------------------- |
321
+ | **`action`** | <code>'enabled' \| 'connected'</code> |
322
+ | **`state`** | <code>boolean</code> |
323
+
324
+
325
+ ### Type Aliases
326
+
327
+
328
+ #### Permissions
329
+
330
+ <code>{ [key in PermissionKey]: <a href="#permissionstate">PermissionState</a>; }</code>
331
+
332
+
333
+ #### PermissionKey
334
+
335
+ <code>"android.permission.BLUETOOTH" | "android.permission.BLUETOOTH_ADMIN" | "android.permission.ACCESS_COARSE_LOCATION" | "android.permission.ACCESS_FINE_LOCATION" | "android.permission.BLUETOOTH_SCAN" | "android.permission.BLUETOOTH_CONNECT"</code>
336
+
337
+
338
+ #### PermissionState
339
+
340
+ <code>'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'</code>
341
+
342
+ </docgen-api>
@@ -0,0 +1,67 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
4
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
5
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.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
+ apply plugin: 'org.jetbrains.kotlin.android'
20
+
21
+ android {
22
+ namespace = "com.autorunify.capacitor.bleprinter"
23
+ compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
24
+ defaultConfig {
25
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
26
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
27
+ versionCode 1
28
+ versionName "1.0"
29
+ testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
30
+ }
31
+ buildTypes {
32
+ release {
33
+ minifyEnabled false
34
+ proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
35
+ }
36
+ }
37
+ lintOptions {
38
+ abortOnError = false
39
+ }
40
+ compileOptions {
41
+ sourceCompatibility JavaVersion.VERSION_21
42
+ targetCompatibility JavaVersion.VERSION_21
43
+ }
44
+ kotlinOptions {
45
+ jvmTarget = '21'
46
+ }
47
+ }
48
+
49
+
50
+ repositories {
51
+ google()
52
+ mavenCentral()
53
+ }
54
+
55
+
56
+ dependencies {
57
+ implementation 'androidx.core:core-ktx:1.10.1'
58
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
59
+ implementation project(':capacitor-android')
60
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
61
+ testImplementation "junit:junit:$junitVersion"
62
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
63
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
64
+
65
+ implementation 'no.nordicsemi.android:ble:2.7.5'
66
+ implementation 'no.nordicsemi.android.support.v18:scanner:1.6.0'
67
+ }
@@ -0,0 +1,21 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ xmlns:tools="http://schemas.android.com/tools">
3
+
4
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
5
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
6
+
7
+ <uses-permission
8
+ android:name="android.permission.BLUETOOTH"
9
+ android:maxSdkVersion="30" />
10
+ <uses-permission
11
+ android:name="android.permission.BLUETOOTH_ADMIN"
12
+ android:maxSdkVersion="30" />
13
+ <uses-permission
14
+ android:name="android.permission.BLUETOOTH_SCAN"
15
+ android:minSdkVersion="31"
16
+ android:usesPermissionFlags="neverForLocation"
17
+ tools:targetApi="31" />
18
+ <uses-permission
19
+ android:name="android.permission.BLUETOOTH_CONNECT"
20
+ android:minSdkVersion="31" />
21
+ </manifest>
@@ -0,0 +1,118 @@
1
+ package com.autorunify.capacitor.bleprinter
2
+
3
+ import com.getcapacitor.JSObject
4
+ import com.getcapacitor.PluginCall
5
+ import kotlinx.coroutines.CoroutineScope
6
+ import kotlinx.coroutines.Dispatchers
7
+ import kotlinx.coroutines.delay
8
+ import kotlinx.coroutines.launch
9
+
10
+ abstract class AsyncCall {
11
+ protected val call: PluginCall
12
+ var done: Boolean
13
+ protected set
14
+
15
+ constructor(call: PluginCall) {
16
+ this.done = false
17
+ this.call = call
18
+ }
19
+
20
+ abstract fun match(code: Int, address: Int): AsyncCall?
21
+
22
+ open fun trigger(data: JSObject?, message: String?) {
23
+ if (data != null) this.resolve(data)
24
+ if (message != null) this.reject(message)
25
+ }
26
+
27
+ open fun timeout(seconds: Int) {
28
+ CoroutineScope(Dispatchers.Default).launch {
29
+ val milliseconds = (seconds * 1000)
30
+ delay(milliseconds.toLong())
31
+ if (!done) {
32
+ val reply = JSObject()
33
+ reply.put("method", call.methodName)
34
+ reply.put("data", JSObject().apply {
35
+ call.data.keys().forEach { key ->
36
+ put(key, call.data.get(key))
37
+ }
38
+ })
39
+ call.reject("Operation timed out", reply)
40
+ done = true
41
+ }
42
+ }
43
+ }
44
+
45
+ private fun resolve(result: JSObject) {
46
+ if (done) return
47
+
48
+ call.resolve(result)
49
+ done = true
50
+ }
51
+
52
+ private fun reject(message: String) {
53
+ if (done) return
54
+
55
+ call.reject(message)
56
+ done = true
57
+ }
58
+ }
59
+
60
+ class SystemAsyncCall : AsyncCall {
61
+ companion object {
62
+ const val PRINTER_STATE_CONNECTED = 0x08000000;
63
+ const val PRINTER_STATE_DISCONNECTED = 0x08000001;
64
+ const val PRINTER_PRINT_FINISHED = 0x08000002;
65
+ }
66
+
67
+ private val code: Int
68
+ private val address: Int
69
+
70
+ constructor(call: PluginCall, code: Int, address: Int) : super(call) {
71
+ this.code = code
72
+ this.address = address
73
+ }
74
+
75
+ override fun match(code: Int, address: Int): AsyncCall? {
76
+ if (this.code == code && this.address == address) return this
77
+
78
+ return null
79
+ }
80
+ }
81
+
82
+ class AsyncManager {
83
+ private val calls: MutableList<AsyncCall> = mutableListOf<AsyncCall>()
84
+
85
+ private fun removeDoneCalls() {
86
+ val doneCalls = this.calls.filter {
87
+ it.done
88
+ }
89
+
90
+ doneCalls.forEach {
91
+ calls.remove(it)
92
+ }
93
+ }
94
+
95
+
96
+ fun on(call: PluginCall, code: Int, address: Int = 0): AsyncCall {
97
+ removeDoneCalls()
98
+ val async = SystemAsyncCall(call, code, address)
99
+ calls.add(async)
100
+ return async
101
+ }
102
+
103
+ fun emit(data: JSObject, code: Int, address: Int = 0) {
104
+ removeDoneCalls()
105
+ calls.forEach { call ->
106
+ call.match(code, address)?.trigger(data, null)
107
+ }
108
+ removeDoneCalls()
109
+ }
110
+
111
+ fun error(message: String, code: Int, address: Int) {
112
+ removeDoneCalls()
113
+ calls.forEach { call ->
114
+ call.match(code, address)?.trigger(null, message)
115
+ }
116
+ removeDoneCalls()
117
+ }
118
+ }
@@ -0,0 +1,45 @@
1
+ package com.autorunify.capacitor.bleprinter
2
+
3
+ import android.bluetooth.BluetoothDevice
4
+ import no.nordicsemi.android.support.v18.scanner.ScanRecord
5
+ import no.nordicsemi.android.support.v18.scanner.ScanResult
6
+
7
+ class BleDevice {
8
+ val scanResult: ScanResult
9
+ val scanRecord: ScanRecord?
10
+
11
+ val device: BluetoothDevice
12
+ val name: String
13
+ val rssi: Int
14
+
15
+ val address: String
16
+ get() = device.address
17
+
18
+ constructor(scanResult: ScanResult) {
19
+ this.scanResult = scanResult
20
+ this.device = scanResult.device
21
+ this.rssi = scanResult.rssi
22
+
23
+ this.scanRecord = scanResult.scanRecord
24
+ this.name = if (scanRecord != null) scanRecord.deviceName!! else "Unknown"
25
+ }
26
+
27
+ fun match(scanResult: ScanResult): Boolean {
28
+ return this.address == scanResult.device.address
29
+ }
30
+
31
+ override fun equals(other: Any?): Boolean {
32
+ if (other is BleDevice) {
33
+ return device.address == other.device.address
34
+ }
35
+ return super.equals(other)
36
+ }
37
+
38
+ override fun hashCode(): Int {
39
+ var result = device.hashCode() ?: 0
40
+ result = 31 * result + (scanResult.hashCode() ?: 0)
41
+ result = 31 * result + (name.hashCode() ?: 0)
42
+ result = 31 * result + rssi
43
+ return result
44
+ }
45
+ }