@capacitor-community/bluetooth-le 7.3.0 → 8.0.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 (46) hide show
  1. package/CapacitorCommunityBluetoothLe.podspec +2 -2
  2. package/Package.swift +28 -0
  3. package/README.md +2 -1
  4. package/android/build.gradle +17 -12
  5. package/dist/esm/bleClient.d.ts +278 -278
  6. package/dist/esm/bleClient.js +361 -361
  7. package/dist/esm/bleClient.js.map +1 -1
  8. package/dist/esm/config.d.ts +53 -53
  9. package/dist/esm/config.js +2 -2
  10. package/dist/esm/conversion.d.ts +56 -46
  11. package/dist/esm/conversion.js +134 -117
  12. package/dist/esm/conversion.js.map +1 -1
  13. package/dist/esm/definitions.d.ts +352 -352
  14. package/dist/esm/definitions.js +42 -42
  15. package/dist/esm/index.d.ts +5 -5
  16. package/dist/esm/index.js +5 -5
  17. package/dist/esm/plugin.d.ts +2 -2
  18. package/dist/esm/plugin.js +4 -4
  19. package/dist/esm/queue.d.ts +3 -3
  20. package/dist/esm/queue.js +17 -17
  21. package/dist/esm/queue.js.map +1 -1
  22. package/dist/esm/timeout.d.ts +1 -1
  23. package/dist/esm/timeout.js +9 -9
  24. package/dist/esm/validators.d.ts +1 -1
  25. package/dist/esm/validators.js +11 -11
  26. package/dist/esm/validators.js.map +1 -1
  27. package/dist/esm/web.d.ts +57 -57
  28. package/dist/esm/web.js +403 -403
  29. package/dist/esm/web.js.map +1 -1
  30. package/dist/plugin.cjs.js +965 -947
  31. package/dist/plugin.cjs.js.map +1 -1
  32. package/dist/plugin.js +965 -947
  33. package/dist/plugin.js.map +1 -1
  34. package/ios/{Plugin → Sources/BluetoothLe}/DeviceListView.swift +1 -1
  35. package/ios/{Plugin → Sources/BluetoothLe}/DeviceManager.swift +1 -0
  36. package/ios/{Plugin → Sources/BluetoothLe}/Plugin.swift +39 -1
  37. package/ios/{Plugin → Sources/BluetoothLe}/ThreadSafeDictionary.swift +2 -0
  38. package/ios/Tests/BluetoothLeTests/ConversionTests.swift +55 -0
  39. package/ios/Tests/BluetoothLeTests/PluginTests.swift +27 -0
  40. package/package.json +18 -14
  41. package/ios/Plugin/Info.plist +0 -24
  42. package/ios/Plugin/Plugin.h +0 -10
  43. package/ios/Plugin/Plugin.m +0 -41
  44. /package/ios/{Plugin → Sources/BluetoothLe}/Conversion.swift +0 -0
  45. /package/ios/{Plugin → Sources/BluetoothLe}/Device.swift +0 -0
  46. /package/ios/{Plugin → Sources/BluetoothLe}/Logging.swift +0 -0
