react-native-bluebreeze 1.0.2 → 1.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.
Files changed (61) hide show
  1. package/README.md +31 -4
  2. package/android/build.gradle +1 -1
  3. package/android/generated/java/com/bluebreeze/NativeBlueBreezeSpec.java +0 -8
  4. package/android/generated/jni/RNBlueBreezeSpec-generated.cpp +0 -7
  5. package/android/generated/jni/react/renderer/components/RNBlueBreezeSpec/RNBlueBreezeSpecJSI.h +0 -78
  6. package/android/src/main/java/com/bluebreeze/BlueBreezeModule.kt +9 -41
  7. package/ios/BlueBreezeImpl.swift +10 -25
  8. package/ios/Bluebreeze.mm +0 -9
  9. package/ios/generated/ReactCodegen/RNBlueBreezeSpec/RNBlueBreezeSpec-generated.mm +0 -12
  10. package/ios/generated/ReactCodegen/RNBlueBreezeSpec/RNBlueBreezeSpec.h +0 -4
  11. package/ios/generated/ReactCodegen/RNBlueBreezeSpecJSI.h +0 -78
  12. package/lib/commonjs/NativeBlueBreeze.js.map +1 -1
  13. package/lib/commonjs/implementation/bluebreeze_characteristic.js +5 -6
  14. package/lib/commonjs/implementation/bluebreeze_characteristic.js.map +1 -1
  15. package/lib/commonjs/implementation/bluebreeze_observable.js +53 -0
  16. package/lib/commonjs/implementation/bluebreeze_observable.js.map +1 -0
  17. package/lib/commonjs/implementation/native_bridge.js +7 -20
  18. package/lib/commonjs/implementation/native_bridge.js.map +1 -1
  19. package/lib/commonjs/index.js +11 -4
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/module/NativeBlueBreeze.js.map +1 -1
  22. package/lib/module/implementation/bluebreeze_characteristic.js +6 -7
  23. package/lib/module/implementation/bluebreeze_characteristic.js.map +1 -1
  24. package/lib/module/implementation/bluebreeze_observable.js +49 -0
  25. package/lib/module/implementation/bluebreeze_observable.js.map +1 -0
  26. package/lib/module/implementation/native_bridge.js +6 -18
  27. package/lib/module/implementation/native_bridge.js.map +1 -1
  28. package/lib/module/index.js +7 -6
  29. package/lib/module/index.js.map +1 -1
  30. package/lib/typescript/commonjs/src/NativeBlueBreeze.d.ts +0 -8
  31. package/lib/typescript/commonjs/src/NativeBlueBreeze.d.ts.map +1 -1
  32. package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts +7 -7
  33. package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts.map +1 -1
  34. package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts +4 -4
  35. package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts.map +1 -1
  36. package/lib/typescript/commonjs/src/implementation/bluebreeze_observable.d.ts +32 -0
  37. package/lib/typescript/commonjs/src/implementation/bluebreeze_observable.d.ts.map +1 -0
  38. package/lib/typescript/commonjs/src/implementation/native_bridge.d.ts +6 -7
  39. package/lib/typescript/commonjs/src/implementation/native_bridge.d.ts.map +1 -1
  40. package/lib/typescript/commonjs/src/index.d.ts +7 -6
  41. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  42. package/lib/typescript/module/src/NativeBlueBreeze.d.ts +0 -8
  43. package/lib/typescript/module/src/NativeBlueBreeze.d.ts.map +1 -1
  44. package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts +7 -7
  45. package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts.map +1 -1
  46. package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts +4 -4
  47. package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts.map +1 -1
  48. package/lib/typescript/module/src/implementation/bluebreeze_observable.d.ts +32 -0
  49. package/lib/typescript/module/src/implementation/bluebreeze_observable.d.ts.map +1 -0
  50. package/lib/typescript/module/src/implementation/native_bridge.d.ts +6 -7
  51. package/lib/typescript/module/src/implementation/native_bridge.d.ts.map +1 -1
  52. package/lib/typescript/module/src/index.d.ts +7 -6
  53. package/lib/typescript/module/src/index.d.ts.map +1 -1
  54. package/package.json +1 -1
  55. package/react-native-bluebreeze.podspec +1 -1
  56. package/src/NativeBlueBreeze.ts +0 -11
  57. package/src/implementation/bluebreeze_characteristic.ts +8 -11
  58. package/src/implementation/bluebreeze_device.ts +4 -4
  59. package/src/implementation/bluebreeze_observable.ts +48 -0
  60. package/src/implementation/native_bridge.ts +20 -52
  61. package/src/index.tsx +7 -5
