@capacitor-community/bluetooth-le 7.0.0 → 7.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.
Files changed (55) hide show
  1. package/CapacitorCommunityBluetoothLe.podspec +17 -17
  2. package/LICENSE +21 -21
  3. package/README.md +95 -26
  4. package/android/build.gradle +68 -68
  5. package/android/src/main/AndroidManifest.xml +22 -22
  6. package/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/BluetoothLe.kt +1061 -1009
  7. package/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Conversion.kt +51 -51
  8. package/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt +720 -718
  9. package/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/DeviceList.kt +28 -28
  10. package/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/DeviceScanner.kt +189 -189
  11. package/dist/docs.json +892 -78
  12. package/dist/esm/bleClient.d.ts +277 -277
  13. package/dist/esm/bleClient.js +349 -349
  14. package/dist/esm/bleClient.js.map +1 -1
  15. package/dist/esm/config.d.ts +53 -53
  16. package/dist/esm/config.js +2 -2
  17. package/dist/esm/config.js.map +1 -1
  18. package/dist/esm/conversion.d.ts +34 -34
  19. package/dist/esm/conversion.js +84 -84
  20. package/dist/esm/conversion.js.map +1 -1
  21. package/dist/esm/definitions.d.ts +301 -292
  22. package/dist/esm/definitions.js +42 -42
  23. package/dist/esm/definitions.js.map +1 -1
  24. package/dist/esm/index.d.ts +5 -5
  25. package/dist/esm/index.js +5 -5
  26. package/dist/esm/index.js.map +1 -1
  27. package/dist/esm/plugin.d.ts +2 -2
  28. package/dist/esm/plugin.js +4 -4
  29. package/dist/esm/plugin.js.map +1 -1
  30. package/dist/esm/queue.d.ts +3 -3
  31. package/dist/esm/queue.js +17 -17
  32. package/dist/esm/queue.js.map +1 -1
  33. package/dist/esm/timeout.d.ts +1 -1
  34. package/dist/esm/timeout.js +9 -9
  35. package/dist/esm/timeout.js.map +1 -1
  36. package/dist/esm/validators.d.ts +1 -1
  37. package/dist/esm/validators.js +11 -11
  38. package/dist/esm/validators.js.map +1 -1
  39. package/dist/esm/web.d.ts +56 -56
  40. package/dist/esm/web.js +340 -335
  41. package/dist/esm/web.js.map +1 -1
  42. package/dist/plugin.cjs.js +841 -834
  43. package/dist/plugin.cjs.js.map +1 -1
  44. package/dist/plugin.js +841 -834
  45. package/dist/plugin.js.map +1 -1
  46. package/ios/Plugin/Conversion.swift +83 -83
  47. package/ios/Plugin/Device.swift +423 -423
  48. package/ios/Plugin/DeviceManager.swift +401 -349
  49. package/ios/Plugin/Info.plist +24 -24
  50. package/ios/Plugin/Logging.swift +8 -8
  51. package/ios/Plugin/Plugin.h +10 -10
  52. package/ios/Plugin/Plugin.m +41 -41
  53. package/ios/Plugin/Plugin.swift +682 -636
  54. package/ios/Plugin/ThreadSafeDictionary.swift +13 -13
  55. package/package.json +101 -101
