@openfitnessprotocol/ofp 0.0.0-stage → 0.2.2
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/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +72 -2
- package/adapters/noble.d.ts +10 -0
- package/adapters/noble.js +13 -0
- package/adapters/react-native-ble-manager.d.ts +33 -0
- package/adapters/react-native-ble-manager.js +84 -0
- package/core/ofp_core.asm.d.ts +2 -0
- package/core/ofp_core.asm.js +56436 -0
- package/core/ofp_core.wasm +0 -0
- package/index.d.ts +148 -0
- package/index.js +410 -0
- package/package.json +65 -4
- package/registry.bundle.json +1 -0
- package/registry.d.ts +3 -0
- package/registry.js +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Open Fitness Protocol (OFP)
|
|
2
|
+
Copyright 2026 TrainerDay Inc and the Open Fitness Protocol contributors
|
|
3
|
+
|
|
4
|
+
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
"Open Fitness Protocol" and "OFP" are trademarks of TrainerDay Inc. The license does not grant
|
|
7
|
+
permission to use them (Apache License 2.0, section 6). What you may do with the names is in
|
|
8
|
+
TRADEMARKS.md.
|
package/README.md
CHANGED
|
@@ -1,3 +1,73 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @openfitnessprotocol/ofp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Open Fitness Protocol for JavaScript apps: read fitness devices from your app's own Bluetooth (heart rate
|
|
4
|
+
straps, power meters, speed and cadence sensors, FTMS bikes, rowers and treadmills, and devices the
|
|
5
|
+
community adds as tables). Reading only: OFP never controls a device. Apache-2.0, no dependencies.
|
|
6
|
+
Project and device list: https://openfitnessprotocol.org
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @openfitnessprotocol/ofp
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
| Import | What it is |
|
|
13
|
+
|---|---|
|
|
14
|
+
| `@openfitnessprotocol/ofp` | `Ofp` (the four hooks), `wasmEngine`, `moduleEngine`, helpers |
|
|
15
|
+
| `@openfitnessprotocol/ofp/core/ofp_core.wasm` | the core as WebAssembly (Electron, browsers, Node) |
|
|
16
|
+
| `@openfitnessprotocol/ofp/core/js` | the same core as plain JavaScript, for runtimes without WebAssembly (React Native's Hermes): `moduleEngine(require(...))` |
|
|
17
|
+
| `@openfitnessprotocol/ofp/registry` | the device tables, as text (`export default`); `.../registry.bundle.json` is the same file |
|
|
18
|
+
| `@openfitnessprotocol/ofp/adapters/noble`, `.../react-native-ble-manager` | helpers for those Bluetooth libraries |
|
|
19
|
+
|
|
20
|
+
New devices come with a new version of this package: no code of yours changes.
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { Ofp, wasmEngine } from '@openfitnessprotocol/ofp'
|
|
26
|
+
|
|
27
|
+
const ofp = new Ofp({
|
|
28
|
+
engine: wasmEngine(wasmBytes), // or moduleEngine(...) where there is no WebAssembly
|
|
29
|
+
bundle: registryBundleJson, // the compiled registry (text)
|
|
30
|
+
iRead: ['180d', '1818', '1816', '1826'], // 1. the Bluetooth services your app reads by itself
|
|
31
|
+
remembered: savedIds, // devices the rider used before
|
|
32
|
+
on: {
|
|
33
|
+
device: (d) => addToDeviceList(d), // 3. a device OFP recognised: { id, name, fields, … }
|
|
34
|
+
values: (v) => show(v.basic, v.fields), // its readings: basic = { power, cadence, speed, heartRate }
|
|
35
|
+
inUse: (id, auto) => markConnected(id),
|
|
36
|
+
lost: (id) => markDisconnected(id), // silent for a minute; comes back by itself if remembered
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
scanner.on('advertisement', (a) => // 2. every advertisement, before your own filtering
|
|
41
|
+
ofp.advertisement({ deviceId: a.id, name: a.name, rssi: a.rssi, manufacturerData: a.manufacturerData }))
|
|
42
|
+
|
|
43
|
+
connectButton.onclick = () => ofp.use(id) // 4. the rider picked it
|
|
44
|
+
disconnectButton.onclick = () => ofp.release(id)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
- `manufacturerData` is the raw manufacturer data **with the 2-byte company id first**, as bytes or
|
|
48
|
+
hex. Some Bluetooth libraries hand it over without the id (React Native ones, keyed by id): put it
|
|
49
|
+
back in front, low byte first. `base64ToHex` and `toHex` are exported for convenience.
|
|
50
|
+
- `values.fields` has every field the device sends, by key; `device.fields` describes each one
|
|
51
|
+
(name, unit, type, range, kind), so an app can show a field it has never heard of.
|
|
52
|
+
- **Radio gaps.** A phone's or computer's radio misses advertisements for seconds at a time. OFP
|
|
53
|
+
keeps the last values for `minStaleMs` (default 10 s) before a device reads zero, and can repeat
|
|
54
|
+
them every `repeatMs` (default 1 s) for apps that treat "no event" as "no data"; set `repeatMs: 0`
|
|
55
|
+
if your app polls.
|
|
56
|
+
- **The scan must keep running** while a broadcast-only device is in use: its advertisements are
|
|
57
|
+
its data. That part is your app's.
|
|
58
|
+
- A failure inside OFP turns it off (`on.error`); it never throws into your scanner.
|
|
59
|
+
- Helpers the two reference integrations use: `ofp.scanNeed()`, `on.radio` (how well the radio hears a
|
|
60
|
+
device, once a minute), `reofferMs`, `ofp.hasHeartRate(id)`, `selfTest(engine, bundle)`, and the
|
|
61
|
+
adapters in `adapters/` for noble and react-native-ble-manager.
|
|
62
|
+
|
|
63
|
+
## The two forms of the core
|
|
64
|
+
|
|
65
|
+
| Runtime | Core | Engine |
|
|
66
|
+
|---|---|---|
|
|
67
|
+
| Node, Electron, browsers | `@openfitnessprotocol/ofp/core/ofp_core.wasm` | `wasmEngine(bytes)` |
|
|
68
|
+
| React Native (Hermes has no WebAssembly) | `@openfitnessprotocol/ofp/core/js`, the same core as plain JavaScript | `moduleEngine(core)` |
|
|
69
|
+
|
|
70
|
+
Quick starts for Electron and React Native: https://openfitnessprotocol.org/developers. In the repository
|
|
71
|
+
both are built by `scripts/build.sh` into `dist/`.
|
|
72
|
+
|
|
73
|
+
`npm test` runs the same tests against both.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { OfpAdvertisement } from '../index'
|
|
2
|
+
|
|
3
|
+
/** A noble peripheral (from the 'discover' event) as an OFP advertisement, or null when it carries no data. */
|
|
4
|
+
export function advertisementFrom(peripheral: {
|
|
5
|
+
id: string
|
|
6
|
+
rssi?: number
|
|
7
|
+
advertisement?: { localName?: string; manufacturerData?: Uint8Array | null }
|
|
8
|
+
}): OfpAdvertisement | null
|
|
9
|
+
/** Arguments for `noble.startScanning`: no service filter, with repeats. */
|
|
10
|
+
export const SCAN_ARGS: [string[], boolean]
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// OFP ↔ noble (@stoprocent/noble, @abandonware/noble): Node and Electron.
|
|
2
|
+
// noble hands over the manufacturer data raw, company id first: exactly what OFP wants.
|
|
3
|
+
|
|
4
|
+
/** A noble peripheral (from the 'discover' event) as an OFP advertisement, or null when it carries no data. */
|
|
5
|
+
export function advertisementFrom(peripheral) {
|
|
6
|
+
const adv = peripheral && peripheral.advertisement;
|
|
7
|
+
const data = adv && adv.manufacturerData;
|
|
8
|
+
if (!data || data.length < 2) return null;
|
|
9
|
+
return { deviceId: peripheral.id, name: adv.localName || null, rssi: peripheral.rssi, manufacturerData: data };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** noble must scan with repeats, or it reports each device once: `noble.startScanning([], true)`. */
|
|
13
|
+
export const SCAN_ARGS = [[], true];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { OfpAdvertisement } from '../index'
|
|
2
|
+
|
|
3
|
+
interface ScannedPeripheral {
|
|
4
|
+
id: string
|
|
5
|
+
name?: string
|
|
6
|
+
rssi?: number
|
|
7
|
+
advertising?: {
|
|
8
|
+
localName?: string
|
|
9
|
+
serviceUUIDs?: string[]
|
|
10
|
+
overflowServiceUUIDs?: string[]
|
|
11
|
+
manufacturerData?: Record<string, { bytes?: number[] } | undefined>
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The OFP advertisements in one scan result: one per company id the registry knows. */
|
|
16
|
+
export function advertisementsFrom(peripheral: ScannedPeripheral, companyIds: number[]): OfpAdvertisement[]
|
|
17
|
+
/**
|
|
18
|
+
* Extra options for `BleManager.scan({...})`, to be spread after the app's own.
|
|
19
|
+
* Android: the company-id filter, and continuous listening while a device is in use.
|
|
20
|
+
* iOS (`platform` 'ios'): an open scan with repeats; drop the results `appCanRead` says no to.
|
|
21
|
+
*/
|
|
22
|
+
export function scanOptionsFor(
|
|
23
|
+
ofp: { companyIds: number[] },
|
|
24
|
+
need: 'in_use' | 'waiting' | null,
|
|
25
|
+
lowLatencyMode?: number,
|
|
26
|
+
platform?: 'ios' | 'android' | string,
|
|
27
|
+
): { manufacturerData?: { manufacturerId: number }; scanMode?: number; serviceUUIDs?: string[]; allowDuplicates?: boolean }
|
|
28
|
+
/** Android degrades a scan that runs longer than 30 minutes: restart before this. */
|
|
29
|
+
export const MAX_SCAN_SECONDS: number
|
|
30
|
+
/** True when the peripheral advertises one of the services the app reads itself (what the app's filtered scan lets through). */
|
|
31
|
+
export function appCanRead(peripheral: ScannedPeripheral, servicesTheAppReads: string[]): boolean
|
|
32
|
+
/** True when none of the peripheral's advertised services is one the app reads itself. */
|
|
33
|
+
export function onlyOfpCanRead(peripheral: ScannedPeripheral, servicesTheAppReads: string[]): boolean
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// OFP ↔ react-native-ble-manager (React Native).
|
|
2
|
+
//
|
|
3
|
+
// Two things differ from what OFP wants:
|
|
4
|
+
// - the manufacturer data arrives keyed by company id ("0102") WITHOUT the id in the bytes (the same on
|
|
5
|
+
// Android and iOS);
|
|
6
|
+
// - apps usually scan filtered to fitness services, which a broadcast-only device does not advertise.
|
|
7
|
+
// Android lets such a device through by its company id. iOS has no such filter: there the scan is opened
|
|
8
|
+
// to every device and the app's own filter is applied to the results (`appCanRead`).
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The OFP advertisements in one scan result (`onDiscoverPeripheral`): one per company id the
|
|
12
|
+
* registry knows. Empty when the peripheral is nothing OFP could read.
|
|
13
|
+
*/
|
|
14
|
+
export function advertisementsFrom(peripheral, companyIds) {
|
|
15
|
+
const adv = peripheral && peripheral.advertising;
|
|
16
|
+
const md = adv && adv.manufacturerData;
|
|
17
|
+
if (!md) return [];
|
|
18
|
+
const out = [];
|
|
19
|
+
for (const key of Object.keys(md)) {
|
|
20
|
+
const companyId = parseInt(key, 16);
|
|
21
|
+
if (!companyIds.includes(companyId)) continue;
|
|
22
|
+
const bytes = (md[key] && md[key].bytes) || [];
|
|
23
|
+
out.push({
|
|
24
|
+
deviceId: peripheral.id,
|
|
25
|
+
name: adv.localName || peripheral.name || null,
|
|
26
|
+
rssi: peripheral.rssi,
|
|
27
|
+
// put the company id back in front, low byte first
|
|
28
|
+
manufacturerData: [companyId & 0xff, (companyId >> 8) & 0xff, ...bytes],
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Extra options for `BleManager.scan({...})` so OFP's devices get through a filtered scan and are heard
|
|
36
|
+
* well enough. Spread them AFTER the app's own options: on iOS they replace two of them.
|
|
37
|
+
*
|
|
38
|
+
* Android: its filters are alternatives (a device passes if it matches any), and the library takes one
|
|
39
|
+
* company id, so that id is added beside the app's services.
|
|
40
|
+
*
|
|
41
|
+
* iOS (`platform` 'ios'): there is no company-id filter, so the scan is opened to every device (an empty
|
|
42
|
+
* service list) and repeats are asked for (without them iOS reports a device once, and for a broadcast-only
|
|
43
|
+
* device the repeats are the data). The app then has to drop what it would not have been given by its own
|
|
44
|
+
* filter: `appCanRead`. iOS only delivers this while the app is in the foreground.
|
|
45
|
+
*
|
|
46
|
+
* @param need `ofp.scanNeed()`
|
|
47
|
+
* @param lowLatencyMode the library's `BleScanMode.LowLatency`
|
|
48
|
+
* @param platform 'ios' | 'android' (React Native's `Platform.OS`); Android when left out
|
|
49
|
+
*/
|
|
50
|
+
export function scanOptionsFor(ofp, need, lowLatencyMode, platform) {
|
|
51
|
+
if (platform === 'ios') return { serviceUUIDs: [], allowDuplicates: true };
|
|
52
|
+
const options = {};
|
|
53
|
+
if (ofp.companyIds.length) options.manufacturerData = { manufacturerId: ofp.companyIds[0] };
|
|
54
|
+
// While a broadcast device is in use its adverts are its data: listen all the time.
|
|
55
|
+
if (need === 'in_use' && lowLatencyMode !== undefined) options.scanMode = lowLatencyMode;
|
|
56
|
+
return options;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Android turns a scan that runs longer than 30 minutes into one that hears almost nothing: restart before. */
|
|
60
|
+
export const MAX_SCAN_SECONDS = 25 * 60;
|
|
61
|
+
|
|
62
|
+
// A 16-bit service comes as "1818" or in its long form "00001818-0000-1000-8000-00805f9b34fb"
|
|
63
|
+
function shortUuid(uuid) {
|
|
64
|
+
const u = String(uuid).toLowerCase();
|
|
65
|
+
const m = /^0000([0-9a-f]{4})-0000-1000-8000-00805f9b34fb$/.exec(u);
|
|
66
|
+
return m ? m[1] : u;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* True when the peripheral advertises one of the services the app reads itself: what a scan filtered to
|
|
71
|
+
* those services would have let through. On an open scan (iOS) everything else that OFP does not read is
|
|
72
|
+
* a phone, a headset, a television: drop it.
|
|
73
|
+
*/
|
|
74
|
+
export function appCanRead(peripheral, servicesTheAppReads) {
|
|
75
|
+
const mine = servicesTheAppReads.map(shortUuid);
|
|
76
|
+
const adv = (peripheral && peripheral.advertising) || {};
|
|
77
|
+
const theirs = [...(adv.serviceUUIDs || []), ...(adv.overflowServiceUUIDs || [])].map(shortUuid);
|
|
78
|
+
return theirs.some((u) => mine.includes(u));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** True when none of the peripheral's advertised services is one the app reads itself: only OFP can read it. */
|
|
82
|
+
export function onlyOfpCanRead(peripheral, servicesTheAppReads) {
|
|
83
|
+
return !appCanRead(peripheral, servicesTheAppReads);
|
|
84
|
+
}
|