@@ -1,4 +1,4 @@
1
- import type { StateValueEmitter } from "react-native-value-emitter"
1
+ import type { BBObservable } from "./bluebreeze_observable"
2
2
  import type { BBDeviceConnectionStatus } from "./bluebreeze_connection_status"
3
3
  import type { BBService } from "./bluebreeze_service"
4
4
  import {
@@ -47,16 +47,16 @@ export class BBDevice {
47
47
  name?: string
48
48
 
49
49
  /** The device's services discovered so far. Empty until {@link discoverServices} has been called and completed. */
50
- services: StateValueEmitter<BBService[]>
50
+ services: BBObservable<BBService[]>
51
51
 
52
52
  /** Current connection status of the device. */
53
- connectionStatus: StateValueEmitter<BBDeviceConnectionStatus>
53
+ connectionStatus: BBObservable<BBDeviceConnectionStatus>
54
54
 
55
55
  /**
56
56
  * The negotiated ATT MTU in bytes. Stays at the platform default until {@link requestMTU}
57
57
  * has been called and resolved.
58
58
  */
59
- mtu: StateValueEmitter<number>
59
+ mtu: BBObservable<number>
60
60
 
61
61
  /**
62
62
  * Connects the device.
@@ -0,0 +1,48 @@
1
+ import { StateValueEmitter, ValueEmitter } from 'react-native-value-emitter'
2
+
3
+ /**
4
+ * An observable value, used for every piece of state BlueBreeze exposes (`BlueBreeze.state`,
5
+ * {@link BBDevice.connectionStatus}, {@link BBCharacteristic.data}, and so on).
6
+ *
7
+ * ```ts
8
+ * characteristic.data.value // the current value
9
+ * characteristic.data.values.onValue((data) => {}) // the current value, then new values
10
+ * characteristic.data.updates.onValue((data) => {}) // only new values
11
+ * ```
12
+ *
13
+ * Streams work with `useValueEmitter` from `react-native-value-emitter`, which unsubscribes
14
+ * automatically when the component unmounts.
15
+ */
16
+ export class BBObservable<T> {
17
+ constructor(initialValue: T) {
18
+ this._values = new StateValueEmitter<T>(initialValue)
19
+ }
20
+
21
+ private readonly _values: StateValueEmitter<T>
22
+ private readonly _updates = new ValueEmitter<T>()
23
+
24
+ /** The current value. */
25
+ get value(): T {
26
+ return this._values.value
27
+ }
28
+
29
+ /** A stream that calls each handler with the current {@link value} first, then with every new one. */
30
+ get values(): ValueEmitter<T> {
31
+ return this._values
32
+ }
33
+
34
+ /** A stream of only the values published from now on, without the current {@link value}. */
35
+ get updates(): ValueEmitter<T> {
36
+ return this._updates
37
+ }
38
+
39
+ /**
40
+ * Publishes a new value. Internal to BlueBreeze: called when the native module reports an
41
+ * update, not meant to be called by apps.
42
+ * @internal
43
+ */
44
+ add(value: T) {
45
+ this._values.add(value)
46
+ this._updates.add(value)
47
+ }
48
+ }
@@ -1,6 +1,6 @@
1
- import { StateValueEmitter, ValueEmitter } from 'react-native-value-emitter'
2
1
  import NativeBlueBreeze, { type SpecDeviceCharacteristic, type SpecDeviceService } from '../NativeBlueBreeze'
3
2
  import { BBCharacteristic } from './bluebreeze_characteristic'
3
+ import { BBObservable } from './bluebreeze_observable'
4
4
  import type { BBDeviceConnectionStatus } from './bluebreeze_connection_status'
5
5
  import { BBService } from './bluebreeze_service'
6
6
 
@@ -22,13 +22,13 @@ function mapGetOrSet<T>(map: Map<string, T>, key: string, defaultValue: () => T)
22
22
 
23
23
  // Device services
24
24
 
25
- const _deviceServices = new Map<string, StateValueEmitter<BBService[]>>()
25
+ const _deviceServices = new Map<string, BBObservable<BBService[]>>()
26
26
 
27
- export const deviceServices = (id: string): StateValueEmitter<BBService[]> => {
27
+ export const deviceServices = (id: string): BBObservable<BBService[]> => {
28
28
  return mapGetOrSet(
29
29
  _deviceServices,
30
30
  id,
31
- () => new StateValueEmitter<BBService[]>(
31
+ () => new BBObservable<BBService[]>(
32
32
  NativeBlueBreeze.deviceServices(id).map(
33
33
  (s) => convertDeviceService(id, s)
34
34
  )
@@ -44,13 +44,13 @@ NativeBlueBreeze.deviceServicesEmitter((value) => {
44
44
 
45
45
  // Device connection status
46
46
 
47
- const _deviceConnectionStatus = new Map<string, StateValueEmitter<BBDeviceConnectionStatus>>()
47
+ const _deviceConnectionStatus = new Map<string, BBObservable<BBDeviceConnectionStatus>>()
48
48
 
49
- export const deviceConnectionStatus = (id: string): StateValueEmitter<BBDeviceConnectionStatus> => {
49
+ export const deviceConnectionStatus = (id: string): BBObservable<BBDeviceConnectionStatus> => {
50
50
  return mapGetOrSet(
51
51
  _deviceConnectionStatus,
52
52
  id,
53
- () => new StateValueEmitter<BBDeviceConnectionStatus>(
53
+ () => new BBObservable<BBDeviceConnectionStatus>(
54
54
  NativeBlueBreeze.deviceConnectionStatus(id) as BBDeviceConnectionStatus
55
55
  )
56
56
  )
@@ -62,13 +62,13 @@ NativeBlueBreeze.deviceConnectionStatusEmitter((value) => {
62
62
 
63
63
  // Device MTU
64
64
 
65
- const _deviceMTU = new Map<string, StateValueEmitter<number>>()
65
+ const _deviceMTU = new Map<string, BBObservable<number>>()
66
66
 
67
- export const deviceMTU = (id: string): StateValueEmitter<number> => {
67
+ export const deviceMTU = (id: string): BBObservable<number> => {
68
68
  return mapGetOrSet(
69
69
  _deviceMTU,
70
70
  id,
71
- () => new StateValueEmitter<number>(
71
+ () => new BBObservable<number>(
72
72
  NativeBlueBreeze.deviceMTU(id)
73
73
  )
74
74
  )
@@ -90,29 +90,29 @@ export const deviceRequestMTU = NativeBlueBreeze.deviceRequestMTU
90
90
 
91
91
  // Device characteristic data
92
92
 
93
- const _deviceCharacteristicData = new Map<string, Map<string, Map<string, StateValueEmitter<number[]>>>>()
93
+ const _deviceCharacteristicData = new Map<string, Map<string, Map<string, BBObservable<number[]>>>>()
94
94
 
95
95
  export const deviceCharacteristicData = (
96
96
  id: string,
97
97
  serviceId: string,
98
98
  characteristicId: string
99
- ): StateValueEmitter<number[]> => {
99
+ ): BBObservable<number[]> => {
100
100
  const deviceMap = mapGetOrSet(
101
101
  _deviceCharacteristicData,
102
102
  id,
103
- () => new Map<string, Map<string, StateValueEmitter<number[]>>>()
103
+ () => new Map<string, Map<string, BBObservable<number[]>>>()
104
104
  )
105
105
 
106
106
  const serviceMap = mapGetOrSet(
107
107
  deviceMap,
108
108
  serviceId,
109
- () => new Map<string, StateValueEmitter<number[]>>()
109
+ () => new Map<string, BBObservable<number[]>>()
110
110
  )
111
111
 
112
112
  return mapGetOrSet(
113
113
  serviceMap,
114
114
  characteristicId,
115
- () => new StateValueEmitter<number[]>(
115
+ () => new BBObservable<number[]>(
116
116
  NativeBlueBreeze.deviceCharacteristicData(id, serviceId, characteristicId)
117
117
  )
118
118
  )
@@ -124,29 +124,29 @@ NativeBlueBreeze.deviceCharacteristicDataEmitter((value) => {
124
124
 
125
125
  // Device characteristic notify enabled
126
126
 
127
- const _deviceCharacteristicNotifyEnabled = new Map<string, Map<string, Map<string, StateValueEmitter<boolean>>>>()
127
+ const _deviceCharacteristicNotifyEnabled = new Map<string, Map<string, Map<string, BBObservable<boolean>>>>()
128
128
 
129
129
  export const deviceCharacteristicNotifyEnabled = (
130
130
  id: string,
131
131
  serviceId: string,
132
132
  characteristicId: string
133
- ): StateValueEmitter<boolean> => {
133
+ ): BBObservable<boolean> => {
134
134
  const deviceMap = mapGetOrSet(
135
135
  _deviceCharacteristicNotifyEnabled,
136
136
  id,
137
- () => new Map<string, Map<string, StateValueEmitter<boolean>>>()
137
+ () => new Map<string, Map<string, BBObservable<boolean>>>()
138
138
  )
139
139
 
140
140
  const serviceMap = mapGetOrSet(
141
141
  deviceMap,
142
142
  serviceId,
143
- () => new Map<string, StateValueEmitter<boolean>>()
143
+ () => new Map<string, BBObservable<boolean>>()
144
144
  )
145
145
 
146
146
  return mapGetOrSet(
147
147
  serviceMap,
148
148
  characteristicId,
149
- () => new StateValueEmitter<boolean>(
149
+ () => new BBObservable<boolean>(
150
150
  NativeBlueBreeze.deviceCharacteristicNotifyEnabled(id, serviceId, characteristicId)
151
151
  )
152
152
  )
@@ -156,38 +156,6 @@ NativeBlueBreeze.deviceCharacteristicNotifyEnabledEmitter((value) => {
156
156
  deviceCharacteristicNotifyEnabled(value.id, value.serviceId, value.characteristicId).add(value.value)
157
157
  })
158
158
 
159
- // Device characteristic notifications
160
-
161
- const _deviceCharacteristicNotifications = new Map<string, Map<string, Map<string, ValueEmitter<number[]>>>>()
162
-
163
- export const deviceCharacteristicNotifications = (
164
- id: string,
165
- serviceId: string,
166
- characteristicId: string
167
- ): ValueEmitter<number[]> => {
168
- const deviceMap = mapGetOrSet(
169
- _deviceCharacteristicNotifications,
170
- id,
171
- () => new Map<string, Map<string, ValueEmitter<number[]>>>()
172
- )
173
-
174
- const serviceMap = mapGetOrSet(
175
- deviceMap,
176
- serviceId,
177
- () => new Map<string, ValueEmitter<number[]>>()
178
- )
179
-
180
- return mapGetOrSet(
181
- serviceMap,
182
- characteristicId,
183
- () => new ValueEmitter<number[]>()
184
- )
185
- }
186
-
187
- NativeBlueBreeze.deviceCharacteristicNotificationsEmitter((value) => {
188
- deviceCharacteristicNotifications(value.id, value.serviceId, value.characteristicId).add(value.value)
189
- })
190
-
191
159
  // Device characteristic operations
192
160
 
193
161
  export const deviceCharacteristicRead = NativeBlueBreeze.deviceCharacteristicRead
package/src/index.tsx CHANGED
@@ -1,7 +1,8 @@
1
- import { StateValueEmitter, ValueEmitter } from 'react-native-value-emitter'
1
+ import { ValueEmitter } from 'react-native-value-emitter'
2
2
  import type { BBAuthorization } from './implementation/bluebreeze_authorization'
3
3
  import { BBCharacteristic } from './implementation/bluebreeze_characteristic'
4
4
  import { BBDevice } from './implementation/bluebreeze_device'
5
+ import { BBObservable } from './implementation/bluebreeze_observable'
5
6
  import { BBScanResult } from './implementation/bluebreeze_scan_result'
6
7
  import { BBService } from './implementation/bluebreeze_service'
7
8
  import type { BBState } from './implementation/bluebreeze_state'
@@ -10,7 +11,7 @@ import NativeBlueBreeze, { type SpecDevice, type SpecScanResult } from './Native
10
11
  // State
11
12
 
12
13
  /** The power/availability state of the device's Bluetooth adapter. */
13
- const state = new StateValueEmitter<BBState>(NativeBlueBreeze.state() as BBState)
14
+ const state = new BBObservable<BBState>(NativeBlueBreeze.state() as BBState)
14
15
 
15
16
  NativeBlueBreeze.stateEmitter((value) => {
16
17
  state.add(value as BBState)
@@ -19,7 +20,7 @@ NativeBlueBreeze.stateEmitter((value) => {
19
20
  // Authorization to use BLE
20
21
 
21
22
  /** Whether this app is currently allowed to use Bluetooth. */
22
- const authorizationStatus = new StateValueEmitter<BBAuthorization>(NativeBlueBreeze.authorizationStatus() as BBAuthorization)
23
+ const authorizationStatus = new BBObservable<BBAuthorization>(NativeBlueBreeze.authorizationStatus() as BBAuthorization)
23
24
 
24
25
  NativeBlueBreeze.authorizationStatusEmitter((value) => {
25
26
  authorizationStatus.add(value as BBAuthorization)
@@ -34,7 +35,7 @@ const authorizationOpenSettings = NativeBlueBreeze.authorizationOpenSettings
34
35
  // Scanning
35
36
 
36
37
  /** Whether a BLE scan is currently running. */
37
- const scanEnabled = new StateValueEmitter<boolean>(NativeBlueBreeze.scanEnabled())
38
+ const scanEnabled = new BBObservable<boolean>(NativeBlueBreeze.scanEnabled())
38
39
 
39
40
  NativeBlueBreeze.scanEnabledEmitter((value) => {
40
41
  scanEnabled.add(value)
@@ -72,7 +73,7 @@ const scanStop = NativeBlueBreeze.scanStop
72
73
  * platform device ID. A device is added here the moment it's first seen in a scan result and
73
74
  * stays here for the app's lifetime.
74
75
  */
75
- const devices = new StateValueEmitter<Map<string, BBDevice>>(
76
+ const devices = new BBObservable<Map<string, BBDevice>>(
76
77
  new Map(
77
78
  NativeBlueBreeze.devices().map((d) => [
78
79
  d.id,
@@ -134,6 +135,7 @@ export default {
134
135
  export {
135
136
  BBCharacteristic,
136
137
  BBDevice,
138
+ BBObservable,
137
139
  BBScanResult,
138
140
  BBService,
139
141
  type BBAuthorization,