@@ -1,362 +1,362 @@
1
- import { Capacitor } from '@capacitor/core';
2
- import { dataViewToHexString, hexStringToDataView, toUint8Array, toHexString } from './conversion';
3
- import { BluetoothLe } from './plugin';
4
- import { getQueue } from './queue';
5
- import { parseUUID } from './validators';
6
- class BleClientClass {
7
- constructor() {
8
- this.scanListener = null;
9
- this.eventListeners = new Map();
10
- this.queue = getQueue(true);
11
- }
12
- enableQueue() {
13
- this.queue = getQueue(true);
14
- }
15
- disableQueue() {
16
- this.queue = getQueue(false);
17
- }
18
- async initialize(options) {
19
- await this.queue(async () => {
20
- await BluetoothLe.initialize(options);
21
- });
22
- }
23
- async isEnabled() {
24
- const enabled = await this.queue(async () => {
25
- const result = await BluetoothLe.isEnabled();
26
- return result.value;
27
- });
28
- return enabled;
29
- }
30
- async requestEnable() {
31
- await this.queue(async () => {
32
- await BluetoothLe.requestEnable();
33
- });
34
- }
35
- async enable() {
36
- await this.queue(async () => {
37
- await BluetoothLe.enable();
38
- });
39
- }
40
- async disable() {
41
- await this.queue(async () => {
42
- await BluetoothLe.disable();
43
- });
44
- }
45
- async startEnabledNotifications(callback) {
46
- await this.queue(async () => {
47
- var _a;
48
- const key = `onEnabledChanged`;
49
- await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
50
- const listener = await BluetoothLe.addListener(key, (result) => {
51
- callback(result.value);
52
- });
53
- this.eventListeners.set(key, listener);
54
- await BluetoothLe.startEnabledNotifications();
55
- });
56
- }
57
- async stopEnabledNotifications() {
58
- await this.queue(async () => {
59
- var _a;
60
- const key = `onEnabledChanged`;
61
- await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
62
- this.eventListeners.delete(key);
63
- await BluetoothLe.stopEnabledNotifications();
64
- });
65
- }
66
- async isLocationEnabled() {
67
- const enabled = await this.queue(async () => {
68
- const result = await BluetoothLe.isLocationEnabled();
69
- return result.value;
70
- });
71
- return enabled;
72
- }
73
- async openLocationSettings() {
74
- await this.queue(async () => {
75
- await BluetoothLe.openLocationSettings();
76
- });
77
- }
78
- async openBluetoothSettings() {
79
- await this.queue(async () => {
80
- await BluetoothLe.openBluetoothSettings();
81
- });
82
- }
83
- async openAppSettings() {
84
- await this.queue(async () => {
85
- await BluetoothLe.openAppSettings();
86
- });
87
- }
88
- async setDisplayStrings(displayStrings) {
89
- await this.queue(async () => {
90
- await BluetoothLe.setDisplayStrings(displayStrings);
91
- });
92
- }
93
- async requestDevice(options) {
94
- options = options ? this.validateRequestBleDeviceOptions(options) : undefined;
95
- const result = await this.queue(async () => {
96
- const device = await BluetoothLe.requestDevice(options);
97
- return device;
98
- });
99
- return result;
100
- }
101
- async requestLEScan(options, callback) {
102
- options = this.validateRequestBleDeviceOptions(options);
103
- await this.queue(async () => {
104
- var _a;
105
- await ((_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove());
106
- this.scanListener = await BluetoothLe.addListener('onScanResult', (resultInternal) => {
107
- const result = Object.assign(Object.assign({}, resultInternal), { manufacturerData: this.convertObject(resultInternal.manufacturerData), serviceData: this.convertObject(resultInternal.serviceData), rawAdvertisement: resultInternal.rawAdvertisement
108
- ? this.convertValue(resultInternal.rawAdvertisement)
109
- : undefined });
110
- callback(result);
111
- });
112
- await BluetoothLe.requestLEScan(options);
113
- });
114
- }
115
- async stopLEScan() {
116
- await this.queue(async () => {
117
- var _a;
118
- await ((_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove());
119
- this.scanListener = null;
120
- await BluetoothLe.stopLEScan();
121
- });
122
- }
123
- async getDevices(deviceIds) {
124
- if (!Array.isArray(deviceIds)) {
125
- throw new Error('deviceIds must be an array');
126
- }
127
- return this.queue(async () => {
128
- const result = await BluetoothLe.getDevices({ deviceIds });
129
- return result.devices;
130
- });
131
- }
132
- async getConnectedDevices(services) {
133
- if (!Array.isArray(services)) {
134
- throw new Error('services must be an array');
135
- }
136
- services = services.map(parseUUID);
137
- return this.queue(async () => {
138
- const result = await BluetoothLe.getConnectedDevices({ services });
139
- return result.devices;
140
- });
141
- }
142
- async getBondedDevices() {
143
- return this.queue(async () => {
144
- const result = await BluetoothLe.getBondedDevices();
145
- return result.devices;
146
- });
147
- }
148
- async connect(deviceId, onDisconnect, options) {
149
- await this.queue(async () => {
150
- var _a;
151
- if (onDisconnect) {
152
- const key = `disconnected|${deviceId}`;
153
- await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
154
- const listener = await BluetoothLe.addListener(key, () => {
155
- onDisconnect(deviceId);
156
- });
157
- this.eventListeners.set(key, listener);
158
- }
159
- await BluetoothLe.connect(Object.assign({ deviceId }, options));
160
- });
161
- }
162
- async createBond(deviceId, options) {
163
- await this.queue(async () => {
164
- await BluetoothLe.createBond(Object.assign({ deviceId }, options));
165
- });
166
- }
167
- async isBonded(deviceId) {
168
- const isBonded = await this.queue(async () => {
169
- const result = await BluetoothLe.isBonded({ deviceId });
170
- return result.value;
171
- });
172
- return isBonded;
173
- }
174
- async disconnect(deviceId) {
175
- await this.queue(async () => {
176
- await BluetoothLe.disconnect({ deviceId });
177
- });
178
- }
179
- async getServices(deviceId) {
180
- const services = await this.queue(async () => {
181
- const result = await BluetoothLe.getServices({ deviceId });
182
- return result.services;
183
- });
184
- return services;
185
- }
186
- async discoverServices(deviceId) {
187
- await this.queue(async () => {
188
- await BluetoothLe.discoverServices({ deviceId });
189
- });
190
- }
191
- async getMtu(deviceId) {
192
- const value = await this.queue(async () => {
193
- const result = await BluetoothLe.getMtu({ deviceId });
194
- return result.value;
195
- });
196
- return value;
197
- }
198
- async requestConnectionPriority(deviceId, connectionPriority) {
199
- await this.queue(async () => {
200
- await BluetoothLe.requestConnectionPriority({ deviceId, connectionPriority });
201
- });
202
- }
203
- async readRssi(deviceId) {
204
- const value = await this.queue(async () => {
205
- const result = await BluetoothLe.readRssi({ deviceId });
206
- return parseFloat(result.value);
207
- });
208
- return value;
209
- }
210
- async read(deviceId, service, characteristic, options) {
211
- service = parseUUID(service);
212
- characteristic = parseUUID(characteristic);
213
- const value = await this.queue(async () => {
214
- const result = await BluetoothLe.read(Object.assign({ deviceId,
215
- service,
216
- characteristic }, options));
217
- return this.convertValue(result.value);
218
- });
219
- return value;
220
- }
221
- async write(deviceId, service, characteristic, value, options) {
222
- service = parseUUID(service);
223
- characteristic = parseUUID(characteristic);
224
- return this.queue(async () => {
225
- if (!(value === null || value === void 0 ? void 0 : value.buffer)) {
226
- throw new Error('Invalid data.');
227
- }
228
- let writeValue = value;
229
- if (Capacitor.getPlatform() !== 'web') {
230
- // on native we can only write strings
231
- writeValue = dataViewToHexString(value);
232
- }
233
- await BluetoothLe.write(Object.assign({ deviceId,
234
- service,
235
- characteristic, value: writeValue }, options));
236
- });
237
- }
238
- async writeWithoutResponse(deviceId, service, characteristic, value, options) {
239
- service = parseUUID(service);
240
- characteristic = parseUUID(characteristic);
241
- await this.queue(async () => {
242
- if (!(value === null || value === void 0 ? void 0 : value.buffer)) {
243
- throw new Error('Invalid data.');
244
- }
245
- let writeValue = value;
246
- if (Capacitor.getPlatform() !== 'web') {
247
- // on native we can only write strings
248
- writeValue = dataViewToHexString(value);
249
- }
250
- await BluetoothLe.writeWithoutResponse(Object.assign({ deviceId,
251
- service,
252
- characteristic, value: writeValue }, options));
253
- });
254
- }
255
- async readDescriptor(deviceId, service, characteristic, descriptor, options) {
256
- service = parseUUID(service);
257
- characteristic = parseUUID(characteristic);
258
- descriptor = parseUUID(descriptor);
259
- const value = await this.queue(async () => {
260
- const result = await BluetoothLe.readDescriptor(Object.assign({ deviceId,
261
- service,
262
- characteristic,
263
- descriptor }, options));
264
- return this.convertValue(result.value);
265
- });
266
- return value;
267
- }
268
- async writeDescriptor(deviceId, service, characteristic, descriptor, value, options) {
269
- service = parseUUID(service);
270
- characteristic = parseUUID(characteristic);
271
- descriptor = parseUUID(descriptor);
272
- return this.queue(async () => {
273
- if (!(value === null || value === void 0 ? void 0 : value.buffer)) {
274
- throw new Error('Invalid data.');
275
- }
276
- let writeValue = value;
277
- if (Capacitor.getPlatform() !== 'web') {
278
- // on native we can only write strings
279
- writeValue = dataViewToHexString(value);
280
- }
281
- await BluetoothLe.writeDescriptor(Object.assign({ deviceId,
282
- service,
283
- characteristic,
284
- descriptor, value: writeValue }, options));
285
- });
286
- }
287
- async startNotifications(deviceId, service, characteristic, callback, options) {
288
- service = parseUUID(service);
289
- characteristic = parseUUID(characteristic);
290
- await this.queue(async () => {
291
- var _a;
292
- const key = `notification|${deviceId}|${service}|${characteristic}`;
293
- await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
294
- const listener = await BluetoothLe.addListener(key, (event) => {
295
- callback(this.convertValue(event === null || event === void 0 ? void 0 : event.value));
296
- });
297
- this.eventListeners.set(key, listener);
298
- await BluetoothLe.startNotifications(Object.assign({ deviceId,
299
- service,
300
- characteristic }, options));
301
- });
302
- }
303
- async stopNotifications(deviceId, service, characteristic) {
304
- service = parseUUID(service);
305
- characteristic = parseUUID(characteristic);
306
- await this.queue(async () => {
307
- var _a;
308
- const key = `notification|${deviceId}|${service}|${characteristic}`;
309
- await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
310
- this.eventListeners.delete(key);
311
- await BluetoothLe.stopNotifications({
312
- deviceId,
313
- service,
314
- characteristic,
315
- });
316
- });
317
- }
318
- validateRequestBleDeviceOptions(options) {
319
- if (options.services) {
320
- options.services = options.services.map(parseUUID);
321
- }
322
- if (options.optionalServices) {
323
- options.optionalServices = options.optionalServices.map(parseUUID);
324
- }
325
- if (options.serviceData && Capacitor.getPlatform() !== 'web') {
326
- // Native platforms: convert to hex strings
327
- options.serviceData = options.serviceData.map((filter) => (Object.assign(Object.assign({}, filter), { serviceUuid: parseUUID(filter.serviceUuid), dataPrefix: toHexString(filter.dataPrefix), mask: toHexString(filter.mask) })));
328
- }
329
- if (options.manufacturerData) {
330
- if (Capacitor.getPlatform() !== 'web') {
331
- // Native platforms: convert to hex strings
332
- options.manufacturerData = options.manufacturerData.map((filter) => (Object.assign(Object.assign({}, filter), { dataPrefix: toHexString(filter.dataPrefix), mask: toHexString(filter.mask) })));
333
- }
334
- else {
335
- // Web platform: convert to Uint8Array for Web Bluetooth API
336
- options.manufacturerData = options.manufacturerData.map((filter) => (Object.assign(Object.assign({}, filter), { dataPrefix: toUint8Array(filter.dataPrefix), mask: toUint8Array(filter.mask) })));
337
- }
338
- }
339
- return options;
340
- }
341
- convertValue(value) {
342
- if (typeof value === 'string') {
343
- return hexStringToDataView(value);
344
- }
345
- else if (value === undefined) {
346
- return new DataView(new ArrayBuffer(0));
347
- }
348
- return value;
349
- }
350
- convertObject(obj) {
351
- if (obj === undefined) {
352
- return undefined;
353
- }
354
- const result = {};
355
- for (const key of Object.keys(obj)) {
356
- result[key] = this.convertValue(obj[key]);
357
- }
358
- return result;
359
- }
360
- }
361
- export const BleClient = new BleClientClass();
1
+ import { Capacitor } from '@capacitor/core';
2
+ import { dataViewToHexString, hexStringToDataView, toUint8Array, toHexString } from './conversion';
3
+ import { BluetoothLe } from './plugin';
4
+ import { getQueue } from './queue';
5
+ import { parseUUID } from './validators';
6
+ class BleClientClass {
7
+ constructor() {
8
+ this.scanListener = null;
9
+ this.eventListeners = new Map();
10
+ this.queue = getQueue(true);
11
+ }
12
+ enableQueue() {
13
+ this.queue = getQueue(true);
14
+ }
15
+ disableQueue() {
16
+ this.queue = getQueue(false);
17
+ }
18
+ async initialize(options) {
19
+ await this.queue(async () => {
20
+ await BluetoothLe.initialize(options);
21
+ });
22
+ }
23
+ async isEnabled() {
24
+ const enabled = await this.queue(async () => {
25
+ const result = await BluetoothLe.isEnabled();
26
+ return result.value;
27
+ });
28
+ return enabled;
29
+ }
30
+ async requestEnable() {
31
+ await this.queue(async () => {
32
+ await BluetoothLe.requestEnable();
33
+ });
34
+ }
35
+ async enable() {
36
+ await this.queue(async () => {
37
+ await BluetoothLe.enable();
38
+ });
39
+ }
40
+ async disable() {
41
+ await this.queue(async () => {
42
+ await BluetoothLe.disable();
43
+ });
44
+ }
45
+ async startEnabledNotifications(callback) {
46
+ await this.queue(async () => {
47
+ var _a;
48
+ const key = `onEnabledChanged`;
49
+ await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
50
+ const listener = await BluetoothLe.addListener(key, (result) => {
51
+ callback(result.value);
52
+ });
53
+ this.eventListeners.set(key, listener);
54
+ await BluetoothLe.startEnabledNotifications();
55
+ });
56
+ }
57
+ async stopEnabledNotifications() {
58
+ await this.queue(async () => {
59
+ var _a;
60
+ const key = `onEnabledChanged`;
61
+ await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
62
+ this.eventListeners.delete(key);
63
+ await BluetoothLe.stopEnabledNotifications();
64
+ });
65
+ }
66
+ async isLocationEnabled() {
67
+ const enabled = await this.queue(async () => {
68
+ const result = await BluetoothLe.isLocationEnabled();
69
+ return result.value;
70
+ });
71
+ return enabled;
72
+ }
73
+ async openLocationSettings() {
74
+ await this.queue(async () => {
75
+ await BluetoothLe.openLocationSettings();
76
+ });
77
+ }
78
+ async openBluetoothSettings() {
79
+ await this.queue(async () => {
80
+ await BluetoothLe.openBluetoothSettings();
81
+ });
82
+ }
83
+ async openAppSettings() {
84
+ await this.queue(async () => {
85
+ await BluetoothLe.openAppSettings();
86
+ });
87
+ }
88
+ async setDisplayStrings(displayStrings) {
89
+ await this.queue(async () => {
90
+ await BluetoothLe.setDisplayStrings(displayStrings);
91
+ });
92
+ }
93
+ async requestDevice(options) {
94
+ options = options ? this.validateRequestBleDeviceOptions(options) : undefined;
95
+ const result = await this.queue(async () => {
96
+ const device = await BluetoothLe.requestDevice(options);
97
+ return device;
98
+ });
99
+ return result;
100
+ }
101
+ async requestLEScan(options, callback) {
102
+ options = this.validateRequestBleDeviceOptions(options);
103
+ await this.queue(async () => {
104
+ var _a;
105
+ await ((_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove());
106
+ this.scanListener = await BluetoothLe.addListener('onScanResult', (resultInternal) => {
107
+ const result = Object.assign(Object.assign({}, resultInternal), { manufacturerData: this.convertObject(resultInternal.manufacturerData), serviceData: this.convertObject(resultInternal.serviceData), rawAdvertisement: resultInternal.rawAdvertisement
108
+ ? this.convertValue(resultInternal.rawAdvertisement)
109
+ : undefined });
110
+ callback(result);
111
+ });
112
+ await BluetoothLe.requestLEScan(options);
113
+ });
114
+ }
115
+ async stopLEScan() {
116
+ await this.queue(async () => {
117
+ var _a;
118
+ await ((_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove());
119
+ this.scanListener = null;
120
+ await BluetoothLe.stopLEScan();
121
+ });
122
+ }
123
+ async getDevices(deviceIds) {
124
+ if (!Array.isArray(deviceIds)) {
125
+ throw new Error('deviceIds must be an array');
126
+ }
127
+ return this.queue(async () => {
128
+ const result = await BluetoothLe.getDevices({ deviceIds });
129
+ return result.devices;
130
+ });
131
+ }
132
+ async getConnectedDevices(services) {
133
+ if (!Array.isArray(services)) {
134
+ throw new Error('services must be an array');
135
+ }
136
+ services = services.map(parseUUID);
137
+ return this.queue(async () => {
138
+ const result = await BluetoothLe.getConnectedDevices({ services });
139
+ return result.devices;
140
+ });
141
+ }
142
+ async getBondedDevices() {
143
+ return this.queue(async () => {
144
+ const result = await BluetoothLe.getBondedDevices();
145
+ return result.devices;
146
+ });
147
+ }
148
+ async connect(deviceId, onDisconnect, options) {
149
+ await this.queue(async () => {
150
+ var _a;
151
+ if (onDisconnect) {
152
+ const key = `disconnected|${deviceId}`;
153
+ await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
154
+ const listener = await BluetoothLe.addListener(key, () => {
155
+ onDisconnect(deviceId);
156
+ });
157
+ this.eventListeners.set(key, listener);
158
+ }
159
+ await BluetoothLe.connect(Object.assign({ deviceId }, options));
160
+ });
161
+ }
162
+ async createBond(deviceId, options) {
163
+ await this.queue(async () => {
164
+ await BluetoothLe.createBond(Object.assign({ deviceId }, options));
165
+ });
166
+ }
167
+ async isBonded(deviceId) {
168
+ const isBonded = await this.queue(async () => {
169
+ const result = await BluetoothLe.isBonded({ deviceId });
170
+ return result.value;
171
+ });
172
+ return isBonded;
173
+ }
174
+ async disconnect(deviceId) {
175
+ await this.queue(async () => {
176
+ await BluetoothLe.disconnect({ deviceId });
177
+ });
178
+ }
179
+ async getServices(deviceId) {
180
+ const services = await this.queue(async () => {
181
+ const result = await BluetoothLe.getServices({ deviceId });
182
+ return result.services;
183
+ });
184
+ return services;
185
+ }
186
+ async discoverServices(deviceId) {
187
+ await this.queue(async () => {
188
+ await BluetoothLe.discoverServices({ deviceId });
189
+ });
190
+ }
191
+ async getMtu(deviceId) {
192
+ const value = await this.queue(async () => {
193
+ const result = await BluetoothLe.getMtu({ deviceId });
194
+ return result.value;
195
+ });
196
+ return value;
197
+ }
198
+ async requestConnectionPriority(deviceId, connectionPriority) {
199
+ await this.queue(async () => {
200
+ await BluetoothLe.requestConnectionPriority({ deviceId, connectionPriority });
201
+ });
202
+ }
203
+ async readRssi(deviceId) {
204
+ const value = await this.queue(async () => {
205
+ const result = await BluetoothLe.readRssi({ deviceId });
206
+ return parseFloat(result.value);
207
+ });
208
+ return value;
209
+ }
210
+ async read(deviceId, service, characteristic, options) {
211
+ service = parseUUID(service);
212
+ characteristic = parseUUID(characteristic);
213
+ const value = await this.queue(async () => {
214
+ const result = await BluetoothLe.read(Object.assign({ deviceId,
215
+ service,
216
+ characteristic }, options));
217
+ return this.convertValue(result.value);
218
+ });
219
+ return value;
220
+ }
221
+ async write(deviceId, service, characteristic, value, options) {
222
+ service = parseUUID(service);
223
+ characteristic = parseUUID(characteristic);
224
+ return this.queue(async () => {
225
+ if (!(value === null || value === void 0 ? void 0 : value.buffer)) {
226
+ throw new Error('Invalid data.');
227
+ }
228
+ let writeValue = value;
229
+ if (Capacitor.getPlatform() !== 'web') {
230
+ // on native we can only write strings
231
+ writeValue = dataViewToHexString(value);
232
+ }
233
+ await BluetoothLe.write(Object.assign({ deviceId,
234
+ service,
235
+ characteristic, value: writeValue }, options));
236
+ });
237
+ }
238
+ async writeWithoutResponse(deviceId, service, characteristic, value, options) {
239
+ service = parseUUID(service);
240
+ characteristic = parseUUID(characteristic);
241
+ await this.queue(async () => {
242
+ if (!(value === null || value === void 0 ? void 0 : value.buffer)) {
243
+ throw new Error('Invalid data.');
244
+ }
245
+ let writeValue = value;
246
+ if (Capacitor.getPlatform() !== 'web') {
247
+ // on native we can only write strings
248
+ writeValue = dataViewToHexString(value);
249
+ }
250
+ await BluetoothLe.writeWithoutResponse(Object.assign({ deviceId,
251
+ service,
252
+ characteristic, value: writeValue }, options));
253
+ });
254
+ }
255
+ async readDescriptor(deviceId, service, characteristic, descriptor, options) {
256
+ service = parseUUID(service);
257
+ characteristic = parseUUID(characteristic);
258
+ descriptor = parseUUID(descriptor);
259
+ const value = await this.queue(async () => {
260
+ const result = await BluetoothLe.readDescriptor(Object.assign({ deviceId,
261
+ service,
262
+ characteristic,
263
+ descriptor }, options));
264
+ return this.convertValue(result.value);
265
+ });
266
+ return value;
267
+ }
268
+ async writeDescriptor(deviceId, service, characteristic, descriptor, value, options) {
269
+ service = parseUUID(service);
270
+ characteristic = parseUUID(characteristic);
271
+ descriptor = parseUUID(descriptor);
272
+ return this.queue(async () => {
273
+ if (!(value === null || value === void 0 ? void 0 : value.buffer)) {
274
+ throw new Error('Invalid data.');
275
+ }
276
+ let writeValue = value;
277
+ if (Capacitor.getPlatform() !== 'web') {
278
+ // on native we can only write strings
279
+ writeValue = dataViewToHexString(value);
280
+ }
281
+ await BluetoothLe.writeDescriptor(Object.assign({ deviceId,
282
+ service,
283
+ characteristic,
284
+ descriptor, value: writeValue }, options));
285
+ });
286
+ }
287
+ async startNotifications(deviceId, service, characteristic, callback, options) {
288
+ service = parseUUID(service);
289
+ characteristic = parseUUID(characteristic);
290
+ await this.queue(async () => {
291
+ var _a;
292
+ const key = `notification|${deviceId}|${service}|${characteristic}`;
293
+ await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
294
+ const listener = await BluetoothLe.addListener(key, (event) => {
295
+ callback(this.convertValue(event === null || event === void 0 ? void 0 : event.value));
296
+ });
297
+ this.eventListeners.set(key, listener);
298
+ await BluetoothLe.startNotifications(Object.assign({ deviceId,
299
+ service,
300
+ characteristic }, options));
301
+ });
302
+ }
303
+ async stopNotifications(deviceId, service, characteristic) {
304
+ service = parseUUID(service);
305
+ characteristic = parseUUID(characteristic);
306
+ await this.queue(async () => {
307
+ var _a;
308
+ const key = `notification|${deviceId}|${service}|${characteristic}`;
309
+ await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove());
310
+ this.eventListeners.delete(key);
311
+ await BluetoothLe.stopNotifications({
312
+ deviceId,
313
+ service,
314
+ characteristic,
315
+ });
316
+ });
317
+ }
318
+ validateRequestBleDeviceOptions(options) {
319
+ if (options.services) {
320
+ options.services = options.services.map(parseUUID);
321
+ }
322
+ if (options.optionalServices) {
323
+ options.optionalServices = options.optionalServices.map(parseUUID);
324
+ }
325
+ if (options.serviceData && Capacitor.getPlatform() !== 'web') {
326
+ // Native platforms: convert to hex strings
327
+ options.serviceData = options.serviceData.map((filter) => (Object.assign(Object.assign({}, filter), { serviceUuid: parseUUID(filter.serviceUuid), dataPrefix: toHexString(filter.dataPrefix), mask: toHexString(filter.mask) })));
328
+ }
329
+ if (options.manufacturerData) {
330
+ if (Capacitor.getPlatform() !== 'web') {
331
+ // Native platforms: convert to hex strings
332
+ options.manufacturerData = options.manufacturerData.map((filter) => (Object.assign(Object.assign({}, filter), { dataPrefix: toHexString(filter.dataPrefix), mask: toHexString(filter.mask) })));
333
+ }
334
+ else {
335
+ // Web platform: convert to Uint8Array for Web Bluetooth API
336
+ options.manufacturerData = options.manufacturerData.map((filter) => (Object.assign(Object.assign({}, filter), { dataPrefix: toUint8Array(filter.dataPrefix), mask: toUint8Array(filter.mask) })));
337
+ }
338
+ }
339
+ return options;
340
+ }
341
+ convertValue(value) {
342
+ if (typeof value === 'string') {
343
+ return hexStringToDataView(value);
344
+ }
345
+ else if (value === undefined) {
346
+ return new DataView(new ArrayBuffer(0));
347
+ }
348
+ return value;
349
+ }
350
+ convertObject(obj) {
351
+ if (obj === undefined) {
352
+ return undefined;
353
+ }
354
+ const result = {};
355
+ for (const key of Object.keys(obj)) {
356
+ result[key] = this.convertValue(obj[key]);
357
+ }
358
+ return result;
359
+ }
360
+ }
361
+ export const BleClient = new BleClientClass();
362
362
  //# sourceMappingURL=bleClient.js.map