react-native-bluebreeze 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.
@@ -0,0 +1,34 @@
1
+ package com.bluebreeze
2
+
3
+ import com.facebook.react.BaseReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.module.model.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+ import java.util.HashMap
9
+
10
+ class BlueBreezePackage : BaseReactPackage() {
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
+ return if (name == BlueBreezeModule.NAME) {
13
+ BlueBreezeModule(reactContext)
14
+ } else {
15
+ null
16
+ }
17
+ }
18
+
19
+ override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
20
+ return ReactModuleInfoProvider {
21
+ val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
22
+ moduleInfos[BlueBreezeModule.NAME] = ReactModuleInfo(
23
+ BlueBreezeModule.NAME,
24
+ BlueBreezeModule.NAME,
25
+ false, // canOverrideExistingModule
26
+ false, // needsEagerInit
27
+ true, // hasConstants
28
+ false, // isCxxModule
29
+ true // isTurboModule
30
+ )
31
+ moduleInfos
32
+ }
33
+ }
34
+ }
@@ -0,0 +1,477 @@
1
+ //
2
+ // BlueBreeze.swift
3
+ // react-native-bluebreeze
4
+ //
5
+ // Created by Alessandro Mulloni on 03.01.25.
6
+ //
7
+
8
+ import BlueBreeze
9
+ import Combine
10
+ import Foundation
11
+
12
+ struct BlueBreezeError : Error {
13
+ let description: String
14
+ }
15
+
16
+ @objc public class BlueBreezeImpl: NSObject {
17
+ var disposeBag: Set<AnyCancellable> = []
18
+ let manager = BBManager()
19
+
20
+ // MARK: - Authorization
21
+
22
+ @objc public func authorizationStatus() -> String {
23
+ return manager.authorizationStatus.value.export
24
+ }
25
+
26
+ @objc public func authorizationStatusObserve(onChanged: @escaping (String) -> Void) {
27
+ manager.authorizationStatus
28
+ .receive(on: DispatchQueue.main)
29
+ .sink { onChanged($0.export) }
30
+ .store(in: &disposeBag)
31
+ }
32
+
33
+ @objc public func authorizationRequest() {
34
+ manager.authorizationRequest()
35
+ }
36
+
37
+ // MARK: - State
38
+
39
+ @objc public func state() -> String {
40
+ return manager.state.value.export
41
+ }
42
+
43
+ @objc public func stateObserve(onChanged: @escaping (String) -> Void) {
44
+ manager.state
45
+ .receive(on: DispatchQueue.main)
46
+ .sink { onChanged($0.export) }
47
+ .store(in: &disposeBag)
48
+ }
49
+
50
+ // MARK: - Scanning
51
+
52
+ @objc public func scanningEnabled() -> Bool {
53
+ return manager.isScanning.value
54
+ }
55
+
56
+ @objc public func scanningEnabledObserve(onChanged: @escaping (Bool) -> Void) {
57
+ return manager.isScanning
58
+ .receive(on: DispatchQueue.main)
59
+ .sink { onChanged($0) }
60
+ .store(in: &disposeBag)
61
+ }
62
+
63
+ @objc public func scanningStart() {
64
+ manager.scanningStart()
65
+ }
66
+
67
+ @objc public func scanningStop() {
68
+ manager.scanningStop()
69
+ }
70
+
71
+ // MARK: - Devices
72
+
73
+ @objc public func devices() -> [Dictionary<String, Any>] {
74
+ return manager.devices.value.values.map { $0.export }
75
+ }
76
+
77
+ @objc public func devicesObserve(onChanged: @escaping ([Dictionary<String, Any>]) -> Void) {
78
+ return manager.devices
79
+ .receive(on: DispatchQueue.main)
80
+ .sink { onChanged($0.values.map { $0.export }) }
81
+ .store(in: &disposeBag)
82
+ }
83
+
84
+ // MARK: - Device services
85
+
86
+ @objc public func deviceServices(id: String) -> [Dictionary<String, Any>] {
87
+ guard let uuid = UUID(uuidString: id) else {
88
+ return []
89
+ }
90
+
91
+ guard let device = manager.devices.value[uuid] else {
92
+ return []
93
+ }
94
+
95
+ return device.services.value.export
96
+ }
97
+
98
+ @objc public func deviceServicesObserve(id: String, onChanged: @escaping ([Dictionary<String, Any>]) -> Void) {
99
+ guard let uuid = UUID(uuidString: id) else {
100
+ return
101
+ }
102
+
103
+ guard let device = manager.devices.value[uuid] else {
104
+ return
105
+ }
106
+
107
+ return device.services
108
+ .receive(on: DispatchQueue.main)
109
+ .sink { onChanged($0.export) }
110
+ .store(in: &disposeBag)
111
+ }
112
+
113
+ // MARK: - Device connection status
114
+
115
+ @objc public func deviceConnectionStatus(id: String) -> String {
116
+ guard let uuid = UUID(uuidString: id) else {
117
+ return ""
118
+ }
119
+
120
+ guard let device = manager.devices.value[uuid] else {
121
+ return ""
122
+ }
123
+
124
+ return device.connectionStatus.value.export
125
+ }
126
+
127
+ @objc public func deviceConnectionStatusObserve(id: String, onChanged: @escaping (String) -> Void) {
128
+ guard let uuid = UUID(uuidString: id) else {
129
+ return
130
+ }
131
+
132
+ guard let device = manager.devices.value[uuid] else {
133
+ return
134
+ }
135
+
136
+ return device.connectionStatus
137
+ .receive(on: DispatchQueue.main)
138
+ .sink { onChanged($0.export) }
139
+ .store(in: &disposeBag)
140
+ }
141
+
142
+ // MARK: - Device MTU
143
+
144
+ @objc public func deviceMTU(id: String) -> Int {
145
+ guard let uuid = UUID(uuidString: id) else {
146
+ return 0
147
+ }
148
+
149
+ guard let device = manager.devices.value[uuid] else {
150
+ return 0
151
+ }
152
+
153
+ return device.mtu.value
154
+ }
155
+
156
+ @objc public func deviceMTUObserve(id: String, onChanged: @escaping (Int) -> Void) {
157
+ guard let uuid = UUID(uuidString: id) else {
158
+ return
159
+ }
160
+
161
+ guard let device = manager.devices.value[uuid] else {
162
+ return
163
+ }
164
+
165
+ return device.mtu
166
+ .receive(on: DispatchQueue.main)
167
+ .sink { onChanged($0) }
168
+ .store(in: &disposeBag)
169
+ }
170
+
171
+ // MARK: - Device operations
172
+
173
+ @objc public func deviceConnect(id: String) async throws {
174
+ guard let uuid = UUID(uuidString: id) else {
175
+ throw BlueBreezeError(description: "Invalid UUID")
176
+ }
177
+
178
+ guard let device = manager.devices.value[uuid] else {
179
+ throw BlueBreezeError(description: "Device not found")
180
+ }
181
+
182
+ try await device.connect()
183
+ }
184
+
185
+ @objc public func deviceDisconnect(id: String) async throws {
186
+ guard let uuid = UUID(uuidString: id) else {
187
+ throw BlueBreezeError(description: "Invalid UUID")
188
+ }
189
+
190
+ guard let device = manager.devices.value[uuid] else {
191
+ throw BlueBreezeError(description: "Device not found")
192
+ }
193
+
194
+ try await device.disconnect()
195
+ }
196
+
197
+ @objc public func deviceDiscoverServices(id: String) async throws {
198
+ guard let uuid = UUID(uuidString: id) else {
199
+ throw BlueBreezeError(description: "Invalid UUID")
200
+ }
201
+
202
+ guard let device = manager.devices.value[uuid] else {
203
+ throw BlueBreezeError(description: "Device not found")
204
+ }
205
+
206
+ try await device.discoverServices()
207
+ }
208
+
209
+ @objc public func deviceRequestMTU(id: String, mtu: Int) async throws {
210
+ guard let uuid = UUID(uuidString: id) else {
211
+ throw BlueBreezeError(description: "Invalid UUID")
212
+ }
213
+
214
+ guard let device = manager.devices.value[uuid] else {
215
+ throw BlueBreezeError(description: "Device not found")
216
+ }
217
+
218
+ try await device.requestMTU(mtu)
219
+ }
220
+
221
+ // MARK: - Device characteristic data
222
+
223
+ @objc public func deviceCharacteristicData(id: String, serviceId: String, characteristicId: String) -> Array<UInt8> {
224
+ guard let uuid = UUID(uuidString: id)else {
225
+ return []
226
+ }
227
+
228
+ guard let device = manager.devices.value[uuid]else {
229
+ return []
230
+ }
231
+
232
+ guard let characteristic = device.getCharacteristic(serviceId: serviceId, characteristicId: characteristicId) else {
233
+ return []
234
+ }
235
+
236
+ return characteristic.data.value.export
237
+ }
238
+
239
+ @objc public func deviceCharacteristicDataObserve(id: String, serviceId: String, characteristicId: String, onChanged: @escaping (Array<UInt8>) -> Void) {
240
+ guard let uuid = UUID(uuidString: id) else {
241
+ return
242
+ }
243
+
244
+ guard let device = manager.devices.value[uuid] else {
245
+ return
246
+ }
247
+
248
+ guard let characteristic = device.getCharacteristic(serviceId: serviceId, characteristicId: characteristicId) else {
249
+ return
250
+ }
251
+
252
+ return characteristic.data
253
+ .receive(on: DispatchQueue.main)
254
+ .sink { onChanged($0.export) }
255
+ .store(in: &disposeBag)
256
+ }
257
+
258
+ // MARK: - Device notify enabled
259
+
260
+ @objc public func deviceCharacteristicNotifyEnabled(id: String, serviceId: String, characteristicId: String) -> Bool {
261
+ guard let uuid = UUID(uuidString: id)else {
262
+ return false
263
+ }
264
+
265
+ guard let device = manager.devices.value[uuid]else {
266
+ return false
267
+ }
268
+
269
+ guard let characteristic = device.getCharacteristic(serviceId: serviceId, characteristicId: characteristicId) else {
270
+ return false
271
+ }
272
+
273
+ return characteristic.isNotifying.value
274
+ }
275
+
276
+ @objc public func deviceCharacteristicNotifyEnabledObserve(id: String, serviceId: String, characteristicId: String, onChanged: @escaping (Bool) -> Void) {
277
+ guard let uuid = UUID(uuidString: id) else {
278
+ return
279
+ }
280
+
281
+ guard let device = manager.devices.value[uuid] else {
282
+ return
283
+ }
284
+
285
+ guard let characteristic = device.getCharacteristic(serviceId: serviceId, characteristicId: characteristicId) else {
286
+ return
287
+ }
288
+
289
+ return characteristic.isNotifying
290
+ .receive(on: DispatchQueue.main)
291
+ .sink { onChanged($0) }
292
+ .store(in: &disposeBag)
293
+ }
294
+
295
+ // MARK: - Device operations
296
+
297
+ @objc public func deviceCharacteristicRead(id: String, serviceId: String, characteristicId: String) async throws -> Array<UInt8> {
298
+ guard let uuid = UUID(uuidString: id) else {
299
+ throw BlueBreezeError(description: "Invalid UUID")
300
+ }
301
+
302
+ guard let device = manager.devices.value[uuid] else {
303
+ throw BlueBreezeError(description: "Device not found")
304
+ }
305
+
306
+ guard let characteristic = device.getCharacteristic(serviceId: serviceId, characteristicId: characteristicId) else {
307
+ throw BlueBreezeError(description: "Characteristic not found")
308
+ }
309
+
310
+ /*let data =*/ try await characteristic.read()
311
+ return []
312
+ }
313
+
314
+ @objc public func deviceCharacteristicWrite(id: String, serviceId: String, characteristicId: String, data: Array<UInt8>, withResponse: Bool) async throws {
315
+ guard let uuid = UUID(uuidString: id) else {
316
+ throw BlueBreezeError(description: "Invalid UUID")
317
+ }
318
+
319
+ guard let device = manager.devices.value[uuid] else {
320
+ throw BlueBreezeError(description: "Device not found")
321
+ }
322
+
323
+ guard let characteristic = device.getCharacteristic(serviceId: serviceId, characteristicId: characteristicId) else {
324
+ throw BlueBreezeError(description: "Characteristic not found")
325
+ }
326
+
327
+ try await characteristic.write(Data(data), withResponse: withResponse)
328
+ }
329
+
330
+ @objc public func deviceCharacteristicSubscribe(id: String, serviceId: String, characteristicId: String) async throws {
331
+ guard let uuid = UUID(uuidString: id) else {
332
+ throw BlueBreezeError(description: "Invalid UUID")
333
+ }
334
+
335
+ guard let device = manager.devices.value[uuid] else {
336
+ throw BlueBreezeError(description: "Device not found")
337
+ }
338
+
339
+ guard let characteristic = device.getCharacteristic(serviceId: serviceId, characteristicId: characteristicId) else {
340
+ throw BlueBreezeError(description: "Characteristic not found")
341
+ }
342
+
343
+ try await characteristic.subscribe()
344
+ }
345
+
346
+ @objc public func deviceCharacteristicUnsubscribe(id: String, serviceId: String, characteristicId: String) async throws {
347
+ guard let uuid = UUID(uuidString: id) else {
348
+ throw BlueBreezeError(description: "Invalid UUID")
349
+ }
350
+
351
+ guard let device = manager.devices.value[uuid] else {
352
+ throw BlueBreezeError(description: "Device not found")
353
+ }
354
+
355
+ guard let characteristic = device.getCharacteristic(serviceId: serviceId, characteristicId: characteristicId) else {
356
+ throw BlueBreezeError(description: "Characteristic not found")
357
+ }
358
+
359
+ try await characteristic.unsubscribe()
360
+ }
361
+ }
362
+
363
+ extension BBAuthorization {
364
+ var export: String {
365
+ switch self {
366
+ case .unknown: return "unknown"
367
+ case .authorized: return "authorized"
368
+ case .denied: return "denied"
369
+ }
370
+ }
371
+ }
372
+
373
+ extension BBState {
374
+ var export: String {
375
+ switch self {
376
+ case .unknown: return "unknown"
377
+ case .poweredOff: return "poweredOff"
378
+ case .poweredOn: return "poweredOn"
379
+ case .unauthorized: return "unauthorized"
380
+ case .unsupported: return "unsupported"
381
+ case .resetting: return "resetting"
382
+ }
383
+ }
384
+ }
385
+
386
+ extension BBCharacteristicProperty {
387
+ var export: String {
388
+ switch self {
389
+ case .read: return "read"
390
+ case .writeWithoutResponse: return "writeWithoutResponse"
391
+ case .writeWithResponse: return "writeWithResponse"
392
+ case .notify: return "notify"
393
+ }
394
+ }
395
+ }
396
+
397
+ extension BBDevice {
398
+ var export: Dictionary<String, Any> {
399
+ var result: Dictionary<String, Any> = [
400
+ "id": id.uuidString,
401
+ "rssi": rssi,
402
+ "isConnectable": isConnectable,
403
+ "advertisedServices": advertisedServices.map(\.uuidString),
404
+ ]
405
+
406
+ if let name {
407
+ result["name"] = name
408
+ }
409
+
410
+ if let manufacturerId {
411
+ result["manufacturerId"] = manufacturerId
412
+ }
413
+
414
+ if let manufacturerName {
415
+ result["manufacturerName"] = manufacturerName
416
+ }
417
+
418
+ if let manufacturerData {
419
+ result["manufacturerData"] = manufacturerData.export
420
+ }
421
+
422
+ return result;
423
+ }
424
+
425
+ func getCharacteristic(serviceId: String, characteristicId: String) -> BBCharacteristic? {
426
+ let service = services.value.first { $0.key == BBUUID(string: serviceId)}
427
+ let characteristic = service?.value.first { $0.id == BBUUID(string: characteristicId) }
428
+ return characteristic
429
+ }
430
+ }
431
+
432
+ extension BBDeviceConnectionStatus {
433
+ var export: String {
434
+ switch self {
435
+ case .connected: return "connected"
436
+ case .disconnected: return "disconnected"
437
+ }
438
+ }
439
+ }
440
+
441
+ extension BBCharacteristic {
442
+ var export: Dictionary<String, Any> {
443
+ var result: Dictionary<String, Any> = [
444
+ "id": id.uuidString,
445
+ "properties": properties.map { $0.export }
446
+ ]
447
+
448
+ if let name = BBConstants.knownCharacteristics[id] {
449
+ result["name"] = name
450
+ }
451
+
452
+ return result
453
+ }
454
+ }
455
+
456
+ extension [BBUUID: [BBCharacteristic]] {
457
+ var export: [Dictionary<String, Any>] {
458
+ return map { element in
459
+ var result: Dictionary<String, Any> = [
460
+ "id": element.key.uuidString,
461
+ "characteristics": element.value.map { $0.export }
462
+ ]
463
+
464
+ if let name = BBConstants.knownServices[element.key] {
465
+ result["name"] = name
466
+ }
467
+
468
+ return result
469
+ }
470
+ }
471
+ }
472
+
473
+ extension Data {
474
+ var export: Array<UInt8> {
475
+ return map { $0 }
476
+ }
477
+ }
@@ -0,0 +1,6 @@
1
+
2
+ #import "generated/RNBlueBreezeSpec/RNBlueBreezeSpec.h"
3
+
4
+ @interface BlueBreeze : NativeBlueBreezeSpecBase <NativeBlueBreezeSpec>
5
+
6
+ @end