package/dist/esm/web.js CHANGED
@@ -1,336 +1,341 @@
1
- import { WebPlugin } from '@capacitor/core';
2
- import { hexStringToDataView, mapToObject, webUUIDToString } from './conversion';
3
- import { runWithTimeout } from './timeout';
4
- export class BluetoothLeWeb extends WebPlugin {
5
- constructor() {
6
- super(...arguments);
7
- this.deviceMap = new Map();
8
- this.discoveredDevices = new Map();
9
- this.scan = null;
10
- this.DEFAULT_CONNECTION_TIMEOUT = 10000;
11
- this.onAdvertisementReceivedCallback = this.onAdvertisementReceived.bind(this);
12
- this.onDisconnectedCallback = this.onDisconnected.bind(this);
13
- this.onCharacteristicValueChangedCallback = this.onCharacteristicValueChanged.bind(this);
14
- }
15
- async initialize() {
16
- if (typeof navigator === 'undefined' || !navigator.bluetooth) {
17
- throw this.unavailable('Web Bluetooth API not available in this browser.');
18
- }
19
- const isAvailable = await navigator.bluetooth.getAvailability();
20
- if (!isAvailable) {
21
- throw this.unavailable('No Bluetooth radio available.');
22
- }
23
- }
24
- async isEnabled() {
25
- // not available on web
26
- return { value: true };
27
- }
28
- async requestEnable() {
29
- throw this.unavailable('requestEnable is not available on web.');
30
- }
31
- async enable() {
32
- throw this.unavailable('enable is not available on web.');
33
- }
34
- async disable() {
35
- throw this.unavailable('disable is not available on web.');
36
- }
37
- async startEnabledNotifications() {
38
- // not available on web
39
- }
40
- async stopEnabledNotifications() {
41
- // not available on web
42
- }
43
- async isLocationEnabled() {
44
- throw this.unavailable('isLocationEnabled is not available on web.');
45
- }
46
- async openLocationSettings() {
47
- throw this.unavailable('openLocationSettings is not available on web.');
48
- }
49
- async openBluetoothSettings() {
50
- throw this.unavailable('openBluetoothSettings is not available on web.');
51
- }
52
- async openAppSettings() {
53
- throw this.unavailable('openAppSettings is not available on web.');
54
- }
55
- async setDisplayStrings() {
56
- // not available on web
57
- }
58
- async requestDevice(options) {
59
- const filters = this.getFilters(options);
60
- const device = await navigator.bluetooth.requestDevice({
61
- filters: filters.length ? filters : undefined,
62
- optionalServices: options === null || options === void 0 ? void 0 : options.optionalServices,
63
- acceptAllDevices: filters.length === 0,
64
- });
65
- this.deviceMap.set(device.id, device);
66
- const bleDevice = this.getBleDevice(device);
67
- return bleDevice;
68
- }
69
- async requestLEScan(options) {
70
- this.requestBleDeviceOptions = options;
71
- const filters = this.getFilters(options);
72
- await this.stopLEScan();
73
- this.discoveredDevices = new Map();
74
- navigator.bluetooth.removeEventListener('advertisementreceived', this.onAdvertisementReceivedCallback);
75
- navigator.bluetooth.addEventListener('advertisementreceived', this.onAdvertisementReceivedCallback);
76
- this.scan = await navigator.bluetooth.requestLEScan({
77
- filters: filters.length ? filters : undefined,
78
- acceptAllAdvertisements: filters.length === 0,
79
- keepRepeatedDevices: options === null || options === void 0 ? void 0 : options.allowDuplicates,
80
- });
81
- }
82
- onAdvertisementReceived(event) {
83
- var _a, _b;
84
- const deviceId = event.device.id;
85
- this.deviceMap.set(deviceId, event.device);
86
- const isNew = !this.discoveredDevices.has(deviceId);
87
- if (isNew || ((_a = this.requestBleDeviceOptions) === null || _a === void 0 ? void 0 : _a.allowDuplicates)) {
88
- this.discoveredDevices.set(deviceId, true);
89
- const device = this.getBleDevice(event.device);
90
- const result = {
91
- device,
92
- localName: device.name,
93
- rssi: event.rssi,
94
- txPower: event.txPower,
95
- manufacturerData: mapToObject(event.manufacturerData),
96
- serviceData: mapToObject(event.serviceData),
97
- uuids: (_b = event.uuids) === null || _b === void 0 ? void 0 : _b.map(webUUIDToString),
98
- };
99
- this.notifyListeners('onScanResult', result);
100
- }
101
- }
102
- async stopLEScan() {
103
- var _a;
104
- if ((_a = this.scan) === null || _a === void 0 ? void 0 : _a.active) {
105
- this.scan.stop();
106
- }
107
- this.scan = null;
108
- }
109
- async getDevices(options) {
110
- const devices = await navigator.bluetooth.getDevices();
111
- const bleDevices = devices
112
- .filter((device) => options.deviceIds.includes(device.id))
113
- .map((device) => {
114
- this.deviceMap.set(device.id, device);
115
- const bleDevice = this.getBleDevice(device);
116
- return bleDevice;
117
- });
118
- return { devices: bleDevices };
119
- }
120
- async getConnectedDevices(_options) {
121
- const devices = await navigator.bluetooth.getDevices();
122
- const bleDevices = devices
123
- .filter((device) => {
124
- var _a;
125
- return (_a = device.gatt) === null || _a === void 0 ? void 0 : _a.connected;
126
- })
127
- .map((device) => {
128
- this.deviceMap.set(device.id, device);
129
- const bleDevice = this.getBleDevice(device);
130
- return bleDevice;
131
- });
132
- return { devices: bleDevices };
133
- }
134
- async getBondedDevices() {
135
- return {};
136
- }
137
- async connect(options) {
138
- var _a, _b;
139
- const device = this.getDeviceFromMap(options.deviceId);
140
- device.removeEventListener('gattserverdisconnected', this.onDisconnectedCallback);
141
- device.addEventListener('gattserverdisconnected', this.onDisconnectedCallback);
142
- const timeoutError = Symbol();
143
- if (device.gatt === undefined) {
144
- throw new Error('No gatt server available.');
145
- }
146
- try {
147
- const timeout = (_a = options.timeout) !== null && _a !== void 0 ? _a : this.DEFAULT_CONNECTION_TIMEOUT;
148
- await runWithTimeout(device.gatt.connect(), timeout, timeoutError);
149
- }
150
- catch (error) {
151
- // cancel pending connect call, does not work yet in chromium because of a bug:
152
- // https://bugs.chromium.org/p/chromium/issues/detail?id=684073
153
- await ((_b = device.gatt) === null || _b === void 0 ? void 0 : _b.disconnect());
154
- if (error === timeoutError) {
155
- throw new Error('Connection timeout');
156
- }
157
- else {
158
- throw error;
159
- }
160
- }
161
- }
162
- onDisconnected(event) {
163
- const deviceId = event.target.id;
164
- const key = `disconnected|${deviceId}`;
165
- this.notifyListeners(key, null);
166
- }
167
- async createBond(_options) {
168
- throw this.unavailable('createBond is not available on web.');
169
- }
170
- async isBonded(_options) {
171
- throw this.unavailable('isBonded is not available on web.');
172
- }
173
- async disconnect(options) {
174
- var _a;
175
- (_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.disconnect();
176
- }
177
- async getServices(options) {
178
- var _a, _b;
179
- const services = (_b = (await ((_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.getPrimaryServices()))) !== null && _b !== void 0 ? _b : [];
180
- const bleServices = [];
181
- for (const service of services) {
182
- const characteristics = await service.getCharacteristics();
183
- const bleCharacteristics = [];
184
- for (const characteristic of characteristics) {
185
- bleCharacteristics.push({
186
- uuid: characteristic.uuid,
187
- properties: this.getProperties(characteristic),
188
- descriptors: await this.getDescriptors(characteristic),
189
- });
190
- }
191
- bleServices.push({ uuid: service.uuid, characteristics: bleCharacteristics });
192
- }
193
- return { services: bleServices };
194
- }
195
- async getDescriptors(characteristic) {
196
- try {
197
- const descriptors = await characteristic.getDescriptors();
198
- return descriptors.map((descriptor) => ({
199
- uuid: descriptor.uuid,
200
- }));
201
- }
202
- catch (_a) {
203
- return [];
204
- }
205
- }
206
- getProperties(characteristic) {
207
- return {
208
- broadcast: characteristic.properties.broadcast,
209
- read: characteristic.properties.read,
210
- writeWithoutResponse: characteristic.properties.writeWithoutResponse,
211
- write: characteristic.properties.write,
212
- notify: characteristic.properties.notify,
213
- indicate: characteristic.properties.indicate,
214
- authenticatedSignedWrites: characteristic.properties.authenticatedSignedWrites,
215
- reliableWrite: characteristic.properties.reliableWrite,
216
- writableAuxiliaries: characteristic.properties.writableAuxiliaries,
217
- };
218
- }
219
- async getCharacteristic(options) {
220
- var _a;
221
- const service = await ((_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.getPrimaryService(options === null || options === void 0 ? void 0 : options.service));
222
- return service === null || service === void 0 ? void 0 : service.getCharacteristic(options === null || options === void 0 ? void 0 : options.characteristic);
223
- }
224
- async getDescriptor(options) {
225
- const characteristic = await this.getCharacteristic(options);
226
- return characteristic === null || characteristic === void 0 ? void 0 : characteristic.getDescriptor(options === null || options === void 0 ? void 0 : options.descriptor);
227
- }
228
- async discoverServices(_options) {
229
- throw this.unavailable('discoverServices is not available on web.');
230
- }
231
- async getMtu(_options) {
232
- throw this.unavailable('getMtu is not available on web.');
233
- }
234
- async requestConnectionPriority(_options) {
235
- throw this.unavailable('requestConnectionPriority is not available on web.');
236
- }
237
- async readRssi(_options) {
238
- throw this.unavailable('readRssi is not available on web.');
239
- }
240
- async read(options) {
241
- const characteristic = await this.getCharacteristic(options);
242
- const value = await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.readValue());
243
- return { value };
244
- }
245
- async write(options) {
246
- const characteristic = await this.getCharacteristic(options);
247
- let dataView;
248
- if (typeof options.value === 'string') {
249
- dataView = hexStringToDataView(options.value);
250
- }
251
- else {
252
- dataView = options.value;
253
- }
254
- await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.writeValueWithResponse(dataView));
255
- }
256
- async writeWithoutResponse(options) {
257
- const characteristic = await this.getCharacteristic(options);
258
- let dataView;
259
- if (typeof options.value === 'string') {
260
- dataView = hexStringToDataView(options.value);
261
- }
262
- else {
263
- dataView = options.value;
264
- }
265
- await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.writeValueWithoutResponse(dataView));
266
- }
267
- async readDescriptor(options) {
268
- const descriptor = await this.getDescriptor(options);
269
- const value = await (descriptor === null || descriptor === void 0 ? void 0 : descriptor.readValue());
270
- return { value };
271
- }
272
- async writeDescriptor(options) {
273
- const descriptor = await this.getDescriptor(options);
274
- let dataView;
275
- if (typeof options.value === 'string') {
276
- dataView = hexStringToDataView(options.value);
277
- }
278
- else {
279
- dataView = options.value;
280
- }
281
- await (descriptor === null || descriptor === void 0 ? void 0 : descriptor.writeValue(dataView));
282
- }
283
- async startNotifications(options) {
284
- const characteristic = await this.getCharacteristic(options);
285
- characteristic === null || characteristic === void 0 ? void 0 : characteristic.removeEventListener('characteristicvaluechanged', this.onCharacteristicValueChangedCallback);
286
- characteristic === null || characteristic === void 0 ? void 0 : characteristic.addEventListener('characteristicvaluechanged', this.onCharacteristicValueChangedCallback);
287
- await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.startNotifications());
288
- }
289
- onCharacteristicValueChanged(event) {
290
- var _a, _b;
291
- const characteristic = event.target;
292
- const key = `notification|${(_a = characteristic.service) === null || _a === void 0 ? void 0 : _a.device.id}|${(_b = characteristic.service) === null || _b === void 0 ? void 0 : _b.uuid}|${characteristic.uuid}`;
293
- this.notifyListeners(key, {
294
- value: characteristic.value,
295
- });
296
- }
297
- async stopNotifications(options) {
298
- const characteristic = await this.getCharacteristic(options);
299
- await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.stopNotifications());
300
- }
301
- getFilters(options) {
302
- var _a;
303
- const filters = [];
304
- for (const service of (_a = options === null || options === void 0 ? void 0 : options.services) !== null && _a !== void 0 ? _a : []) {
305
- filters.push({
306
- services: [service],
307
- name: options === null || options === void 0 ? void 0 : options.name,
308
- namePrefix: options === null || options === void 0 ? void 0 : options.namePrefix,
309
- });
310
- }
311
- if (((options === null || options === void 0 ? void 0 : options.name) || (options === null || options === void 0 ? void 0 : options.namePrefix)) && filters.length === 0) {
312
- filters.push({
313
- name: options.name,
314
- namePrefix: options.namePrefix,
315
- });
316
- }
317
- return filters;
318
- }
319
- getDeviceFromMap(deviceId) {
320
- const device = this.deviceMap.get(deviceId);
321
- if (device === undefined) {
322
- throw new Error('Device not found. Call "requestDevice", "requestLEScan" or "getDevices" first.');
323
- }
324
- return device;
325
- }
326
- getBleDevice(device) {
327
- var _a;
328
- const bleDevice = {
329
- deviceId: device.id,
330
- // use undefined instead of null if name is not available
331
- name: (_a = device.name) !== null && _a !== void 0 ? _a : undefined,
332
- };
333
- return bleDevice;
334
- }
335
- }
1
+ import { WebPlugin } from '@capacitor/core';
2
+ import { hexStringToDataView, mapToObject, webUUIDToString } from './conversion';
3
+ import { runWithTimeout } from './timeout';
4
+ export class BluetoothLeWeb extends WebPlugin {
5
+ constructor() {
6
+ super(...arguments);
7
+ this.deviceMap = new Map();
8
+ this.discoveredDevices = new Map();
9
+ this.scan = null;
10
+ this.DEFAULT_CONNECTION_TIMEOUT = 10000;
11
+ this.onAdvertisementReceivedCallback = this.onAdvertisementReceived.bind(this);
12
+ this.onDisconnectedCallback = this.onDisconnected.bind(this);
13
+ this.onCharacteristicValueChangedCallback = this.onCharacteristicValueChanged.bind(this);
14
+ }
15
+ async initialize() {
16
+ if (typeof navigator === 'undefined' || !navigator.bluetooth) {
17
+ throw this.unavailable('Web Bluetooth API not available in this browser.');
18
+ }
19
+ const isAvailable = await navigator.bluetooth.getAvailability();
20
+ if (!isAvailable) {
21
+ throw this.unavailable('No Bluetooth radio available.');
22
+ }
23
+ }
24
+ async isEnabled() {
25
+ // not available on web
26
+ return { value: true };
27
+ }
28
+ async requestEnable() {
29
+ throw this.unavailable('requestEnable is not available on web.');
30
+ }
31
+ async enable() {
32
+ throw this.unavailable('enable is not available on web.');
33
+ }
34
+ async disable() {
35
+ throw this.unavailable('disable is not available on web.');
36
+ }
37
+ async startEnabledNotifications() {
38
+ // not available on web
39
+ }
40
+ async stopEnabledNotifications() {
41
+ // not available on web
42
+ }
43
+ async isLocationEnabled() {
44
+ throw this.unavailable('isLocationEnabled is not available on web.');
45
+ }
46
+ async openLocationSettings() {
47
+ throw this.unavailable('openLocationSettings is not available on web.');
48
+ }
49
+ async openBluetoothSettings() {
50
+ throw this.unavailable('openBluetoothSettings is not available on web.');
51
+ }
52
+ async openAppSettings() {
53
+ throw this.unavailable('openAppSettings is not available on web.');
54
+ }
55
+ async setDisplayStrings() {
56
+ // not available on web
57
+ }
58
+ async requestDevice(options) {
59
+ const filters = this.getFilters(options);
60
+ const device = await navigator.bluetooth.requestDevice({
61
+ filters: filters.length ? filters : undefined,
62
+ optionalServices: options === null || options === void 0 ? void 0 : options.optionalServices,
63
+ acceptAllDevices: filters.length === 0,
64
+ });
65
+ this.deviceMap.set(device.id, device);
66
+ const bleDevice = this.getBleDevice(device);
67
+ return bleDevice;
68
+ }
69
+ async requestLEScan(options) {
70
+ this.requestBleDeviceOptions = options;
71
+ const filters = this.getFilters(options);
72
+ await this.stopLEScan();
73
+ this.discoveredDevices = new Map();
74
+ navigator.bluetooth.removeEventListener('advertisementreceived', this.onAdvertisementReceivedCallback);
75
+ navigator.bluetooth.addEventListener('advertisementreceived', this.onAdvertisementReceivedCallback);
76
+ this.scan = await navigator.bluetooth.requestLEScan({
77
+ filters: filters.length ? filters : undefined,
78
+ acceptAllAdvertisements: filters.length === 0,
79
+ keepRepeatedDevices: options === null || options === void 0 ? void 0 : options.allowDuplicates,
80
+ });
81
+ }
82
+ onAdvertisementReceived(event) {
83
+ var _a, _b;
84
+ const deviceId = event.device.id;
85
+ this.deviceMap.set(deviceId, event.device);
86
+ const isNew = !this.discoveredDevices.has(deviceId);
87
+ if (isNew || ((_a = this.requestBleDeviceOptions) === null || _a === void 0 ? void 0 : _a.allowDuplicates)) {
88
+ this.discoveredDevices.set(deviceId, true);
89
+ const device = this.getBleDevice(event.device);
90
+ const result = {
91
+ device,
92
+ localName: device.name,
93
+ rssi: event.rssi,
94
+ txPower: event.txPower,
95
+ manufacturerData: mapToObject(event.manufacturerData),
96
+ serviceData: mapToObject(event.serviceData),
97
+ uuids: (_b = event.uuids) === null || _b === void 0 ? void 0 : _b.map(webUUIDToString),
98
+ };
99
+ this.notifyListeners('onScanResult', result);
100
+ }
101
+ }
102
+ async stopLEScan() {
103
+ var _a;
104
+ if ((_a = this.scan) === null || _a === void 0 ? void 0 : _a.active) {
105
+ this.scan.stop();
106
+ }
107
+ this.scan = null;
108
+ }
109
+ async getDevices(options) {
110
+ const devices = await navigator.bluetooth.getDevices();
111
+ const bleDevices = devices
112
+ .filter((device) => options.deviceIds.includes(device.id))
113
+ .map((device) => {
114
+ this.deviceMap.set(device.id, device);
115
+ const bleDevice = this.getBleDevice(device);
116
+ return bleDevice;
117
+ });
118
+ return { devices: bleDevices };
119
+ }
120
+ async getConnectedDevices(_options) {
121
+ const devices = await navigator.bluetooth.getDevices();
122
+ const bleDevices = devices
123
+ .filter((device) => {
124
+ var _a;
125
+ return (_a = device.gatt) === null || _a === void 0 ? void 0 : _a.connected;
126
+ })
127
+ .map((device) => {
128
+ this.deviceMap.set(device.id, device);
129
+ const bleDevice = this.getBleDevice(device);
130
+ return bleDevice;
131
+ });
132
+ return { devices: bleDevices };
133
+ }
134
+ async getBondedDevices() {
135
+ return {};
136
+ }
137
+ async connect(options) {
138
+ var _a, _b;
139
+ const device = this.getDeviceFromMap(options.deviceId);
140
+ device.removeEventListener('gattserverdisconnected', this.onDisconnectedCallback);
141
+ device.addEventListener('gattserverdisconnected', this.onDisconnectedCallback);
142
+ const timeoutError = Symbol();
143
+ if (device.gatt === undefined) {
144
+ throw new Error('No gatt server available.');
145
+ }
146
+ try {
147
+ const timeout = (_a = options.timeout) !== null && _a !== void 0 ? _a : this.DEFAULT_CONNECTION_TIMEOUT;
148
+ await runWithTimeout(device.gatt.connect(), timeout, timeoutError);
149
+ }
150
+ catch (error) {
151
+ // cancel pending connect call, does not work yet in chromium because of a bug:
152
+ // https://bugs.chromium.org/p/chromium/issues/detail?id=684073
153
+ await ((_b = device.gatt) === null || _b === void 0 ? void 0 : _b.disconnect());
154
+ if (error === timeoutError) {
155
+ throw new Error('Connection timeout');
156
+ }
157
+ else {
158
+ throw error;
159
+ }
160
+ }
161
+ }
162
+ onDisconnected(event) {
163
+ const deviceId = event.target.id;
164
+ const key = `disconnected|${deviceId}`;
165
+ this.notifyListeners(key, null);
166
+ }
167
+ async createBond(_options) {
168
+ throw this.unavailable('createBond is not available on web.');
169
+ }
170
+ async isBonded(_options) {
171
+ throw this.unavailable('isBonded is not available on web.');
172
+ }
173
+ async disconnect(options) {
174
+ var _a;
175
+ (_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.disconnect();
176
+ }
177
+ async getServices(options) {
178
+ var _a, _b;
179
+ const services = (_b = (await ((_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.getPrimaryServices()))) !== null && _b !== void 0 ? _b : [];
180
+ const bleServices = [];
181
+ for (const service of services) {
182
+ const characteristics = await service.getCharacteristics();
183
+ const bleCharacteristics = [];
184
+ for (const characteristic of characteristics) {
185
+ bleCharacteristics.push({
186
+ uuid: characteristic.uuid,
187
+ properties: this.getProperties(characteristic),
188
+ descriptors: await this.getDescriptors(characteristic),
189
+ });
190
+ }
191
+ bleServices.push({ uuid: service.uuid, characteristics: bleCharacteristics });
192
+ }
193
+ return { services: bleServices };
194
+ }
195
+ async getDescriptors(characteristic) {
196
+ try {
197
+ const descriptors = await characteristic.getDescriptors();
198
+ return descriptors.map((descriptor) => ({
199
+ uuid: descriptor.uuid,
200
+ }));
201
+ }
202
+ catch (_a) {
203
+ return [];
204
+ }
205
+ }
206
+ getProperties(characteristic) {
207
+ return {
208
+ broadcast: characteristic.properties.broadcast,
209
+ read: characteristic.properties.read,
210
+ writeWithoutResponse: characteristic.properties.writeWithoutResponse,
211
+ write: characteristic.properties.write,
212
+ notify: characteristic.properties.notify,
213
+ indicate: characteristic.properties.indicate,
214
+ authenticatedSignedWrites: characteristic.properties.authenticatedSignedWrites,
215
+ reliableWrite: characteristic.properties.reliableWrite,
216
+ writableAuxiliaries: characteristic.properties.writableAuxiliaries,
217
+ };
218
+ }
219
+ async getCharacteristic(options) {
220
+ var _a;
221
+ const service = await ((_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.getPrimaryService(options === null || options === void 0 ? void 0 : options.service));
222
+ return service === null || service === void 0 ? void 0 : service.getCharacteristic(options === null || options === void 0 ? void 0 : options.characteristic);
223
+ }
224
+ async getDescriptor(options) {
225
+ const characteristic = await this.getCharacteristic(options);
226
+ return characteristic === null || characteristic === void 0 ? void 0 : characteristic.getDescriptor(options === null || options === void 0 ? void 0 : options.descriptor);
227
+ }
228
+ async discoverServices(_options) {
229
+ throw this.unavailable('discoverServices is not available on web.');
230
+ }
231
+ async getMtu(_options) {
232
+ throw this.unavailable('getMtu is not available on web.');
233
+ }
234
+ async requestConnectionPriority(_options) {
235
+ throw this.unavailable('requestConnectionPriority is not available on web.');
236
+ }
237
+ async readRssi(_options) {
238
+ throw this.unavailable('readRssi is not available on web.');
239
+ }
240
+ async read(options) {
241
+ const characteristic = await this.getCharacteristic(options);
242
+ const value = await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.readValue());
243
+ return { value };
244
+ }
245
+ async write(options) {
246
+ const characteristic = await this.getCharacteristic(options);
247
+ let dataView;
248
+ if (typeof options.value === 'string') {
249
+ dataView = hexStringToDataView(options.value);
250
+ }
251
+ else {
252
+ dataView = options.value;
253
+ }
254
+ await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.writeValueWithResponse(dataView));
255
+ }
256
+ async writeWithoutResponse(options) {
257
+ const characteristic = await this.getCharacteristic(options);
258
+ let dataView;
259
+ if (typeof options.value === 'string') {
260
+ dataView = hexStringToDataView(options.value);
261
+ }
262
+ else {
263
+ dataView = options.value;
264
+ }
265
+ await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.writeValueWithoutResponse(dataView));
266
+ }
267
+ async readDescriptor(options) {
268
+ const descriptor = await this.getDescriptor(options);
269
+ const value = await (descriptor === null || descriptor === void 0 ? void 0 : descriptor.readValue());
270
+ return { value };
271
+ }
272
+ async writeDescriptor(options) {
273
+ const descriptor = await this.getDescriptor(options);
274
+ let dataView;
275
+ if (typeof options.value === 'string') {
276
+ dataView = hexStringToDataView(options.value);
277
+ }
278
+ else {
279
+ dataView = options.value;
280
+ }
281
+ await (descriptor === null || descriptor === void 0 ? void 0 : descriptor.writeValue(dataView));
282
+ }
283
+ async startNotifications(options) {
284
+ const characteristic = await this.getCharacteristic(options);
285
+ characteristic === null || characteristic === void 0 ? void 0 : characteristic.removeEventListener('characteristicvaluechanged', this.onCharacteristicValueChangedCallback);
286
+ characteristic === null || characteristic === void 0 ? void 0 : characteristic.addEventListener('characteristicvaluechanged', this.onCharacteristicValueChangedCallback);
287
+ await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.startNotifications());
288
+ }
289
+ onCharacteristicValueChanged(event) {
290
+ var _a, _b;
291
+ const characteristic = event.target;
292
+ const key = `notification|${(_a = characteristic.service) === null || _a === void 0 ? void 0 : _a.device.id}|${(_b = characteristic.service) === null || _b === void 0 ? void 0 : _b.uuid}|${characteristic.uuid}`;
293
+ this.notifyListeners(key, {
294
+ value: characteristic.value,
295
+ });
296
+ }
297
+ async stopNotifications(options) {
298
+ const characteristic = await this.getCharacteristic(options);
299
+ await (characteristic === null || characteristic === void 0 ? void 0 : characteristic.stopNotifications());
300
+ }
301
+ getFilters(options) {
302
+ var _a, _b;
303
+ const filters = [];
304
+ for (const service of (_a = options === null || options === void 0 ? void 0 : options.services) !== null && _a !== void 0 ? _a : []) {
305
+ filters.push({
306
+ services: [service],
307
+ name: options === null || options === void 0 ? void 0 : options.name,
308
+ namePrefix: options === null || options === void 0 ? void 0 : options.namePrefix,
309
+ });
310
+ }
311
+ if (((options === null || options === void 0 ? void 0 : options.name) || (options === null || options === void 0 ? void 0 : options.namePrefix)) && filters.length === 0) {
312
+ filters.push({
313
+ name: options.name,
314
+ namePrefix: options.namePrefix,
315
+ });
316
+ }
317
+ for (const manufacturerData of (_b = options === null || options === void 0 ? void 0 : options.manufacturerData) !== null && _b !== void 0 ? _b : []) {
318
+ filters.push({
319
+ manufacturerData: [manufacturerData],
320
+ });
321
+ }
322
+ return filters;
323
+ }
324
+ getDeviceFromMap(deviceId) {
325
+ const device = this.deviceMap.get(deviceId);
326
+ if (device === undefined) {
327
+ throw new Error('Device not found. Call "requestDevice", "requestLEScan" or "getDevices" first.');
328
+ }
329
+ return device;
330
+ }
331
+ getBleDevice(device) {
332
+ var _a;
333
+ const bleDevice = {
334
+ deviceId: device.id,
335
+ // use undefined instead of null if name is not available
336
+ name: (_a = device.name) !== null && _a !== void 0 ? _a : undefined,
337
+ };
338
+ return bleDevice;
339
+ }
340
+ }
336
341
  //# sourceMappingURL=web.js.map