brilliantsole 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.
- package/LICENSE +21 -0
- package/README.md +21 -0
- package/build/brilliantsole.cjs +5957 -0
- package/build/brilliantsole.cjs.map +1 -0
- package/build/brilliantsole.js +5448 -0
- package/build/brilliantsole.js.map +1 -0
- package/build/brilliantsole.ls.js +4872 -0
- package/build/brilliantsole.ls.js.map +1 -0
- package/build/brilliantsole.min.js +6 -0
- package/build/brilliantsole.min.js.map +1 -0
- package/build/brilliantsole.module.d.ts +908 -0
- package/build/brilliantsole.module.js +5412 -0
- package/build/brilliantsole.module.js.map +1 -0
- package/build/brilliantsole.module.min.d.ts +908 -0
- package/build/brilliantsole.module.min.js +6 -0
- package/build/brilliantsole.module.min.js.map +1 -0
- package/build/brilliantsole.node.module.d.ts +908 -0
- package/build/brilliantsole.node.module.js +5906 -0
- package/build/brilliantsole.node.module.js.map +1 -0
- package/build/dts/BS.d.ts +25 -0
- package/build/dts/Device.d.ts +136 -0
- package/build/dts/DeviceInformationManager.d.ts +56 -0
- package/build/dts/DeviceManager.d.ts +67 -0
- package/build/dts/FileTransferManager.d.ts +84 -0
- package/build/dts/FirmwareManager.d.ts +71 -0
- package/build/dts/InformationManager.d.ts +66 -0
- package/build/dts/TfliteManager.d.ts +92 -0
- package/build/dts/connection/BaseConnectionManager.d.ts +59 -0
- package/build/dts/connection/ClientConnectionManager.d.ts +23 -0
- package/build/dts/connection/WebSocketClientConnectionManager.d.ts +23 -0
- package/build/dts/connection/bluetooth/BluetoothConnectionManager.d.ts +10 -0
- package/build/dts/connection/bluetooth/NobleConnectionManager.d.ts +42 -0
- package/build/dts/connection/bluetooth/WebBluetoothConnectionManager.d.ts +20 -0
- package/build/dts/connection/bluetooth/bluetoothUUIDs.d.ts +14 -0
- package/build/dts/connection/webSocket/ClientConnectionManager.d.ts +23 -0
- package/build/dts/connection/webSocket/WebSocketClientConnectionManager.d.ts +23 -0
- package/build/dts/devicePair/DevicePair.d.ts +60 -0
- package/build/dts/devicePair/DevicePairPressureSensorDataManager.d.ts +25 -0
- package/build/dts/devicePair/DevicePairSensorDataManager.d.ts +33 -0
- package/build/dts/scanner/BaseScanner.d.ts +66 -0
- package/build/dts/scanner/NobleScanner.d.ts +17 -0
- package/build/dts/scanner/Scanner.d.ts +3 -0
- package/build/dts/sensor/BarometerSensorDataManager.d.ts +16 -0
- package/build/dts/sensor/MotionSensorDataManager.d.ts +69 -0
- package/build/dts/sensor/PressureSensorDataManager.d.ts +36 -0
- package/build/dts/sensor/SensorConfigurationManager.d.ts +44 -0
- package/build/dts/sensor/SensorDataManager.d.ts +40 -0
- package/build/dts/server/BaseClient.d.ts +85 -0
- package/build/dts/server/BaseServer.d.ts +48 -0
- package/build/dts/server/ServerUtils.d.ts +23 -0
- package/build/dts/server/udp/UDPServer.d.ts +11 -0
- package/build/dts/server/udp/UDPUtils.d.ts +9 -0
- package/build/dts/server/websocket/WebSocketClient.d.ts +17 -0
- package/build/dts/server/websocket/WebSocketServer.d.ts +13 -0
- package/build/dts/server/websocket/WebSocketUtils.d.ts +9 -0
- package/build/dts/utils/ArrayBufferUtils.d.ts +7 -0
- package/build/dts/utils/ArrayUtils.d.ts +2 -0
- package/build/dts/utils/CenterOfPressureHelper.d.ts +15 -0
- package/build/dts/utils/Console.d.ts +34 -0
- package/build/dts/utils/EventDispatcher.d.ts +50 -0
- package/build/dts/utils/EventUtils.d.ts +6 -0
- package/build/dts/utils/MathUtils.d.ts +21 -0
- package/build/dts/utils/ParseUtils.d.ts +5 -0
- package/build/dts/utils/RangeHelper.d.ts +8 -0
- package/build/dts/utils/Text.d.ts +6 -0
- package/build/dts/utils/Timer.d.ts +14 -0
- package/build/dts/utils/TypeScriptUtils.d.ts +19 -0
- package/build/dts/utils/cbor.d.ts +6 -0
- package/build/dts/utils/checksum.d.ts +3 -0
- package/build/dts/utils/environment.d.ts +13 -0
- package/build/dts/utils/mcumgr.d.ts +88 -0
- package/build/dts/utils/stringUtils.d.ts +2 -0
- package/build/dts/vibration/VibrationManager.d.ts +45 -0
- package/build/dts/vibration/VibrationWaveformEffects.d.ts +2 -0
- package/build/index.d.ts +908 -0
- package/build/index.node.d.ts +908 -0
- package/examples/3d/index.html +109 -0
- package/examples/3d/scene.html +57 -0
- package/examples/3d/script.js +419 -0
- package/examples/balance/index.html +138 -0
- package/examples/balance/script.js +243 -0
- package/examples/basic/index.html +327 -0
- package/examples/basic/script.js +1093 -0
- package/examples/center-of-pressure/index.html +132 -0
- package/examples/center-of-pressure/script.js +207 -0
- package/examples/device-pair/index.html +72 -0
- package/examples/device-pair/script.js +187 -0
- package/examples/edge-impulse/index.html +94 -0
- package/examples/edge-impulse/script.js +1033 -0
- package/examples/graph/index.html +83 -0
- package/examples/graph/script.js +469 -0
- package/examples/machine-learning/index.html +366 -0
- package/examples/machine-learning/script.js +1774 -0
- package/examples/pressure/index.html +145 -0
- package/examples/pressure/script.js +201 -0
- package/examples/recording/index.html +187 -0
- package/examples/recording/script.js +736 -0
- package/examples/server/index.html +266 -0
- package/examples/server/script.js +925 -0
- package/examples/utils/aframe/fingertip-button-component.js +201 -0
- package/examples/utils/aframe/fingertip-collider-target-component.js +102 -0
- package/examples/utils/aframe/fingertip-colliders-component.js +147 -0
- package/examples/utils/three/three.module.min.js +24846 -0
- package/examples/webxr/index.html +221 -0
- package/examples/webxr/script.js +1127 -0
- package/package.json +83 -0
- package/src/BS.ts +68 -0
- package/src/Device.ts +734 -0
- package/src/DeviceInformationManager.ts +146 -0
- package/src/DeviceManager.ts +354 -0
- package/src/FileTransferManager.ts +452 -0
- package/src/FirmwareManager.ts +357 -0
- package/src/InformationManager.ts +283 -0
- package/src/TfliteManager.ts +450 -0
- package/src/connection/BaseConnectionManager.ts +255 -0
- package/src/connection/ClientConnectionManager.ts +120 -0
- package/src/connection/bluetooth/BluetoothConnectionManager.ts +34 -0
- package/src/connection/bluetooth/NobleConnectionManager.ts +302 -0
- package/src/connection/bluetooth/WebBluetoothConnectionManager.ts +269 -0
- package/src/connection/bluetooth/bluetoothUUIDs.ts +218 -0
- package/src/devicePair/DevicePair.ts +253 -0
- package/src/devicePair/DevicePairPressureSensorDataManager.ts +82 -0
- package/src/devicePair/DevicePairSensorDataManager.ts +90 -0
- package/src/scanner/BaseScanner.ts +189 -0
- package/src/scanner/NobleScanner.ts +195 -0
- package/src/scanner/Scanner.ts +16 -0
- package/src/sensor/BarometerSensorDataManager.ts +41 -0
- package/src/sensor/MotionSensorDataManager.ts +151 -0
- package/src/sensor/PressureSensorDataManager.ts +112 -0
- package/src/sensor/SensorConfigurationManager.ts +177 -0
- package/src/sensor/SensorDataManager.ts +166 -0
- package/src/server/BaseClient.ts +368 -0
- package/src/server/BaseServer.ts +344 -0
- package/src/server/ServerUtils.ts +93 -0
- package/src/server/udp/UDPServer.ts +229 -0
- package/src/server/udp/UDPUtils.ts +20 -0
- package/src/server/websocket/WebSocketClient.ts +179 -0
- package/src/server/websocket/WebSocketServer.ts +184 -0
- package/src/server/websocket/WebSocketUtils.ts +20 -0
- package/src/utils/ArrayBufferUtils.ts +88 -0
- package/src/utils/ArrayUtils.ts +15 -0
- package/src/utils/CenterOfPressureHelper.ts +39 -0
- package/src/utils/Console.ts +156 -0
- package/src/utils/EventDispatcher.ts +153 -0
- package/src/utils/EventUtils.ts +41 -0
- package/src/utils/MathUtils.ts +53 -0
- package/src/utils/ParseUtils.ts +46 -0
- package/src/utils/RangeHelper.ts +38 -0
- package/src/utils/Text.ts +30 -0
- package/src/utils/Timer.ts +72 -0
- package/src/utils/TypeScriptUtils.ts +22 -0
- package/src/utils/cbor.js +429 -0
- package/src/utils/checksum.ts +41 -0
- package/src/utils/environment.ts +46 -0
- package/src/utils/mcumgr.js +444 -0
- package/src/utils/stringUtils.ts +11 -0
- package/src/vibration/VibrationManager.ts +308 -0
- package/src/vibration/VibrationWaveformEffects.ts +128 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { createConsole } from "../../utils/Console.ts";
|
|
2
|
+
import { isInNode, isInBrowser, isInBluefy, isInWebBLE } from "../../utils/environment.ts";
|
|
3
|
+
import { addEventListeners, removeEventListeners } from "../../utils/EventUtils.ts";
|
|
4
|
+
import {
|
|
5
|
+
serviceUUIDs,
|
|
6
|
+
optionalServiceUUIDs,
|
|
7
|
+
getServiceNameFromUUID,
|
|
8
|
+
getCharacteristicNameFromUUID,
|
|
9
|
+
getCharacteristicProperties,
|
|
10
|
+
} from "./bluetoothUUIDs.ts";
|
|
11
|
+
import BluetoothConnectionManager from "./BluetoothConnectionManager.ts";
|
|
12
|
+
import { BluetoothCharacteristicName, BluetoothServiceName } from "./bluetoothUUIDs.ts";
|
|
13
|
+
import { ConnectionType } from "../BaseConnectionManager.ts";
|
|
14
|
+
|
|
15
|
+
const _console = createConsole("WebBluetoothConnectionManager", { log: true });
|
|
16
|
+
|
|
17
|
+
type WebBluetoothInterface = webbluetooth.Bluetooth | Bluetooth;
|
|
18
|
+
|
|
19
|
+
interface BluetoothService extends BluetoothRemoteGATTService {
|
|
20
|
+
name?: BluetoothServiceName;
|
|
21
|
+
}
|
|
22
|
+
interface BluetoothCharacteristic extends BluetoothRemoteGATTCharacteristic {
|
|
23
|
+
name?: BluetoothCharacteristicName;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var bluetooth: WebBluetoothInterface | undefined;
|
|
27
|
+
/** NODE_START */
|
|
28
|
+
import * as webbluetooth from "webbluetooth";
|
|
29
|
+
if (isInNode) {
|
|
30
|
+
bluetooth = webbluetooth.bluetooth;
|
|
31
|
+
}
|
|
32
|
+
/** NODE_END */
|
|
33
|
+
|
|
34
|
+
/** BROWSER_START */
|
|
35
|
+
if (isInBrowser) {
|
|
36
|
+
bluetooth = window.navigator.bluetooth;
|
|
37
|
+
}
|
|
38
|
+
/** BROWSER_END */
|
|
39
|
+
|
|
40
|
+
class WebBluetoothConnectionManager extends BluetoothConnectionManager {
|
|
41
|
+
get bluetoothId() {
|
|
42
|
+
return this.device!.id;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#boundBluetoothCharacteristicEventListeners: { [eventType: string]: EventListener } = {
|
|
46
|
+
characteristicvaluechanged: this.#onCharacteristicvaluechanged.bind(this),
|
|
47
|
+
};
|
|
48
|
+
#boundBluetoothDeviceEventListeners: { [eventType: string]: EventListener } = {
|
|
49
|
+
gattserverdisconnected: this.#onGattserverdisconnected.bind(this),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
static get isSupported() {
|
|
53
|
+
return Boolean(bluetooth);
|
|
54
|
+
}
|
|
55
|
+
static get type(): ConnectionType {
|
|
56
|
+
return "webBluetooth";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#device!: BluetoothDevice | undefined;
|
|
60
|
+
get device() {
|
|
61
|
+
return this.#device;
|
|
62
|
+
}
|
|
63
|
+
set device(newDevice) {
|
|
64
|
+
if (this.#device == newDevice) {
|
|
65
|
+
_console.log("tried to assign the same BluetoothDevice");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (this.#device) {
|
|
69
|
+
removeEventListeners(this.#device, this.#boundBluetoothDeviceEventListeners);
|
|
70
|
+
}
|
|
71
|
+
if (newDevice) {
|
|
72
|
+
addEventListeners(newDevice, this.#boundBluetoothDeviceEventListeners);
|
|
73
|
+
}
|
|
74
|
+
this.#device = newDevice;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get server(): BluetoothRemoteGATTServer | undefined {
|
|
78
|
+
return this.#device?.gatt;
|
|
79
|
+
}
|
|
80
|
+
get isConnected() {
|
|
81
|
+
return this.server?.connected || false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#services: Map<BluetoothServiceName, BluetoothService> = new Map();
|
|
85
|
+
#characteristics: Map<BluetoothCharacteristicName, BluetoothCharacteristic> = new Map();
|
|
86
|
+
|
|
87
|
+
async connect() {
|
|
88
|
+
await super.connect();
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
const device = await bluetooth!.requestDevice({
|
|
92
|
+
filters: [{ services: serviceUUIDs }],
|
|
93
|
+
optionalServices: isInBrowser ? optionalServiceUUIDs : [],
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
_console.log("got BluetoothDevice");
|
|
97
|
+
this.device = device;
|
|
98
|
+
|
|
99
|
+
_console.log("connecting to device...");
|
|
100
|
+
const server = await this.server!.connect();
|
|
101
|
+
_console.log(`connected to device? ${server.connected}`);
|
|
102
|
+
|
|
103
|
+
await this.#getServicesAndCharacteristics();
|
|
104
|
+
|
|
105
|
+
_console.log("fully connected");
|
|
106
|
+
|
|
107
|
+
this.status = "connected";
|
|
108
|
+
} catch (error) {
|
|
109
|
+
_console.error(error);
|
|
110
|
+
this.status = "notConnected";
|
|
111
|
+
this.server?.disconnect();
|
|
112
|
+
this.#removeEventListeners();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async #getServicesAndCharacteristics() {
|
|
116
|
+
this.#removeEventListeners();
|
|
117
|
+
|
|
118
|
+
_console.log("getting services...");
|
|
119
|
+
const services = await this.server!.getPrimaryServices();
|
|
120
|
+
_console.log("got services", services.length);
|
|
121
|
+
const service = await this.server!.getPrimaryService("8d53dc1d-1db7-4cd3-868b-8a527460aa84");
|
|
122
|
+
|
|
123
|
+
_console.log("getting characteristics...");
|
|
124
|
+
for (const serviceIndex in services) {
|
|
125
|
+
const service = services[serviceIndex] as BluetoothService;
|
|
126
|
+
_console.log({ service });
|
|
127
|
+
const serviceName = getServiceNameFromUUID(service.uuid)!;
|
|
128
|
+
_console.assertWithError(serviceName, `no name found for service uuid "${service.uuid}"`);
|
|
129
|
+
_console.log(`got "${serviceName}" service`);
|
|
130
|
+
service.name = serviceName;
|
|
131
|
+
this.#services.set(serviceName, service);
|
|
132
|
+
_console.log(`getting characteristics for "${serviceName}" service`);
|
|
133
|
+
const characteristics = await service.getCharacteristics();
|
|
134
|
+
_console.log(`got characteristics for "${serviceName}" service`);
|
|
135
|
+
for (const characteristicIndex in characteristics) {
|
|
136
|
+
const characteristic = characteristics[characteristicIndex] as BluetoothCharacteristic;
|
|
137
|
+
_console.log({ characteristic });
|
|
138
|
+
const characteristicName = getCharacteristicNameFromUUID(characteristic.uuid)!;
|
|
139
|
+
_console.assertWithError(
|
|
140
|
+
Boolean(characteristicName),
|
|
141
|
+
`no name found for characteristic uuid "${characteristic.uuid}" in "${serviceName}" service`
|
|
142
|
+
);
|
|
143
|
+
_console.log(`got "${characteristicName}" characteristic in "${serviceName}" service`);
|
|
144
|
+
characteristic.name = characteristicName;
|
|
145
|
+
this.#characteristics.set(characteristicName, characteristic);
|
|
146
|
+
addEventListeners(characteristic, this.#boundBluetoothCharacteristicEventListeners);
|
|
147
|
+
const characteristicProperties = characteristic.properties || getCharacteristicProperties(characteristicName);
|
|
148
|
+
if (characteristicProperties.notify) {
|
|
149
|
+
_console.log(`starting notifications for "${characteristicName}" characteristic`);
|
|
150
|
+
await characteristic.startNotifications();
|
|
151
|
+
}
|
|
152
|
+
if (characteristicProperties.read) {
|
|
153
|
+
_console.log(`reading "${characteristicName}" characteristic...`);
|
|
154
|
+
await characteristic.readValue();
|
|
155
|
+
if (isInBluefy || isInWebBLE) {
|
|
156
|
+
this.#onCharacteristicValueChanged(characteristic);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async #removeEventListeners() {
|
|
163
|
+
if (this.device) {
|
|
164
|
+
removeEventListeners(this.device, this.#boundBluetoothDeviceEventListeners);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const promises = Array.from(this.#characteristics.keys()).map((characteristicName) => {
|
|
168
|
+
const characteristic = this.#characteristics.get(characteristicName)!;
|
|
169
|
+
removeEventListeners(characteristic, this.#boundBluetoothCharacteristicEventListeners);
|
|
170
|
+
const characteristicProperties = characteristic.properties || getCharacteristicProperties(characteristicName);
|
|
171
|
+
if (characteristicProperties.notify) {
|
|
172
|
+
_console.log(`stopping notifications for "${characteristicName}" characteristic`);
|
|
173
|
+
return characteristic.stopNotifications();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return Promise.allSettled(promises);
|
|
178
|
+
}
|
|
179
|
+
async disconnect() {
|
|
180
|
+
await this.#removeEventListeners();
|
|
181
|
+
await super.disconnect();
|
|
182
|
+
this.server?.disconnect();
|
|
183
|
+
this.status = "notConnected";
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#onCharacteristicvaluechanged(event: Event) {
|
|
187
|
+
_console.log("oncharacteristicvaluechanged");
|
|
188
|
+
|
|
189
|
+
const characteristic = event.target as BluetoothCharacteristic;
|
|
190
|
+
this.#onCharacteristicValueChanged(characteristic);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
#onCharacteristicValueChanged(characteristic: BluetoothCharacteristic) {
|
|
194
|
+
_console.log("onCharacteristicValue");
|
|
195
|
+
|
|
196
|
+
const characteristicName = characteristic.name!;
|
|
197
|
+
_console.assertWithError(
|
|
198
|
+
Boolean(characteristicName),
|
|
199
|
+
`no name found for characteristic with uuid "${characteristic.uuid}"`
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
_console.log(`oncharacteristicvaluechanged for "${characteristicName}" characteristic`);
|
|
203
|
+
const dataView = characteristic.value!;
|
|
204
|
+
_console.assertWithError(dataView, `no data found for "${characteristicName}" characteristic`);
|
|
205
|
+
_console.log(`data for "${characteristicName}" characteristic`, Array.from(new Uint8Array(dataView.buffer)));
|
|
206
|
+
|
|
207
|
+
try {
|
|
208
|
+
this.onCharacteristicValueChanged(characteristicName, dataView);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
_console.error(error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async writeCharacteristic(characteristicName: BluetoothCharacteristicName, data: ArrayBuffer) {
|
|
215
|
+
super.writeCharacteristic(characteristicName, data);
|
|
216
|
+
|
|
217
|
+
const characteristic = this.#characteristics.get(characteristicName)!;
|
|
218
|
+
_console.assertWithError(characteristic, `${characteristicName} characteristic not found`);
|
|
219
|
+
_console.log("writing characteristic", characteristic, data);
|
|
220
|
+
const characteristicProperties = characteristic.properties || getCharacteristicProperties(characteristicName);
|
|
221
|
+
if (characteristicProperties.writeWithoutResponse) {
|
|
222
|
+
_console.log("writing without response");
|
|
223
|
+
await characteristic.writeValueWithoutResponse(data);
|
|
224
|
+
} else {
|
|
225
|
+
_console.log("writing with response");
|
|
226
|
+
await characteristic.writeValueWithResponse(data);
|
|
227
|
+
}
|
|
228
|
+
_console.log("wrote characteristic");
|
|
229
|
+
|
|
230
|
+
if (characteristicProperties.read && !characteristicProperties.notify) {
|
|
231
|
+
_console.log("reading value after write...");
|
|
232
|
+
await characteristic.readValue();
|
|
233
|
+
if (isInBluefy || isInWebBLE) {
|
|
234
|
+
this.#onCharacteristicValueChanged(characteristic);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
#onGattserverdisconnected() {
|
|
240
|
+
_console.log("gattserverdisconnected");
|
|
241
|
+
this.status = "notConnected";
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
get canReconnect() {
|
|
245
|
+
return Boolean(this.server && !this.server.connected && this.isInRange);
|
|
246
|
+
}
|
|
247
|
+
async reconnect() {
|
|
248
|
+
await super.reconnect();
|
|
249
|
+
_console.log("attempting to reconnect...");
|
|
250
|
+
this.status = "connecting";
|
|
251
|
+
try {
|
|
252
|
+
await this.server!.connect();
|
|
253
|
+
} catch (error) {
|
|
254
|
+
_console.error(error);
|
|
255
|
+
this.isInRange = false;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (this.isConnected) {
|
|
259
|
+
_console.log("successfully reconnected!");
|
|
260
|
+
await this.#getServicesAndCharacteristics();
|
|
261
|
+
this.status = "connected";
|
|
262
|
+
} else {
|
|
263
|
+
_console.log("unable to reconnect");
|
|
264
|
+
this.status = "notConnected";
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export default WebBluetoothConnectionManager;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { isInBrowser, isInNode } from "../../utils/environment.ts";
|
|
2
|
+
import { createConsole } from "../../utils/Console.ts";
|
|
3
|
+
|
|
4
|
+
const _console = createConsole("bluetoothUUIDs", { log: false });
|
|
5
|
+
|
|
6
|
+
/** NODE_START */
|
|
7
|
+
import * as webbluetooth from "webbluetooth";
|
|
8
|
+
var BluetoothUUID = webbluetooth.BluetoothUUID;
|
|
9
|
+
/** NODE_END */
|
|
10
|
+
/** BROWSER_START */
|
|
11
|
+
if (isInBrowser) {
|
|
12
|
+
var BluetoothUUID = window.BluetoothUUID;
|
|
13
|
+
}
|
|
14
|
+
/** BROWSER_END */
|
|
15
|
+
|
|
16
|
+
function generateBluetoothUUID(value: string): BluetoothServiceUUID {
|
|
17
|
+
_console.assertTypeWithError(value, "string");
|
|
18
|
+
_console.assertWithError(value.length == 4, "value must be 4 characters long");
|
|
19
|
+
return `ea6da725-${value}-4f9b-893d-c3913e33b39f`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function stringToCharacteristicUUID(identifier: string): BluetoothCharacteristicUUID {
|
|
23
|
+
return BluetoothUUID?.getCharacteristic?.(identifier);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function stringToServiceUUID(identifier: string): BluetoothServiceUUID {
|
|
27
|
+
return BluetoothUUID?.getService?.(identifier);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type BluetoothServiceName = "deviceInformation" | "battery" | "main" | "smp";
|
|
31
|
+
import { DeviceInformationMessageType } from "../../DeviceInformationManager.ts";
|
|
32
|
+
export type BluetoothCharacteristicName = DeviceInformationMessageType | "batteryLevel" | "rx" | "tx" | "smp";
|
|
33
|
+
|
|
34
|
+
interface BluetoothCharacteristicInformation {
|
|
35
|
+
uuid: BluetoothCharacteristicUUID;
|
|
36
|
+
}
|
|
37
|
+
interface BluetoothServiceInformation {
|
|
38
|
+
uuid: BluetoothServiceUUID;
|
|
39
|
+
characteristics: { [characteristicName in BluetoothCharacteristicName]?: BluetoothCharacteristicInformation };
|
|
40
|
+
}
|
|
41
|
+
interface BluetoothServicesInformation {
|
|
42
|
+
services: { [serviceName in BluetoothServiceName]: BluetoothServiceInformation };
|
|
43
|
+
}
|
|
44
|
+
const bluetoothUUIDs: BluetoothServicesInformation = Object.freeze({
|
|
45
|
+
services: {
|
|
46
|
+
deviceInformation: {
|
|
47
|
+
uuid: stringToServiceUUID("device_information"),
|
|
48
|
+
characteristics: {
|
|
49
|
+
manufacturerName: {
|
|
50
|
+
uuid: stringToCharacteristicUUID("manufacturer_name_string"),
|
|
51
|
+
},
|
|
52
|
+
modelNumber: {
|
|
53
|
+
uuid: stringToCharacteristicUUID("model_number_string"),
|
|
54
|
+
},
|
|
55
|
+
hardwareRevision: {
|
|
56
|
+
uuid: stringToCharacteristicUUID("hardware_revision_string"),
|
|
57
|
+
},
|
|
58
|
+
firmwareRevision: {
|
|
59
|
+
uuid: stringToCharacteristicUUID("firmware_revision_string"),
|
|
60
|
+
},
|
|
61
|
+
softwareRevision: {
|
|
62
|
+
uuid: stringToCharacteristicUUID("software_revision_string"),
|
|
63
|
+
},
|
|
64
|
+
pnpId: {
|
|
65
|
+
uuid: stringToCharacteristicUUID("pnp_id"),
|
|
66
|
+
},
|
|
67
|
+
serialNumber: {
|
|
68
|
+
uuid: stringToCharacteristicUUID("serial_number_string"),
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
battery: {
|
|
73
|
+
uuid: stringToServiceUUID("battery_service"),
|
|
74
|
+
characteristics: {
|
|
75
|
+
batteryLevel: {
|
|
76
|
+
uuid: stringToCharacteristicUUID("battery_level"),
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
main: {
|
|
81
|
+
uuid: generateBluetoothUUID("0000"),
|
|
82
|
+
characteristics: {
|
|
83
|
+
rx: { uuid: generateBluetoothUUID("1000") },
|
|
84
|
+
tx: { uuid: generateBluetoothUUID("1001") },
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
smp: {
|
|
88
|
+
uuid: "8d53dc1d-1db7-4cd3-868b-8a527460aa84",
|
|
89
|
+
characteristics: {
|
|
90
|
+
smp: { uuid: "da2e7828-fbce-4e01-ae9e-261174997c48" },
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
export const serviceUUIDs = [bluetoothUUIDs.services.main.uuid];
|
|
97
|
+
export const optionalServiceUUIDs = [
|
|
98
|
+
bluetoothUUIDs.services.deviceInformation.uuid,
|
|
99
|
+
bluetoothUUIDs.services.battery.uuid,
|
|
100
|
+
bluetoothUUIDs.services.smp.uuid,
|
|
101
|
+
];
|
|
102
|
+
export const allServiceUUIDs = [...serviceUUIDs, ...optionalServiceUUIDs];
|
|
103
|
+
|
|
104
|
+
export function getServiceNameFromUUID(serviceUUID: BluetoothServiceUUID): BluetoothServiceName | undefined {
|
|
105
|
+
serviceUUID = serviceUUID.toString().toLowerCase();
|
|
106
|
+
const serviceNames = Object.keys(bluetoothUUIDs.services) as BluetoothServiceName[];
|
|
107
|
+
return serviceNames.find((serviceName) => {
|
|
108
|
+
const serviceInfo = bluetoothUUIDs.services[serviceName];
|
|
109
|
+
let serviceInfoUUID = serviceInfo.uuid.toString();
|
|
110
|
+
if (serviceUUID.length == 4) {
|
|
111
|
+
serviceInfoUUID = serviceInfoUUID.slice(4, 8);
|
|
112
|
+
}
|
|
113
|
+
if (!serviceUUID.includes("-")) {
|
|
114
|
+
serviceInfoUUID = serviceInfoUUID.replaceAll("-", "");
|
|
115
|
+
}
|
|
116
|
+
return serviceUUID == serviceInfoUUID;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export const characteristicUUIDs: BluetoothCharacteristicUUID[] = [];
|
|
121
|
+
export const allCharacteristicUUIDs: BluetoothCharacteristicUUID[] = [];
|
|
122
|
+
|
|
123
|
+
export const characteristicNames: BluetoothCharacteristicName[] = [];
|
|
124
|
+
export const allCharacteristicNames: BluetoothCharacteristicName[] = [];
|
|
125
|
+
|
|
126
|
+
Object.values(bluetoothUUIDs.services).forEach((serviceInfo) => {
|
|
127
|
+
if (!serviceInfo.characteristics) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const characteristicNames = Object.keys(serviceInfo.characteristics) as BluetoothCharacteristicName[];
|
|
131
|
+
characteristicNames.forEach((characteristicName) => {
|
|
132
|
+
const characteristicInfo = serviceInfo.characteristics[characteristicName]!;
|
|
133
|
+
if (serviceUUIDs.includes(serviceInfo.uuid)) {
|
|
134
|
+
characteristicUUIDs.push(characteristicInfo.uuid);
|
|
135
|
+
characteristicNames.push(characteristicName);
|
|
136
|
+
}
|
|
137
|
+
allCharacteristicUUIDs.push(characteristicInfo.uuid);
|
|
138
|
+
allCharacteristicNames.push(characteristicName);
|
|
139
|
+
});
|
|
140
|
+
}, []);
|
|
141
|
+
|
|
142
|
+
//_console.log({ characteristicUUIDs, allCharacteristicUUIDs });
|
|
143
|
+
|
|
144
|
+
export function getCharacteristicNameFromUUID(
|
|
145
|
+
characteristicUUID: BluetoothCharacteristicUUID
|
|
146
|
+
): BluetoothCharacteristicName | undefined {
|
|
147
|
+
//_console.log({ characteristicUUID });
|
|
148
|
+
characteristicUUID = characteristicUUID.toString().toLowerCase();
|
|
149
|
+
var characteristicName: BluetoothCharacteristicName | undefined;
|
|
150
|
+
Object.values(bluetoothUUIDs.services).some((serviceInfo) => {
|
|
151
|
+
const characteristicNames = Object.keys(serviceInfo.characteristics) as BluetoothCharacteristicName[];
|
|
152
|
+
characteristicName = characteristicNames.find((_characteristicName) => {
|
|
153
|
+
const characteristicInfo = serviceInfo.characteristics[_characteristicName]!;
|
|
154
|
+
let characteristicInfoUUID = characteristicInfo.uuid.toString();
|
|
155
|
+
if (characteristicUUID.length == 4) {
|
|
156
|
+
characteristicInfoUUID = characteristicInfoUUID.slice(4, 8);
|
|
157
|
+
}
|
|
158
|
+
if (!characteristicUUID.includes("-")) {
|
|
159
|
+
characteristicInfoUUID = characteristicInfoUUID.replaceAll("-", "");
|
|
160
|
+
}
|
|
161
|
+
return characteristicUUID == characteristicInfoUUID;
|
|
162
|
+
});
|
|
163
|
+
return characteristicName;
|
|
164
|
+
});
|
|
165
|
+
return characteristicName;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function getCharacteristicProperties(
|
|
169
|
+
characteristicName: BluetoothCharacteristicName
|
|
170
|
+
): BluetoothCharacteristicProperties {
|
|
171
|
+
const properties = {
|
|
172
|
+
broadcast: false,
|
|
173
|
+
read: true,
|
|
174
|
+
writeWithoutResponse: false,
|
|
175
|
+
write: false,
|
|
176
|
+
notify: false,
|
|
177
|
+
indicate: false,
|
|
178
|
+
authenticatedSignedWrites: false,
|
|
179
|
+
reliableWrite: false,
|
|
180
|
+
writableAuxiliaries: false,
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// read
|
|
184
|
+
switch (characteristicName) {
|
|
185
|
+
case "rx":
|
|
186
|
+
case "tx":
|
|
187
|
+
case "smp":
|
|
188
|
+
properties.read = false;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// notify
|
|
193
|
+
switch (characteristicName) {
|
|
194
|
+
case "batteryLevel":
|
|
195
|
+
case "rx":
|
|
196
|
+
case "smp":
|
|
197
|
+
properties.notify = true;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// write without response
|
|
202
|
+
switch (characteristicName) {
|
|
203
|
+
case "smp":
|
|
204
|
+
properties.writeWithoutResponse = true;
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// write
|
|
209
|
+
switch (characteristicName) {
|
|
210
|
+
case "tx":
|
|
211
|
+
properties.write = true;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return properties;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export const serviceDataUUID = "0000";
|