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,120 @@
|
|
|
1
|
+
import { createConsole } from "../utils/Console.ts";
|
|
2
|
+
import { isInBrowser } from "../utils/environment.ts";
|
|
3
|
+
import BaseConnectionManager, { ConnectionType, ConnectionMessageType } from "./BaseConnectionManager.ts";
|
|
4
|
+
import { DeviceEventTypes } from "../Device.ts";
|
|
5
|
+
import { parseMessage } from "../utils/ParseUtils.ts";
|
|
6
|
+
import { DeviceInformationMessageTypes } from "../DeviceInformationManager.ts";
|
|
7
|
+
import { DeviceEventType } from "../Device.ts";
|
|
8
|
+
import { ClientDeviceMessage } from "../server/ServerUtils.ts";
|
|
9
|
+
|
|
10
|
+
const _console = createConsole("ClientConnectionManager", { log: true });
|
|
11
|
+
|
|
12
|
+
export type SendClientMessageCallback = (...messages: ClientDeviceMessage[]) => void;
|
|
13
|
+
|
|
14
|
+
const ClientDeviceInformationMessageTypes: ConnectionMessageType[] = [...DeviceInformationMessageTypes, "batteryLevel"];
|
|
15
|
+
|
|
16
|
+
class ClientConnectionManager extends BaseConnectionManager {
|
|
17
|
+
static get isSupported() {
|
|
18
|
+
return isInBrowser;
|
|
19
|
+
}
|
|
20
|
+
static get type(): ConnectionType {
|
|
21
|
+
return "client";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#bluetoothId!: string;
|
|
25
|
+
get bluetoothId() {
|
|
26
|
+
return this.#bluetoothId!;
|
|
27
|
+
}
|
|
28
|
+
set bluetoothId(newBluetoothId) {
|
|
29
|
+
_console.assertTypeWithError(newBluetoothId, "string");
|
|
30
|
+
if (this.#bluetoothId == newBluetoothId) {
|
|
31
|
+
_console.log("redundant bluetoothId assignment");
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.#bluetoothId = newBluetoothId;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#isConnected = false;
|
|
38
|
+
get isConnected() {
|
|
39
|
+
return this.#isConnected;
|
|
40
|
+
}
|
|
41
|
+
set isConnected(newIsConnected) {
|
|
42
|
+
_console.assertTypeWithError(newIsConnected, "boolean");
|
|
43
|
+
if (this.#isConnected == newIsConnected) {
|
|
44
|
+
_console.log("redundant newIsConnected assignment", newIsConnected);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this.#isConnected = newIsConnected;
|
|
48
|
+
|
|
49
|
+
this.status = this.#isConnected ? "connected" : "notConnected";
|
|
50
|
+
|
|
51
|
+
if (this.isConnected) {
|
|
52
|
+
this.#requestDeviceInformation();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async connect() {
|
|
57
|
+
await super.connect();
|
|
58
|
+
this.sendClientConnectMessage();
|
|
59
|
+
}
|
|
60
|
+
async disconnect() {
|
|
61
|
+
await super.disconnect();
|
|
62
|
+
this.sendClientDisconnectMessage();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get canReconnect() {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
async reconnect() {
|
|
69
|
+
await super.reconnect();
|
|
70
|
+
_console.log("attempting to reconnect...");
|
|
71
|
+
this.connect();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
sendClientMessage!: SendClientMessageCallback;
|
|
75
|
+
sendClientConnectMessage!: Function;
|
|
76
|
+
sendClientDisconnectMessage!: Function;
|
|
77
|
+
|
|
78
|
+
async sendSmpMessage(data: ArrayBuffer) {
|
|
79
|
+
super.sendSmpMessage(data);
|
|
80
|
+
this.sendClientMessage({ type: "smp", data });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async sendTxData(data: ArrayBuffer) {
|
|
84
|
+
super.sendTxData(data);
|
|
85
|
+
this.sendClientMessage({ type: "tx", data });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#requestDeviceInformation() {
|
|
89
|
+
this.sendClientMessage(...ClientDeviceInformationMessageTypes);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
onClientMessage(dataView: DataView) {
|
|
93
|
+
_console.log({ dataView });
|
|
94
|
+
parseMessage(dataView, DeviceEventTypes, this.#onClientMessageCallback.bind(this), null, true);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#onClientMessageCallback(messageType: DeviceEventType, dataView: DataView) {
|
|
98
|
+
let byteOffset = 0;
|
|
99
|
+
|
|
100
|
+
_console.log({ messageType }, dataView);
|
|
101
|
+
|
|
102
|
+
switch (messageType) {
|
|
103
|
+
case "isConnected":
|
|
104
|
+
const isConnected = Boolean(dataView.getUint8(byteOffset++));
|
|
105
|
+
_console.log({ isConnected });
|
|
106
|
+
this.isConnected = isConnected;
|
|
107
|
+
break;
|
|
108
|
+
|
|
109
|
+
case "rx":
|
|
110
|
+
this.parseRxMessage(dataView);
|
|
111
|
+
break;
|
|
112
|
+
|
|
113
|
+
default:
|
|
114
|
+
this.onMessageReceived!(messageType as ConnectionMessageType, dataView);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default ClientConnectionManager;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createConsole } from "../../utils/Console.ts";
|
|
2
|
+
import BaseConnectionManager from "../BaseConnectionManager.ts";
|
|
3
|
+
|
|
4
|
+
const _console = createConsole("BluetoothConnectionManager", { log: true });
|
|
5
|
+
|
|
6
|
+
import { BluetoothCharacteristicName } from "./bluetoothUUIDs.ts";
|
|
7
|
+
|
|
8
|
+
abstract class BluetoothConnectionManager extends BaseConnectionManager {
|
|
9
|
+
isInRange = true;
|
|
10
|
+
|
|
11
|
+
protected onCharacteristicValueChanged(characteristicName: BluetoothCharacteristicName, dataView: DataView) {
|
|
12
|
+
if (characteristicName == "rx") {
|
|
13
|
+
this.parseRxMessage(dataView);
|
|
14
|
+
} else {
|
|
15
|
+
this.onMessageReceived?.(characteristicName, dataView);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
protected async writeCharacteristic(characteristicName: BluetoothCharacteristicName, data: ArrayBuffer) {
|
|
20
|
+
_console.log("writeCharacteristic", ...arguments);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async sendSmpMessage(data: ArrayBuffer) {
|
|
24
|
+
super.sendSmpMessage(data);
|
|
25
|
+
await this.writeCharacteristic("smp", data);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async sendTxData(data: ArrayBuffer) {
|
|
29
|
+
super.sendTxData(data);
|
|
30
|
+
await this.writeCharacteristic("tx", data);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default BluetoothConnectionManager;
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { dataToArrayBuffer } from "../../utils/ArrayBufferUtils.ts";
|
|
2
|
+
import { createConsole } from "../../utils/Console.ts";
|
|
3
|
+
import { isInNode } from "../../utils/environment.ts";
|
|
4
|
+
import { addEventListeners, removeEventListeners, BoundGenericEventListeners } from "../../utils/EventUtils.ts";
|
|
5
|
+
import {
|
|
6
|
+
allServiceUUIDs,
|
|
7
|
+
getServiceNameFromUUID,
|
|
8
|
+
getCharacteristicNameFromUUID,
|
|
9
|
+
allCharacteristicNames,
|
|
10
|
+
getCharacteristicProperties,
|
|
11
|
+
} from "./bluetoothUUIDs.ts";
|
|
12
|
+
import BluetoothConnectionManager from "./BluetoothConnectionManager.ts";
|
|
13
|
+
|
|
14
|
+
const _console = createConsole("NobleConnectionManager", { log: true });
|
|
15
|
+
|
|
16
|
+
/** NODE_START */
|
|
17
|
+
import type * as noble from "@abandonware/noble";
|
|
18
|
+
/** NODE_END */
|
|
19
|
+
|
|
20
|
+
import { BluetoothCharacteristicName, BluetoothServiceName } from "./bluetoothUUIDs.ts";
|
|
21
|
+
import { ConnectionType } from "../BaseConnectionManager.ts";
|
|
22
|
+
import NobleScanner from "../../scanner/NobleScanner.ts";
|
|
23
|
+
|
|
24
|
+
interface HasConnectionManager {
|
|
25
|
+
connectionManager: NobleConnectionManager | undefined;
|
|
26
|
+
}
|
|
27
|
+
export interface NoblePeripheral extends noble.Peripheral, HasConnectionManager {
|
|
28
|
+
scanner: NobleScanner;
|
|
29
|
+
}
|
|
30
|
+
interface NobleService extends noble.Service, HasConnectionManager {
|
|
31
|
+
name: BluetoothServiceName;
|
|
32
|
+
}
|
|
33
|
+
interface NobleCharacteristic extends noble.Characteristic, HasConnectionManager {
|
|
34
|
+
name: BluetoothCharacteristicName;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class NobleConnectionManager extends BluetoothConnectionManager {
|
|
38
|
+
get bluetoothId() {
|
|
39
|
+
return this.#noblePeripheral!.id;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static get isSupported() {
|
|
43
|
+
return isInNode;
|
|
44
|
+
}
|
|
45
|
+
static get type(): ConnectionType {
|
|
46
|
+
return "noble";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get isConnected() {
|
|
50
|
+
return this.#noblePeripheral?.state == "connected";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async connect() {
|
|
54
|
+
await super.connect();
|
|
55
|
+
await this.#noblePeripheral!.connectAsync();
|
|
56
|
+
}
|
|
57
|
+
async disconnect() {
|
|
58
|
+
await super.disconnect();
|
|
59
|
+
await this.#noblePeripheral!.disconnectAsync();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async writeCharacteristic(characteristicName: BluetoothCharacteristicName, data: ArrayBuffer) {
|
|
63
|
+
const characteristic = this.#characteristics.get(characteristicName)!;
|
|
64
|
+
_console.assertWithError(characteristic, `no characteristic found with name "${characteristicName}"`);
|
|
65
|
+
// if (data instanceof DataView) {
|
|
66
|
+
// data = data.buffer;
|
|
67
|
+
// }
|
|
68
|
+
const properties = getCharacteristicProperties(characteristicName);
|
|
69
|
+
const buffer = Buffer.from(data);
|
|
70
|
+
const writeWithoutResponse = properties.writeWithoutResponse;
|
|
71
|
+
_console.log(`writing to ${characteristicName} ${writeWithoutResponse ? "without" : "with"} response`, buffer);
|
|
72
|
+
await characteristic.writeAsync(buffer, writeWithoutResponse);
|
|
73
|
+
if (characteristic.properties.includes("read")) {
|
|
74
|
+
await characteristic.readAsync();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get canReconnect() {
|
|
79
|
+
return this.#noblePeripheral!.connectable;
|
|
80
|
+
}
|
|
81
|
+
async reconnect() {
|
|
82
|
+
await super.reconnect();
|
|
83
|
+
_console.log("attempting to reconnect...");
|
|
84
|
+
this.connect();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// NOBLE
|
|
88
|
+
#noblePeripheral!: NoblePeripheral | undefined;
|
|
89
|
+
get noblePeripheral(): NoblePeripheral | undefined {
|
|
90
|
+
return this.#noblePeripheral;
|
|
91
|
+
}
|
|
92
|
+
set noblePeripheral(newNoblePeripheral: NoblePeripheral) {
|
|
93
|
+
_console.assertTypeWithError(newNoblePeripheral, "object");
|
|
94
|
+
if (this.noblePeripheral == newNoblePeripheral) {
|
|
95
|
+
_console.log("attempted to assign duplicate noblePeripheral");
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_console.log("newNoblePeripheral", newNoblePeripheral.id);
|
|
100
|
+
|
|
101
|
+
if (this.#noblePeripheral) {
|
|
102
|
+
removeEventListeners(this.#noblePeripheral, this.#unboundNoblePeripheralListeners);
|
|
103
|
+
delete this.#noblePeripheral!.connectionManager;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (newNoblePeripheral) {
|
|
107
|
+
newNoblePeripheral.connectionManager = this;
|
|
108
|
+
addEventListeners(newNoblePeripheral, this.#unboundNoblePeripheralListeners);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this.#noblePeripheral = newNoblePeripheral;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// NOBLE EVENTLISTENERS
|
|
115
|
+
#unboundNoblePeripheralListeners: BoundGenericEventListeners = {
|
|
116
|
+
connect: this.#onNoblePeripheralConnect,
|
|
117
|
+
disconnect: this.#onNoblePeripheralDisconnect,
|
|
118
|
+
rssiUpdate: this.#onNoblePeripheralRssiUpdate,
|
|
119
|
+
servicesDiscover: this.#onNoblePeripheralServicesDiscover,
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
async #onNoblePeripheralConnect(this: NoblePeripheral) {
|
|
123
|
+
await this.connectionManager!.onNoblePeripheralConnect(this);
|
|
124
|
+
}
|
|
125
|
+
async onNoblePeripheralConnect(noblePeripheral: NoblePeripheral) {
|
|
126
|
+
_console.log("onNoblePeripheralConnect", noblePeripheral.id, noblePeripheral.state);
|
|
127
|
+
if (noblePeripheral.state == "connected") {
|
|
128
|
+
await this.#noblePeripheral!.discoverServicesAsync(allServiceUUIDs as string[]);
|
|
129
|
+
}
|
|
130
|
+
// this gets called when it connects and disconnects, so we use the noblePeripheral's "state" property instead
|
|
131
|
+
await this.#onNoblePeripheralState();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async #onNoblePeripheralDisconnect(this: NoblePeripheral) {
|
|
135
|
+
await this.connectionManager!.onNoblePeripheralConnect(this);
|
|
136
|
+
}
|
|
137
|
+
async onNoblePeripheralDisconnect(noblePeripheral: NoblePeripheral) {
|
|
138
|
+
_console.log("onNoblePeripheralDisconnect", noblePeripheral.id);
|
|
139
|
+
await this.#onNoblePeripheralState();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async #onNoblePeripheralState() {
|
|
143
|
+
_console.log(`noblePeripheral ${this.bluetoothId} state ${this.#noblePeripheral!.state}`);
|
|
144
|
+
|
|
145
|
+
switch (this.#noblePeripheral!.state) {
|
|
146
|
+
case "connected":
|
|
147
|
+
//this.status = "connected";
|
|
148
|
+
break;
|
|
149
|
+
case "connecting":
|
|
150
|
+
//this.status = "connecting";
|
|
151
|
+
break;
|
|
152
|
+
case "disconnected":
|
|
153
|
+
this.#removeEventListeners();
|
|
154
|
+
this.status = "notConnected";
|
|
155
|
+
break;
|
|
156
|
+
case "disconnecting":
|
|
157
|
+
this.status = "disconnecting";
|
|
158
|
+
break;
|
|
159
|
+
case "error":
|
|
160
|
+
_console.error("noblePeripheral error");
|
|
161
|
+
break;
|
|
162
|
+
default:
|
|
163
|
+
_console.log(`uncaught noblePeripheral state ${this.#noblePeripheral!.state}`);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
#removeEventListeners() {
|
|
169
|
+
_console.log("removing noblePeripheral eventListeners");
|
|
170
|
+
this.#services.forEach((service) => {
|
|
171
|
+
removeEventListeners(service, this.#unboundNobleServiceListeners);
|
|
172
|
+
});
|
|
173
|
+
this.#services.clear();
|
|
174
|
+
|
|
175
|
+
this.#characteristics.forEach((characteristic) => {
|
|
176
|
+
removeEventListeners(characteristic, this.#unboundNobleCharacteristicListeners);
|
|
177
|
+
});
|
|
178
|
+
this.#characteristics.clear();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async #onNoblePeripheralRssiUpdate(this: NoblePeripheral, rssi: number) {
|
|
182
|
+
await this.connectionManager!.onNoblePeripheralRssiUpdate(this, rssi);
|
|
183
|
+
}
|
|
184
|
+
async onNoblePeripheralRssiUpdate(noblePeripheral: NoblePeripheral, rssi: number) {
|
|
185
|
+
_console.log("onNoblePeripheralRssiUpdate", noblePeripheral.id, rssi);
|
|
186
|
+
// FILL
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async #onNoblePeripheralServicesDiscover(this: NoblePeripheral, services: NobleService[]) {
|
|
190
|
+
await this.connectionManager!.onNoblePeripheralServicesDiscover(this, services);
|
|
191
|
+
}
|
|
192
|
+
async onNoblePeripheralServicesDiscover(noblePeripheral: NoblePeripheral, services: NobleService[]) {
|
|
193
|
+
_console.log(
|
|
194
|
+
"onNoblePeripheralServicesDiscover",
|
|
195
|
+
noblePeripheral.id,
|
|
196
|
+
services.map((service) => service.uuid)
|
|
197
|
+
);
|
|
198
|
+
for (const index in services) {
|
|
199
|
+
const service = services[index];
|
|
200
|
+
_console.log("service", service.uuid);
|
|
201
|
+
const serviceName = getServiceNameFromUUID(service.uuid)!;
|
|
202
|
+
_console.assertWithError(serviceName, `no name found for service uuid "${service.uuid}"`);
|
|
203
|
+
_console.log({ serviceName });
|
|
204
|
+
this.#services.set(serviceName, service);
|
|
205
|
+
service.name = serviceName;
|
|
206
|
+
service.connectionManager = this;
|
|
207
|
+
addEventListeners(service, this.#unboundNobleServiceListeners);
|
|
208
|
+
await service.discoverCharacteristicsAsync();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// NOBLE SERVICE
|
|
213
|
+
#services: Map<BluetoothServiceName, NobleService> = new Map();
|
|
214
|
+
|
|
215
|
+
#unboundNobleServiceListeners = {
|
|
216
|
+
characteristicsDiscover: this.#onNobleServiceCharacteristicsDiscover,
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
async #onNobleServiceCharacteristicsDiscover(this: NobleService, characteristics: NobleCharacteristic[]) {
|
|
220
|
+
await this.connectionManager!.onNobleServiceCharacteristicsDiscover(this, characteristics);
|
|
221
|
+
}
|
|
222
|
+
async onNobleServiceCharacteristicsDiscover(service: NobleService, characteristics: NobleCharacteristic[]) {
|
|
223
|
+
_console.log(
|
|
224
|
+
"onNobleServiceCharacteristicsDiscover",
|
|
225
|
+
service.uuid,
|
|
226
|
+
characteristics.map((characteristic) => characteristic.uuid)
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
for (const index in characteristics) {
|
|
230
|
+
const characteristic = characteristics[index];
|
|
231
|
+
_console.log("characteristic", characteristic.uuid);
|
|
232
|
+
const characteristicName = getCharacteristicNameFromUUID(characteristic.uuid)!;
|
|
233
|
+
_console.assertWithError(
|
|
234
|
+
Boolean(characteristicName),
|
|
235
|
+
`no name found for characteristic uuid "${characteristic.uuid}"`
|
|
236
|
+
);
|
|
237
|
+
_console.log({ characteristicName });
|
|
238
|
+
this.#characteristics.set(characteristicName, characteristic);
|
|
239
|
+
characteristic.name = characteristicName;
|
|
240
|
+
characteristic.connectionManager = this;
|
|
241
|
+
addEventListeners(characteristic, this.#unboundNobleCharacteristicListeners);
|
|
242
|
+
if (characteristic.properties.includes("read")) {
|
|
243
|
+
await characteristic.readAsync();
|
|
244
|
+
}
|
|
245
|
+
if (characteristic.properties.includes("notify")) {
|
|
246
|
+
await characteristic.subscribeAsync();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (this.#hasAllCharacteristics) {
|
|
251
|
+
this.status = "connected";
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// NOBLE CHARACTERISRTIC
|
|
256
|
+
#unboundNobleCharacteristicListeners = {
|
|
257
|
+
data: this.#onNobleCharacteristicData,
|
|
258
|
+
write: this.#onNobleCharacteristicWrite,
|
|
259
|
+
notify: this.#onNobleCharacteristicNotify,
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
#characteristics: Map<BluetoothCharacteristicName, NobleCharacteristic> = new Map();
|
|
263
|
+
|
|
264
|
+
get #hasAllCharacteristics() {
|
|
265
|
+
return allCharacteristicNames.every((characteristicName) => {
|
|
266
|
+
return this.#characteristics.has(characteristicName);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
#onNobleCharacteristicData(this: NobleCharacteristic, data: Buffer, isNotification: boolean) {
|
|
271
|
+
this.connectionManager!.onNobleCharacteristicData(this, data, isNotification);
|
|
272
|
+
}
|
|
273
|
+
onNobleCharacteristicData(characteristic: NobleCharacteristic, data: Buffer, isNotification: boolean) {
|
|
274
|
+
_console.log("onNobleCharacteristicData", characteristic.uuid, data, isNotification);
|
|
275
|
+
const dataView = new DataView(dataToArrayBuffer(data));
|
|
276
|
+
|
|
277
|
+
const characteristicName: BluetoothCharacteristicName = characteristic.name;
|
|
278
|
+
_console.assertWithError(
|
|
279
|
+
Boolean(characteristicName),
|
|
280
|
+
`no name found for characteristic with uuid "${characteristic.uuid}"`
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
this.onCharacteristicValueChanged(characteristicName, dataView);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
#onNobleCharacteristicWrite(this: NobleCharacteristic) {
|
|
287
|
+
this.connectionManager!.onNobleCharacteristicWrite(this);
|
|
288
|
+
}
|
|
289
|
+
onNobleCharacteristicWrite(characteristic: NobleCharacteristic) {
|
|
290
|
+
_console.log("onNobleCharacteristicWrite", characteristic.uuid);
|
|
291
|
+
// FILL
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
#onNobleCharacteristicNotify(this: NobleCharacteristic, isSubscribed: boolean) {
|
|
295
|
+
this.connectionManager!.onNobleCharacteristicNotify(this, isSubscribed);
|
|
296
|
+
}
|
|
297
|
+
onNobleCharacteristicNotify(characteristic: NobleCharacteristic, isSubscribed: boolean) {
|
|
298
|
+
_console.log("onNobleCharacteristicNotify", characteristic.uuid, isSubscribed);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export default NobleConnectionManager;
|