react-native-bluebreeze 1.0.1 → 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.
- package/README.md +31 -4
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/bluebreeze/BlueBreezeModule.kt +15 -14
- package/ios/BlueBreezeImpl.swift +14 -14
- package/lib/commonjs/implementation/bluebreeze_characteristic.js +5 -2
- package/lib/commonjs/implementation/bluebreeze_characteristic.js.map +1 -1
- package/lib/commonjs/implementation/bluebreeze_observable.js +53 -0
- package/lib/commonjs/implementation/bluebreeze_observable.js.map +1 -0
- package/lib/commonjs/implementation/native_bridge.js +18 -18
- package/lib/commonjs/implementation/native_bridge.js.map +1 -1
- package/lib/commonjs/index.js +11 -4
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/implementation/bluebreeze_characteristic.js +5 -2
- package/lib/module/implementation/bluebreeze_characteristic.js.map +1 -1
- package/lib/module/implementation/bluebreeze_observable.js +49 -0
- package/lib/module/implementation/bluebreeze_observable.js.map +1 -0
- package/lib/module/implementation/native_bridge.js +17 -17
- package/lib/module/implementation/native_bridge.js.map +1 -1
- package/lib/module/index.js +7 -6
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts +7 -4
- package/lib/typescript/commonjs/src/implementation/bluebreeze_characteristic.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts +4 -4
- package/lib/typescript/commonjs/src/implementation/bluebreeze_device.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/implementation/bluebreeze_observable.d.ts +32 -0
- package/lib/typescript/commonjs/src/implementation/bluebreeze_observable.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/implementation/native_bridge.d.ts +6 -6
- package/lib/typescript/commonjs/src/implementation/native_bridge.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/index.d.ts +7 -6
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
- package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts +7 -4
- package/lib/typescript/module/src/implementation/bluebreeze_characteristic.d.ts.map +1 -1
- package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts +4 -4
- package/lib/typescript/module/src/implementation/bluebreeze_device.d.ts.map +1 -1
- package/lib/typescript/module/src/implementation/bluebreeze_observable.d.ts +32 -0
- package/lib/typescript/module/src/implementation/bluebreeze_observable.d.ts.map +1 -0
- package/lib/typescript/module/src/implementation/native_bridge.d.ts +6 -6
- package/lib/typescript/module/src/implementation/native_bridge.d.ts.map +1 -1
- package/lib/typescript/module/src/index.d.ts +7 -6
- package/lib/typescript/module/src/index.d.ts.map +1 -1
- package/package.json +3 -2
- package/react-native-bluebreeze.podspec +1 -1
- package/src/implementation/bluebreeze_characteristic.ts +9 -5
- package/src/implementation/bluebreeze_device.ts +4 -4
- package/src/implementation/bluebreeze_observable.ts +48 -0
- package/src/implementation/native_bridge.ts +32 -32
- package/src/index.tsx +7 -5
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BBObservable } from "./bluebreeze_observable"
|
|
2
2
|
import {
|
|
3
3
|
deviceCharacteristicData,
|
|
4
4
|
deviceCharacteristicNotifyEnabled,
|
|
@@ -27,7 +27,8 @@ export class BBCharacteristic {
|
|
|
27
27
|
this.properties = properties
|
|
28
28
|
|
|
29
29
|
this.data = deviceCharacteristicData(deviceId, serviceId, id)
|
|
30
|
-
|
|
30
|
+
|
|
31
|
+
this.isNotifying = deviceCharacteristicNotifyEnabled(deviceId, serviceId, id)
|
|
31
32
|
|
|
32
33
|
this.read = () => deviceCharacteristicRead(deviceId, serviceId, id)
|
|
33
34
|
this.write = (data: number[], withResponse: boolean) => deviceCharacteristicWrite(deviceId, serviceId, id, data, withResponse)
|
|
@@ -44,11 +45,14 @@ export class BBCharacteristic {
|
|
|
44
45
|
/** The GATT properties this characteristic declares. */
|
|
45
46
|
properties: string[]
|
|
46
47
|
|
|
47
|
-
/**
|
|
48
|
-
|
|
48
|
+
/**
|
|
49
|
+
* The last value read or notified for this characteristic. Empty until first populated.
|
|
50
|
+
* Use `data.updates` to receive only the values that arrive from now on.
|
|
51
|
+
*/
|
|
52
|
+
data: BBObservable<number[]>
|
|
49
53
|
|
|
50
54
|
/** Whether this app is currently subscribed to notifications for this characteristic. */
|
|
51
|
-
|
|
55
|
+
isNotifying: BBObservable<boolean>
|
|
52
56
|
|
|
53
57
|
/**
|
|
54
58
|
* Reads this characteristic's current value, also updating {@link data}.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
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:
|
|
50
|
+
services: BBObservable<BBService[]>
|
|
51
51
|
|
|
52
52
|
/** Current connection status of the device. */
|
|
53
|
-
connectionStatus:
|
|
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:
|
|
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 } 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,
|
|
25
|
+
const _deviceServices = new Map<string, BBObservable<BBService[]>>()
|
|
26
26
|
|
|
27
|
-
export const deviceServices = (id: string):
|
|
27
|
+
export const deviceServices = (id: string): BBObservable<BBService[]> => {
|
|
28
28
|
return mapGetOrSet(
|
|
29
29
|
_deviceServices,
|
|
30
30
|
id,
|
|
31
|
-
() => new
|
|
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,
|
|
47
|
+
const _deviceConnectionStatus = new Map<string, BBObservable<BBDeviceConnectionStatus>>()
|
|
48
48
|
|
|
49
|
-
export const deviceConnectionStatus = (id: string):
|
|
49
|
+
export const deviceConnectionStatus = (id: string): BBObservable<BBDeviceConnectionStatus> => {
|
|
50
50
|
return mapGetOrSet(
|
|
51
51
|
_deviceConnectionStatus,
|
|
52
52
|
id,
|
|
53
|
-
() => new
|
|
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,
|
|
65
|
+
const _deviceMTU = new Map<string, BBObservable<number>>()
|
|
66
66
|
|
|
67
|
-
export const deviceMTU = (id: string):
|
|
67
|
+
export const deviceMTU = (id: string): BBObservable<number> => {
|
|
68
68
|
return mapGetOrSet(
|
|
69
69
|
_deviceMTU,
|
|
70
70
|
id,
|
|
71
|
-
() => new
|
|
71
|
+
() => new BBObservable<number>(
|
|
72
72
|
NativeBlueBreeze.deviceMTU(id)
|
|
73
73
|
)
|
|
74
74
|
)
|
|
@@ -88,72 +88,72 @@ export const deviceDiscoverServices = NativeBlueBreeze.deviceDiscoverServices
|
|
|
88
88
|
|
|
89
89
|
export const deviceRequestMTU = NativeBlueBreeze.deviceRequestMTU
|
|
90
90
|
|
|
91
|
-
// Device characteristic
|
|
91
|
+
// Device characteristic data
|
|
92
92
|
|
|
93
|
-
const
|
|
93
|
+
const _deviceCharacteristicData = new Map<string, Map<string, Map<string, BBObservable<number[]>>>>()
|
|
94
94
|
|
|
95
|
-
export const
|
|
95
|
+
export const deviceCharacteristicData = (
|
|
96
96
|
id: string,
|
|
97
97
|
serviceId: string,
|
|
98
98
|
characteristicId: string
|
|
99
|
-
):
|
|
99
|
+
): BBObservable<number[]> => {
|
|
100
100
|
const deviceMap = mapGetOrSet(
|
|
101
|
-
|
|
101
|
+
_deviceCharacteristicData,
|
|
102
102
|
id,
|
|
103
|
-
() => new Map<string, Map<string,
|
|
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,
|
|
109
|
+
() => new Map<string, BBObservable<number[]>>()
|
|
110
110
|
)
|
|
111
111
|
|
|
112
112
|
return mapGetOrSet(
|
|
113
113
|
serviceMap,
|
|
114
114
|
characteristicId,
|
|
115
|
-
() => new
|
|
116
|
-
NativeBlueBreeze.
|
|
115
|
+
() => new BBObservable<number[]>(
|
|
116
|
+
NativeBlueBreeze.deviceCharacteristicData(id, serviceId, characteristicId)
|
|
117
117
|
)
|
|
118
118
|
)
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
NativeBlueBreeze.
|
|
122
|
-
|
|
121
|
+
NativeBlueBreeze.deviceCharacteristicDataEmitter((value) => {
|
|
122
|
+
deviceCharacteristicData(value.id, value.serviceId, value.characteristicId).add(value.value)
|
|
123
123
|
})
|
|
124
124
|
|
|
125
|
-
// Device characteristic
|
|
125
|
+
// Device characteristic notify enabled
|
|
126
126
|
|
|
127
|
-
const
|
|
127
|
+
const _deviceCharacteristicNotifyEnabled = new Map<string, Map<string, Map<string, BBObservable<boolean>>>>()
|
|
128
128
|
|
|
129
|
-
export const
|
|
129
|
+
export const deviceCharacteristicNotifyEnabled = (
|
|
130
130
|
id: string,
|
|
131
131
|
serviceId: string,
|
|
132
132
|
characteristicId: string
|
|
133
|
-
):
|
|
133
|
+
): BBObservable<boolean> => {
|
|
134
134
|
const deviceMap = mapGetOrSet(
|
|
135
|
-
|
|
135
|
+
_deviceCharacteristicNotifyEnabled,
|
|
136
136
|
id,
|
|
137
|
-
() => new Map<string, Map<string,
|
|
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,
|
|
143
|
+
() => new Map<string, BBObservable<boolean>>()
|
|
144
144
|
)
|
|
145
145
|
|
|
146
146
|
return mapGetOrSet(
|
|
147
147
|
serviceMap,
|
|
148
148
|
characteristicId,
|
|
149
|
-
() => new
|
|
150
|
-
NativeBlueBreeze.
|
|
149
|
+
() => new BBObservable<boolean>(
|
|
150
|
+
NativeBlueBreeze.deviceCharacteristicNotifyEnabled(id, serviceId, characteristicId)
|
|
151
151
|
)
|
|
152
152
|
)
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
-
NativeBlueBreeze.
|
|
156
|
-
|
|
155
|
+
NativeBlueBreeze.deviceCharacteristicNotifyEnabledEmitter((value) => {
|
|
156
|
+
deviceCharacteristicNotifyEnabled(value.id, value.serviceId, value.characteristicId).add(value.value)
|
|
157
157
|
})
|
|
158
158
|
|
|
159
159
|
// Device characteristic operations
|
package/src/index.tsx
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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,
|