react-native-bluebreeze 0.0.1 → 0.0.5
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/android/build.gradle +8 -17
- package/android/generated/java/com/bluebreeze/NativeBlueBreezeSpec.java +17 -9
- package/android/generated/jni/RNBlueBreezeSpec-generated.cpp +21 -14
- package/android/generated/jni/react/renderer/components/RNBlueBreezeSpec/RNBlueBreezeSpecJSI-generated.cpp +19 -12
- package/android/generated/jni/react/renderer/components/RNBlueBreezeSpec/RNBlueBreezeSpecJSI.h +279 -53
- package/android/gradle.properties +5 -5
- package/android/src/main/java/com/bluebreeze/BlueBreezeModule.kt +632 -16
- package/android/src/main/java/com/bluebreeze/BlueBreezePackage.kt +0 -1
- package/ios/BlueBreezeImpl.swift +92 -84
- package/ios/Bluebreeze.mm +52 -25
- package/ios/generated/RNBlueBreezeSpec/RNBlueBreezeSpec-generated.mm +30 -18
- package/ios/generated/RNBlueBreezeSpec/RNBlueBreezeSpec.h +12 -7
- package/ios/generated/RNBlueBreezeSpecJSI-generated.cpp +19 -12
- package/ios/generated/RNBlueBreezeSpecJSI.h +279 -53
- package/lib/commonjs/NativeBlueBreeze.js +11 -0
- package/lib/commonjs/NativeBlueBreeze.js.map +1 -0
- package/lib/commonjs/implementation/bluebreeze_authorization.js +2 -0
- package/lib/commonjs/implementation/bluebreeze_authorization.js.map +1 -0
- package/lib/commonjs/implementation/bluebreeze_characteristic.js +29 -0
- package/lib/commonjs/implementation/bluebreeze_characteristic.js.map +1 -0
- package/lib/commonjs/implementation/bluebreeze_device.js +31 -0
- package/lib/commonjs/implementation/bluebreeze_device.js.map +1 -0
- package/lib/commonjs/implementation/bluebreeze_scan_result.js +20 -0
- package/lib/commonjs/implementation/bluebreeze_scan_result.js.map +1 -0
- package/lib/commonjs/implementation/bluebreeze_service.js +15 -0
- package/lib/commonjs/implementation/bluebreeze_service.js.map +1 -0
- package/lib/commonjs/implementation/bluebreeze_state.js +2 -0
- package/lib/commonjs/implementation/bluebreeze_state.js.map +1 -0
- package/lib/commonjs/implementation/emitters.js +32 -0
- package/lib/commonjs/implementation/emitters.js.map +1 -0
- package/lib/commonjs/index.js +171 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/module/NativeBlueBreeze.js +10 -0
- package/lib/module/NativeBlueBreeze.js.map +1 -0
- package/lib/module/implementation/bluebreeze_authorization.js +2 -0
- package/lib/module/implementation/bluebreeze_authorization.js.map +1 -0
- package/lib/module/implementation/bluebreeze_characteristic.js +23 -0
- package/lib/module/implementation/bluebreeze_characteristic.js.map +1 -0
- package/lib/module/implementation/bluebreeze_device.js +25 -0
- package/lib/module/implementation/bluebreeze_device.js.map +1 -0
- package/lib/module/implementation/bluebreeze_scan_result.js +15 -0
- package/lib/module/implementation/bluebreeze_scan_result.js.map +1 -0
- package/lib/module/implementation/bluebreeze_service.js +10 -0
- package/lib/module/implementation/bluebreeze_service.js.map +1 -0
- package/lib/module/implementation/bluebreeze_state.js +2 -0
- package/lib/module/implementation/bluebreeze_state.js.map +1 -0
- package/lib/module/implementation/emitters.js +25 -0
- package/lib/module/implementation/emitters.js.map +1 -0
- package/lib/module/index.js +168 -0
- package/lib/module/index.js.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/src/NativeBlueBreeze.d.ts +86 -0
- package/lib/typescript/commonjs/src/NativeBlueBreeze.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_authorization.d.ts +2 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_authorization.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts +14 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts +15 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_scan_result.d.ts +13 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_scan_result.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_service.d.ts +8 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_service.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_state.d.ts +2 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_state.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/implementation/emitters.d.ts +12 -0
- package/lib/typescript/commonjs/src/implementation/emitters.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/index.d.ts +32 -0
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/src/NativeBlueBreeze.d.ts +86 -0
- package/lib/typescript/module/src/NativeBlueBreeze.d.ts.map +1 -0
- package/lib/typescript/module/src/implementation/bluebreeze_authorization.d.ts +2 -0
- package/lib/typescript/module/src/implementation/bluebreeze_authorization.d.ts.map +1 -0
- package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts +14 -0
- package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts.map +1 -0
- package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts +15 -0
- package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts.map +1 -0
- package/lib/typescript/module/src/implementation/bluebreeze_scan_result.d.ts +13 -0
- package/lib/typescript/module/src/implementation/bluebreeze_scan_result.d.ts.map +1 -0
- package/lib/typescript/module/src/implementation/bluebreeze_service.d.ts +8 -0
- package/lib/typescript/module/src/implementation/bluebreeze_service.d.ts.map +1 -0
- package/lib/typescript/module/src/implementation/bluebreeze_state.d.ts +2 -0
- package/lib/typescript/module/src/implementation/bluebreeze_state.d.ts.map +1 -0
- package/lib/typescript/module/src/implementation/emitters.d.ts +12 -0
- package/lib/typescript/module/src/implementation/emitters.d.ts.map +1 -0
- package/lib/typescript/module/src/index.d.ts +32 -0
- package/lib/typescript/module/src/index.d.ts.map +1 -0
- package/package.json +25 -21
- package/react-native-bluebreeze.podspec +1 -1
- package/src/NativeBlueBreeze.ts +83 -104
- package/src/implementation/bluebreeze_authorization.ts +1 -0
- package/src/implementation/bluebreeze_characteristic.ts +40 -0
- package/src/implementation/bluebreeze_device.ts +40 -0
- package/src/implementation/bluebreeze_scan_result.ts +32 -0
- package/src/implementation/bluebreeze_service.ts +17 -0
- package/src/implementation/bluebreeze_state.ts +1 -0
- package/src/implementation/emitters.ts +28 -0
- package/src/index.tsx +292 -81
|
@@ -1,23 +1,639 @@
|
|
|
1
1
|
package com.bluebreeze
|
|
2
2
|
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import com.facebook.react.bridge.Arguments
|
|
5
|
+
import com.facebook.react.bridge.Promise
|
|
3
6
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
7
|
+
import com.facebook.react.bridge.ReadableArray
|
|
8
|
+
import com.facebook.react.bridge.WritableArray
|
|
9
|
+
import com.facebook.react.bridge.WritableMap
|
|
4
10
|
import com.facebook.react.module.annotations.ReactModule
|
|
11
|
+
import dev.likemagic.bluebreeze.BBAuthorization
|
|
12
|
+
import dev.likemagic.bluebreeze.BBCharacteristic
|
|
13
|
+
import dev.likemagic.bluebreeze.BBCharacteristicProperty
|
|
14
|
+
import dev.likemagic.bluebreeze.BBDevice
|
|
15
|
+
import dev.likemagic.bluebreeze.BBDeviceConnectionStatus
|
|
16
|
+
import dev.likemagic.bluebreeze.BBManager
|
|
17
|
+
import dev.likemagic.bluebreeze.BBScanResult
|
|
18
|
+
import dev.likemagic.bluebreeze.BBService
|
|
19
|
+
import dev.likemagic.bluebreeze.BBState
|
|
20
|
+
import dev.likemagic.bluebreeze.BBUUID
|
|
21
|
+
import kotlinx.coroutines.CoroutineScope
|
|
22
|
+
import kotlinx.coroutines.Dispatchers
|
|
23
|
+
import kotlinx.coroutines.Job
|
|
24
|
+
import kotlinx.coroutines.flow.FlowCollector
|
|
25
|
+
import kotlinx.coroutines.flow.SharedFlow
|
|
26
|
+
import kotlinx.coroutines.launch
|
|
5
27
|
|
|
6
28
|
@ReactModule(name = BlueBreezeModule.NAME)
|
|
7
|
-
class BlueBreezeModule(reactContext: ReactApplicationContext) :
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
29
|
+
class BlueBreezeModule(reactContext: ReactApplicationContext) : NativeBlueBreezeSpec(reactContext) {
|
|
30
|
+
val manager = BBManager(reactContext)
|
|
31
|
+
val jobs: MutableList<Job> = mutableListOf()
|
|
32
|
+
|
|
33
|
+
val context: Context
|
|
34
|
+
get() = currentActivity ?: reactApplicationContext
|
|
35
|
+
|
|
36
|
+
private val trackedDeviceServices = mutableSetOf<String>()
|
|
37
|
+
private val trackedDeviceConnectionStatus = mutableSetOf<String>()
|
|
38
|
+
private val trackedDeviceMTU = mutableSetOf<String>()
|
|
39
|
+
private val trackedDeviceCharacteristicData = mutableSetOf<String>()
|
|
40
|
+
private val trackedDeviceCharacteristicNotifyEnabled = mutableSetOf<String>()
|
|
41
|
+
|
|
42
|
+
// Lifecycle
|
|
43
|
+
|
|
44
|
+
override fun initialize() {
|
|
45
|
+
super.initialize()
|
|
46
|
+
|
|
47
|
+
// State
|
|
48
|
+
|
|
49
|
+
manager.state.collectAsync {
|
|
50
|
+
safeEmit {
|
|
51
|
+
emitStateEmitter(it.name)
|
|
52
|
+
}
|
|
53
|
+
}.storeIn(jobs)
|
|
54
|
+
|
|
55
|
+
// Authorization
|
|
56
|
+
|
|
57
|
+
manager.authorizationStatus.collectAsync {
|
|
58
|
+
safeEmit {
|
|
59
|
+
emitAuthorizationStatusEmitter(it.toJs)
|
|
60
|
+
}
|
|
61
|
+
}.storeIn(jobs)
|
|
62
|
+
|
|
63
|
+
// Scanning
|
|
64
|
+
|
|
65
|
+
manager.scanEnabled.collectAsync {
|
|
66
|
+
safeEmit {
|
|
67
|
+
emitScanEnabledEmitter(it)
|
|
68
|
+
}
|
|
69
|
+
}.storeIn(jobs)
|
|
70
|
+
|
|
71
|
+
manager.scanResults.collectAsync {
|
|
72
|
+
safeEmit {
|
|
73
|
+
emitScanResultEmitter(it.toJs)
|
|
74
|
+
}
|
|
75
|
+
}.storeIn(jobs)
|
|
76
|
+
|
|
77
|
+
// Devices
|
|
78
|
+
|
|
79
|
+
manager.devices.collectAsync { devices ->
|
|
80
|
+
safeEmit {
|
|
81
|
+
emitDevicesEmitter(
|
|
82
|
+
devices.map { it.value.toJs }.writableArray
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
for ((deviceId, device) in devices) {
|
|
87
|
+
// Device services
|
|
88
|
+
if (trackedDeviceServices.contains(deviceId).not()) {
|
|
89
|
+
trackedDeviceServices.add(deviceId)
|
|
90
|
+
device.services.collectAsync { services ->
|
|
91
|
+
safeEmit {
|
|
92
|
+
emitDeviceServicesEmitter(
|
|
93
|
+
writableMapOf(
|
|
94
|
+
"id" to deviceId,
|
|
95
|
+
"value" to services.map { it.toJs }
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
services.forEach { service ->
|
|
101
|
+
val serviceId = service.uuid
|
|
102
|
+
service.characteristics.forEach { characteristic ->
|
|
103
|
+
val characteristicId = characteristic.uuid
|
|
104
|
+
val trackingKey = "${deviceId}:${serviceId}:${characteristicId}"
|
|
105
|
+
|
|
106
|
+
// Device characteristic data
|
|
107
|
+
if (trackedDeviceCharacteristicData.contains(trackingKey).not()) {
|
|
108
|
+
trackedDeviceCharacteristicData.add(trackingKey)
|
|
109
|
+
characteristic.data.collectAsync { value ->
|
|
110
|
+
safeEmit {
|
|
111
|
+
emitDeviceCharacteristicDataEmitter(
|
|
112
|
+
writableMapOf(
|
|
113
|
+
"id" to deviceId,
|
|
114
|
+
"serviceId" to serviceId.toJs,
|
|
115
|
+
"characteristicId" to characteristicId.toJs,
|
|
116
|
+
"value" to value.toJs
|
|
117
|
+
)
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Device characteristic notify enabled
|
|
124
|
+
if (trackedDeviceCharacteristicNotifyEnabled.contains(trackingKey)
|
|
125
|
+
.not()
|
|
126
|
+
) {
|
|
127
|
+
trackedDeviceCharacteristicNotifyEnabled.add(trackingKey)
|
|
128
|
+
characteristic.isNotifying.collectAsync { value ->
|
|
129
|
+
safeEmit {
|
|
130
|
+
emitDeviceCharacteristicNotifyEnabledEmitter(
|
|
131
|
+
writableMapOf(
|
|
132
|
+
"id" to deviceId,
|
|
133
|
+
"serviceId" to serviceId.toJs,
|
|
134
|
+
"characteristicId" to characteristicId.toJs,
|
|
135
|
+
"value" to value
|
|
136
|
+
)
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Device connection status
|
|
147
|
+
if (trackedDeviceConnectionStatus.contains(deviceId).not()) {
|
|
148
|
+
trackedDeviceConnectionStatus.add(deviceId)
|
|
149
|
+
device.connectionStatus.collectAsync { value ->
|
|
150
|
+
safeEmit {
|
|
151
|
+
emitDeviceConnectionStatusEmitter(
|
|
152
|
+
writableMapOf(
|
|
153
|
+
"id" to deviceId,
|
|
154
|
+
"value" to value.toJs,
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Device MTU
|
|
162
|
+
if (trackedDeviceMTU.contains(deviceId).not()) {
|
|
163
|
+
trackedDeviceMTU.add(deviceId)
|
|
164
|
+
device.mtu.collectAsync { value ->
|
|
165
|
+
safeEmit {
|
|
166
|
+
emitDeviceMTUEmitter(
|
|
167
|
+
writableMapOf(
|
|
168
|
+
"id" to deviceId,
|
|
169
|
+
"value" to value,
|
|
170
|
+
)
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
override fun invalidate() {
|
|
180
|
+
super.invalidate()
|
|
181
|
+
|
|
182
|
+
jobs.forEach { it.cancel() }
|
|
183
|
+
jobs.clear()
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// State
|
|
187
|
+
|
|
188
|
+
override fun state(): String = manager.state.value.name
|
|
189
|
+
|
|
190
|
+
// Authorization
|
|
191
|
+
|
|
192
|
+
override fun authorizationStatus(): String = manager.authorizationStatus.value.toJs
|
|
193
|
+
|
|
194
|
+
override fun authorizationRequest() = manager.authorizationRequest(context)
|
|
195
|
+
|
|
196
|
+
override fun authorizationOpenSettings() = manager.authorizationOpenSettings(context)
|
|
197
|
+
|
|
198
|
+
// Scanning
|
|
199
|
+
|
|
200
|
+
override fun scanEnabled(): Boolean = manager.scanEnabled.value
|
|
201
|
+
|
|
202
|
+
override fun scanStart() = manager.scanStart(context)
|
|
203
|
+
|
|
204
|
+
override fun scanStop() = manager.scanStop(context)
|
|
205
|
+
|
|
206
|
+
// Devices
|
|
207
|
+
|
|
208
|
+
override fun devices(): WritableArray =
|
|
209
|
+
manager.devices.value.map { it.value.toJs }.writableArray
|
|
210
|
+
|
|
211
|
+
// Device services
|
|
212
|
+
|
|
213
|
+
override fun deviceServices(id: String?): WritableArray {
|
|
214
|
+
val device = manager.devices.value[id] ?: return emptyWritableArray()
|
|
215
|
+
return device.services.value.map { it.toJs }.writableArray
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Device connection status
|
|
219
|
+
|
|
220
|
+
override fun deviceConnectionStatus(id: String?): String =
|
|
221
|
+
manager.devices.value[id]?.connectionStatus?.value?.toJs ?: ""
|
|
222
|
+
|
|
223
|
+
// Device MTU
|
|
224
|
+
|
|
225
|
+
override fun deviceMTU(id: String?): Double =
|
|
226
|
+
manager.devices.value[id]?.mtu?.value?.toDouble() ?: 0.0
|
|
227
|
+
|
|
228
|
+
// Device operations
|
|
229
|
+
|
|
230
|
+
override fun deviceConnect(id: String?, promise: Promise?) {
|
|
231
|
+
val device = manager.devices.value[id] ?: run {
|
|
232
|
+
promise?.reject(Throwable("Device not found"))
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async {
|
|
237
|
+
try {
|
|
238
|
+
device.connect()
|
|
239
|
+
promise?.resolve(null)
|
|
240
|
+
} catch (e: Throwable) {
|
|
241
|
+
promise?.reject(e)
|
|
242
|
+
}
|
|
243
|
+
}.storeIn(jobs)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
override fun deviceDisconnect(id: String?, promise: Promise?) {
|
|
247
|
+
val device = manager.devices.value[id] ?: run {
|
|
248
|
+
promise?.reject(Throwable("Device not found"))
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async {
|
|
253
|
+
try {
|
|
254
|
+
device.disconnect()
|
|
255
|
+
promise?.resolve(null)
|
|
256
|
+
} catch (e: Throwable) {
|
|
257
|
+
promise?.reject(e)
|
|
258
|
+
}
|
|
259
|
+
}.storeIn(jobs)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
override fun deviceDiscoverServices(id: String?, promise: Promise?) {
|
|
263
|
+
val device = manager.devices.value[id] ?: run {
|
|
264
|
+
promise?.reject(Throwable("Device not found"))
|
|
265
|
+
return
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async {
|
|
269
|
+
try {
|
|
270
|
+
device.discoverServices()
|
|
271
|
+
promise?.resolve(null)
|
|
272
|
+
} catch (e: Throwable) {
|
|
273
|
+
promise?.reject(e)
|
|
274
|
+
}
|
|
275
|
+
}.storeIn(jobs)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
override fun deviceRequestMTU(id: String?, mtu: Double, promise: Promise?) {
|
|
279
|
+
val device = manager.devices.value[id] ?: run {
|
|
280
|
+
promise?.reject(Throwable("Device not found"))
|
|
281
|
+
return
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async {
|
|
285
|
+
try {
|
|
286
|
+
val result = device.requestMTU(mtu.toInt())
|
|
287
|
+
promise?.resolve(result)
|
|
288
|
+
} catch (e: Throwable) {
|
|
289
|
+
promise?.reject(e)
|
|
290
|
+
}
|
|
291
|
+
}.storeIn(jobs)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Device characteristic data
|
|
295
|
+
|
|
296
|
+
override fun deviceCharacteristicData(
|
|
297
|
+
id: String?,
|
|
298
|
+
serviceId: String?,
|
|
299
|
+
characteristicId: String?
|
|
300
|
+
): WritableArray {
|
|
301
|
+
val device = manager.devices.value[id] ?: return emptyWritableArray()
|
|
302
|
+
val service = device.services.value.firstOrNull {
|
|
303
|
+
it.uuid.toString() == serviceId
|
|
304
|
+
} ?: return emptyWritableArray()
|
|
305
|
+
val characteristic = service.characteristics.firstOrNull {
|
|
306
|
+
it.uuid.toString() == characteristicId
|
|
307
|
+
} ?: return emptyWritableArray()
|
|
308
|
+
return characteristic.data.value.toJs
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Device characteristic notify enabled
|
|
312
|
+
|
|
313
|
+
override fun deviceCharacteristicNotifyEnabled(
|
|
314
|
+
id: String?,
|
|
315
|
+
serviceId: String?,
|
|
316
|
+
characteristicId: String?
|
|
317
|
+
): Boolean {
|
|
318
|
+
val device = manager.devices.value[id] ?: return false
|
|
319
|
+
val service = device.services.value.firstOrNull {
|
|
320
|
+
it.uuid.toString() == serviceId
|
|
321
|
+
} ?: return false
|
|
322
|
+
val characteristic = service.characteristics.firstOrNull {
|
|
323
|
+
it.uuid.toString() == characteristicId
|
|
324
|
+
} ?: return false
|
|
325
|
+
return characteristic.isNotifying.value
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Device characteristic operations
|
|
329
|
+
|
|
330
|
+
override fun deviceCharacteristicRead(
|
|
331
|
+
id: String?,
|
|
332
|
+
serviceId: String?,
|
|
333
|
+
characteristicId: String?,
|
|
334
|
+
promise: Promise?
|
|
335
|
+
) {
|
|
336
|
+
val device = manager.devices.value[id] ?: run {
|
|
337
|
+
promise?.reject(Throwable("Device not found"))
|
|
338
|
+
return
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
val service = device.services.value.firstOrNull {
|
|
342
|
+
it.uuid.toString() == serviceId
|
|
343
|
+
} ?: run {
|
|
344
|
+
promise?.reject(Throwable("Service not found"))
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
val characteristic = service.characteristics.firstOrNull {
|
|
349
|
+
it.uuid.toString() == characteristicId
|
|
350
|
+
} ?: run {
|
|
351
|
+
promise?.reject(Throwable("Characteristic not found"))
|
|
352
|
+
return
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async {
|
|
356
|
+
try {
|
|
357
|
+
val result = characteristic.read()
|
|
358
|
+
promise?.resolve(result.toJs)
|
|
359
|
+
|
|
360
|
+
} catch (e: Throwable) {
|
|
361
|
+
promise?.reject(e)
|
|
362
|
+
}
|
|
363
|
+
}.storeIn(jobs)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
override fun deviceCharacteristicWrite(
|
|
367
|
+
id: String?,
|
|
368
|
+
serviceId: String?,
|
|
369
|
+
characteristicId: String?,
|
|
370
|
+
data: ReadableArray?,
|
|
371
|
+
withResponse: Boolean,
|
|
372
|
+
promise: Promise?
|
|
373
|
+
) {
|
|
374
|
+
val device = manager.devices.value[id] ?: run {
|
|
375
|
+
promise?.reject(Throwable("Device not found"))
|
|
376
|
+
return
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
val service = device.services.value.firstOrNull {
|
|
380
|
+
it.uuid.toString() == serviceId
|
|
381
|
+
} ?: run {
|
|
382
|
+
promise?.reject(Throwable("Service not found"))
|
|
383
|
+
return
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
val characteristic = service.characteristics.firstOrNull {
|
|
387
|
+
it.uuid.toString() == characteristicId
|
|
388
|
+
} ?: run {
|
|
389
|
+
promise?.reject(Throwable("Characteristic not found"))
|
|
390
|
+
return
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
val writeValue = ByteArray(data?.size() ?: 0)
|
|
394
|
+
data?.toArrayList()?.forEachIndexed { index, v ->
|
|
395
|
+
writeValue[index] = (v as Int).toByte()
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
async {
|
|
399
|
+
try {
|
|
400
|
+
characteristic.write(
|
|
401
|
+
data = writeValue,
|
|
402
|
+
withResponse = withResponse,
|
|
403
|
+
)
|
|
404
|
+
promise?.resolve(null)
|
|
405
|
+
} catch (e: Throwable) {
|
|
406
|
+
promise?.reject(e)
|
|
407
|
+
}
|
|
408
|
+
}.storeIn(jobs)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
override fun deviceCharacteristicSubscribe(
|
|
412
|
+
id: String?,
|
|
413
|
+
serviceId: String?,
|
|
414
|
+
characteristicId: String?,
|
|
415
|
+
promise: Promise?
|
|
416
|
+
) {
|
|
417
|
+
val device = manager.devices.value[id] ?: run {
|
|
418
|
+
promise?.reject(Throwable("Device not found"))
|
|
419
|
+
return
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
val service = device.services.value.firstOrNull {
|
|
423
|
+
it.uuid.toString() == serviceId
|
|
424
|
+
} ?: run {
|
|
425
|
+
promise?.reject(Throwable("Service not found"))
|
|
426
|
+
return
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
val characteristic = service.characteristics.firstOrNull {
|
|
430
|
+
it.uuid.toString() == characteristicId
|
|
431
|
+
} ?: run {
|
|
432
|
+
promise?.reject(Throwable("Characteristic not found"))
|
|
433
|
+
return
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
async {
|
|
437
|
+
try {
|
|
438
|
+
characteristic.subscribe()
|
|
439
|
+
promise?.resolve(null)
|
|
440
|
+
} catch (e: Throwable) {
|
|
441
|
+
promise?.reject(e)
|
|
442
|
+
}
|
|
443
|
+
}.storeIn(jobs)
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
override fun deviceCharacteristicUnsubscribe(
|
|
447
|
+
id: String?,
|
|
448
|
+
serviceId: String?,
|
|
449
|
+
characteristicId: String?,
|
|
450
|
+
promise: Promise?
|
|
451
|
+
) {
|
|
452
|
+
val device = manager.devices.value[id] ?: run {
|
|
453
|
+
promise?.reject(Throwable("Device not found"))
|
|
454
|
+
return
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
val service = device.services.value.firstOrNull {
|
|
458
|
+
it.uuid.toString() == serviceId
|
|
459
|
+
} ?: run {
|
|
460
|
+
promise?.reject(Throwable("Service not found"))
|
|
461
|
+
return
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
val characteristic = service.characteristics.firstOrNull {
|
|
465
|
+
it.uuid.toString() == characteristicId
|
|
466
|
+
} ?: run {
|
|
467
|
+
promise?.reject(Throwable("Characteristic not found"))
|
|
468
|
+
return
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
async {
|
|
472
|
+
try {
|
|
473
|
+
characteristic.unsubscribe()
|
|
474
|
+
promise?.resolve(null)
|
|
475
|
+
} catch (e: Throwable) {
|
|
476
|
+
promise?.reject(e)
|
|
477
|
+
}
|
|
478
|
+
}.storeIn(jobs)
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Helper for safely emitting data
|
|
482
|
+
|
|
483
|
+
private fun safeEmit(block: () -> (Unit)) {
|
|
484
|
+
if (mEventEmitterCallback != null) {
|
|
485
|
+
block()
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// Module name
|
|
490
|
+
|
|
491
|
+
override fun getName(): String {
|
|
492
|
+
return NAME
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
companion object {
|
|
496
|
+
const val NAME = "BlueBreeze"
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/// Export native values
|
|
501
|
+
|
|
502
|
+
val BBAuthorization.toJs: String
|
|
503
|
+
get() = when (this) {
|
|
504
|
+
BBAuthorization.unknown -> "unknown"
|
|
505
|
+
BBAuthorization.showRationale -> "showRationale"
|
|
506
|
+
BBAuthorization.authorized -> "authorized"
|
|
507
|
+
BBAuthorization.denied -> "denied"
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
val BBState.toJs: String
|
|
511
|
+
get() = when (this) {
|
|
512
|
+
BBState.unknown -> "unknown"
|
|
513
|
+
BBState.poweredOff -> "poweredOff"
|
|
514
|
+
BBState.poweredOn -> "poweredOn"
|
|
515
|
+
BBState.unauthorized -> "unauthorized"
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
val BBCharacteristicProperty.toJs: String
|
|
519
|
+
get() = when (this) {
|
|
520
|
+
BBCharacteristicProperty.read -> "read"
|
|
521
|
+
BBCharacteristicProperty.writeWithoutResponse -> "writeWithoutResponse"
|
|
522
|
+
BBCharacteristicProperty.writeWithResponse -> "writeWithResponse"
|
|
523
|
+
BBCharacteristicProperty.notify -> "notify"
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
val BBScanResult.toJs: WritableMap
|
|
527
|
+
get() = writableMapOf(
|
|
528
|
+
"id" to device.address,
|
|
529
|
+
"rssi" to rssi,
|
|
530
|
+
"connectable" to connectable,
|
|
531
|
+
"advertisedServices" to advertisedServices.map { it.toJs },
|
|
532
|
+
"manufacturerId" to manufacturerId,
|
|
533
|
+
"manufacturerName" to manufacturerName,
|
|
534
|
+
"manufacturerData" to manufacturerData?.toJs,
|
|
535
|
+
)
|
|
536
|
+
|
|
537
|
+
val BBDevice.toJs: WritableMap
|
|
538
|
+
get() = writableMapOf(
|
|
539
|
+
"id" to address,
|
|
540
|
+
"name" to name,
|
|
541
|
+
)
|
|
542
|
+
|
|
543
|
+
val BBDeviceConnectionStatus.toJs: String
|
|
544
|
+
get() = when (this) {
|
|
545
|
+
BBDeviceConnectionStatus.connected -> "connected"
|
|
546
|
+
BBDeviceConnectionStatus.disconnected -> "disconnected"
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
val BBCharacteristic.toJs: WritableMap
|
|
550
|
+
get() = writableMapOf(
|
|
551
|
+
"id" to uuid.toString(),
|
|
552
|
+
"properties" to properties.map { it.toJs },
|
|
553
|
+
"name" to name,
|
|
554
|
+
)
|
|
555
|
+
|
|
556
|
+
val BBService.toJs: WritableMap
|
|
557
|
+
get() = writableMapOf(
|
|
558
|
+
"id" to uuid.toString(),
|
|
559
|
+
"characteristics" to characteristics.map { it.toJs },
|
|
560
|
+
"name" to name,
|
|
561
|
+
)
|
|
562
|
+
|
|
563
|
+
val ByteArray.toJs: WritableArray
|
|
564
|
+
get() {
|
|
565
|
+
val result = Arguments.createArray()
|
|
566
|
+
forEach { result.pushInt(it.toInt()) }
|
|
567
|
+
return result
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
val BBUUID.toJs: String
|
|
571
|
+
get() = toString()
|
|
572
|
+
|
|
573
|
+
/// Functions to collect data asynchronously
|
|
574
|
+
|
|
575
|
+
fun Job.storeIn(list: MutableList<Job>) = list.add(this)
|
|
576
|
+
|
|
577
|
+
fun async(
|
|
578
|
+
block: suspend CoroutineScope.() -> Unit,
|
|
579
|
+
): Job = CoroutineScope(Dispatchers.IO).launch(block = block)
|
|
580
|
+
|
|
581
|
+
fun <T> SharedFlow<T>.collectAsync(
|
|
582
|
+
collector: FlowCollector<T>,
|
|
583
|
+
): Job {
|
|
584
|
+
return CoroutineScope(Dispatchers.IO)
|
|
585
|
+
.launch {
|
|
586
|
+
collect(collector)
|
|
587
|
+
}
|
|
23
588
|
}
|
|
589
|
+
|
|
590
|
+
/// Functions to handle React Native types
|
|
591
|
+
|
|
592
|
+
val List<*>.writableArray: WritableArray
|
|
593
|
+
get() {
|
|
594
|
+
val result = Arguments.createArray()
|
|
595
|
+
forEach { value ->
|
|
596
|
+
when (value) {
|
|
597
|
+
null -> result.pushNull()
|
|
598
|
+
is Boolean -> result.pushBoolean(value)
|
|
599
|
+
is Double -> result.pushDouble(value)
|
|
600
|
+
is Int -> result.pushInt(value)
|
|
601
|
+
is String -> result.pushString(value)
|
|
602
|
+
is Map<*, *> -> result.pushMap(value.writableMap)
|
|
603
|
+
is List<*> -> result.pushArray(value.writableArray)
|
|
604
|
+
is WritableArray -> result.pushArray(value)
|
|
605
|
+
is WritableMap -> result.pushMap(value)
|
|
606
|
+
else -> throw Throwable("Unsupported type ${value.javaClass}")
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
return result
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
val Map<*, *>.writableMap: WritableMap
|
|
614
|
+
get() {
|
|
615
|
+
val result = Arguments.createMap()
|
|
616
|
+
entries.forEach { (key, value) ->
|
|
617
|
+
if (key is String) {
|
|
618
|
+
when (value) {
|
|
619
|
+
null -> result.putNull(key)
|
|
620
|
+
is Boolean -> result.putBoolean(key, value)
|
|
621
|
+
is Double -> result.putDouble(key, value)
|
|
622
|
+
is Int -> result.putInt(key, value)
|
|
623
|
+
is String -> result.putString(key, value)
|
|
624
|
+
is Map<*, *> -> result.putMap(key, value.writableMap)
|
|
625
|
+
is List<*> -> result.putArray(key, value.writableArray)
|
|
626
|
+
is WritableArray -> result.putArray(key, value)
|
|
627
|
+
is WritableMap -> result.putMap(key, value)
|
|
628
|
+
else -> throw Throwable("Unsupported type ${value.javaClass}")
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return result
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
fun <T> writableArrayOf(vararg pairs: T): WritableArray = listOf(*pairs).writableArray
|
|
636
|
+
|
|
637
|
+
fun emptyWritableArray(): WritableArray = writableArrayOf<Void>()
|
|
638
|
+
|
|
639
|
+
fun <K, V> writableMapOf(vararg pairs: Pair<K, V>): WritableMap = mapOf(*pairs).writableMap
|