-
Notifications
You must be signed in to change notification settings - Fork 0
/
BleService.ts
145 lines (118 loc) · 4.32 KB
/
BleService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { BleClient, BleDevice, ScanMode } from '@capacitor-community/bluetooth-le';
import {
BLE_MAX_TRANSMISSION_LENGTH,
BLE_SERVICE_UUID,
BleOption,
DaemonDevice,
getCharFromOption, RESET_UUID,
SERIAL_CHAR_UUID, SSID_CHAR_UUID
} from 'src/bluetooth/BleOptions';
import { SetupStore } from 'stores/setup';
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
export async function init(): Promise<DaemonDevice> {
const daemon = await connectDaemon();
const id = await getDeviceId(daemon);
return { daemon, id }
}
export async function config(store: SetupStore) {
if (!store.daemon) {
console.log('[BleService] Daemon was undefined!');
return;
}
await configureDaemon(store.daemon, (deviceId, configSsid) =>
// Write all other configuration first.
Object.keys(store)
.map((option) => ({
char: getCharFromOption(option as BleOption),
value: store[option as BleOption]
}))
.filter(pair => !!pair.char && (configSsid ? pair.char === SSID_CHAR_UUID : pair.char !== SSID_CHAR_UUID))
.map(config => writeToChar(deviceId, config.char, config.value as string))
)
}
export async function disconnect(deviceId: string | undefined) {
if (!deviceId) {
console.log('[BleService] Device id was undefined! Ignoring disconnect request.');
return;
}
await BleClient.disconnect(deviceId);
console.log('[BleService] Disconnected from device:', deviceId);
}
export async function reset(device: DaemonDevice) {
try {
await writeToChar(device.daemon.deviceId, RESET_UUID, "x");
} catch (e) {
await disconnect(device.daemon.deviceId)
}
}
async function getDeviceId(device: BleDevice): Promise<string> {
const view = await BleClient.read(device.deviceId, BLE_SERVICE_UUID, SERIAL_CHAR_UUID)
return textDecoder.decode(view)
}
async function writeToChar(deviceId: string, char: string | undefined, value: string) {
if (!value || !char) {
console.log(`[BleService] Skipping write to char <${char}>. Value empty.`);
return
}
let chunks: string[];
// Split the chunks to be less than the max transmission length
if (value.length >= BLE_MAX_TRANSMISSION_LENGTH) {
const chunkCount = Math.ceil(value.length / BLE_MAX_TRANSMISSION_LENGTH);
chunks = Array.from({ length: chunkCount }, (_, index) => {
const start = index * BLE_MAX_TRANSMISSION_LENGTH;
const end = Math.min((index + 1) * BLE_MAX_TRANSMISSION_LENGTH, value.length);
return value.substring(start, end);
});
} else {
chunks = [value];
}
console.log(`[BleService] Writing (${value.length} chars, ${chunks?.length ?? 1} chunk(s)) <${value}> to characteristic <${char}>...`);
// Write all the chunks in order
for (const [i, chunk] of chunks.entries()) {
console.log(`[BleService] Writing chunk (${i + 1} / ${chunks.length})...`);
const viewChunk = new DataView(textEncoder.encode(chunk).buffer);
await BleClient.write(deviceId, BLE_SERVICE_UUID, char, viewChunk);
}
}
async function connectDaemon(): Promise<BleDevice> {
try {
await BleClient.initialize();
} catch (e) {
throw new Error(
"Sorry, but your device does not support Bluetooth Low Energy.\n" +
"Please retry on a device with support."
)
}
console.log('[BleService] Requesting BLE device with daemon capabilities...');
let device;
try {
device = await BleClient.requestDevice({
services: [BLE_SERVICE_UUID],
scanMode: ScanMode.SCAN_MODE_BALANCED
});
} catch (e) {
throw new Error(
"Could not find a suitable Daemon.\n" +
"Please try again."
)
}
// connect to device, the onDisconnect callback is optional
await BleClient.connect(device.deviceId, (deviceId) => onDisconnect(deviceId));
console.log('[BleService] Connected to BLE device:', JSON.stringify(device));
return device
}
async function configureDaemon(
daemon: BleDevice,
configureLambda: (deviceId: string, configSsid: boolean) => Array<Promise<void>>
) {
// Configure the device & await the writes IN ORDER
for (const writeToDaemon of configureLambda(daemon.deviceId, false)) {
await writeToDaemon;
}
// Configure SSID last because it triggers setup
await Promise.all(configureLambda(daemon.deviceId, true));
}
function onDisconnect(deviceId: string): void {
console.log(`[BleService] Device ${deviceId} disconnected.`);
}