@capacitor-community/bluetooth-le 7.1.0 → 7.2.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.
- package/README.md +23 -13
- package/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/BluetoothLe.kt +11 -2
- package/android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Device.kt +47 -0
- package/dist/docs.json +85 -41
- package/dist/esm/bleClient.d.ts +3 -2
- package/dist/esm/bleClient.js +3 -5
- package/dist/esm/bleClient.js.map +1 -1
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/conversion.js.map +1 -1
- package/dist/esm/definitions.d.ts +18 -6
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/queue.js.map +1 -1
- package/dist/esm/timeout.js.map +1 -1
- package/dist/esm/validators.js.map +1 -1
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +42 -46
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +42 -46
- package/dist/plugin.js.map +1 -1
- package/package.json +1 -1
package/dist/plugin.cjs.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var core = require('@capacitor/core');
|
|
6
4
|
|
|
7
5
|
/**
|
|
@@ -209,7 +207,7 @@ class BleClientClass {
|
|
|
209
207
|
await this.queue(async () => {
|
|
210
208
|
var _a;
|
|
211
209
|
const key = `onEnabledChanged`;
|
|
212
|
-
await ((_a = this.eventListeners.get(key)) === null || _a ===
|
|
210
|
+
await ((_a = this.eventListeners.get(key)) === null || _a === undefined ? undefined : _a.remove());
|
|
213
211
|
const listener = await BluetoothLe.addListener(key, (result) => {
|
|
214
212
|
callback(result.value);
|
|
215
213
|
});
|
|
@@ -221,7 +219,7 @@ class BleClientClass {
|
|
|
221
219
|
await this.queue(async () => {
|
|
222
220
|
var _a;
|
|
223
221
|
const key = `onEnabledChanged`;
|
|
224
|
-
await ((_a = this.eventListeners.get(key)) === null || _a ===
|
|
222
|
+
await ((_a = this.eventListeners.get(key)) === null || _a === undefined ? undefined : _a.remove());
|
|
225
223
|
this.eventListeners.delete(key);
|
|
226
224
|
await BluetoothLe.stopEnabledNotifications();
|
|
227
225
|
});
|
|
@@ -265,7 +263,7 @@ class BleClientClass {
|
|
|
265
263
|
options = this.validateRequestBleDeviceOptions(options);
|
|
266
264
|
await this.queue(async () => {
|
|
267
265
|
var _a;
|
|
268
|
-
await ((_a = this.scanListener) === null || _a ===
|
|
266
|
+
await ((_a = this.scanListener) === null || _a === undefined ? undefined : _a.remove());
|
|
269
267
|
this.scanListener = await BluetoothLe.addListener('onScanResult', (resultInternal) => {
|
|
270
268
|
const result = Object.assign(Object.assign({}, resultInternal), { manufacturerData: this.convertObject(resultInternal.manufacturerData), serviceData: this.convertObject(resultInternal.serviceData), rawAdvertisement: resultInternal.rawAdvertisement
|
|
271
269
|
? this.convertValue(resultInternal.rawAdvertisement)
|
|
@@ -278,7 +276,7 @@ class BleClientClass {
|
|
|
278
276
|
async stopLEScan() {
|
|
279
277
|
await this.queue(async () => {
|
|
280
278
|
var _a;
|
|
281
|
-
await ((_a = this.scanListener) === null || _a ===
|
|
279
|
+
await ((_a = this.scanListener) === null || _a === undefined ? undefined : _a.remove());
|
|
282
280
|
this.scanListener = null;
|
|
283
281
|
await BluetoothLe.stopLEScan();
|
|
284
282
|
});
|
|
@@ -313,7 +311,7 @@ class BleClientClass {
|
|
|
313
311
|
var _a;
|
|
314
312
|
if (onDisconnect) {
|
|
315
313
|
const key = `disconnected|${deviceId}`;
|
|
316
|
-
await ((_a = this.eventListeners.get(key)) === null || _a ===
|
|
314
|
+
await ((_a = this.eventListeners.get(key)) === null || _a === undefined ? undefined : _a.remove());
|
|
317
315
|
const listener = await BluetoothLe.addListener(key, () => {
|
|
318
316
|
onDisconnect(deviceId);
|
|
319
317
|
});
|
|
@@ -385,7 +383,7 @@ class BleClientClass {
|
|
|
385
383
|
service = parseUUID(service);
|
|
386
384
|
characteristic = parseUUID(characteristic);
|
|
387
385
|
return this.queue(async () => {
|
|
388
|
-
if (!(value === null || value ===
|
|
386
|
+
if (!(value === null || value === undefined ? undefined : value.buffer)) {
|
|
389
387
|
throw new Error('Invalid data.');
|
|
390
388
|
}
|
|
391
389
|
let writeValue = value;
|
|
@@ -402,7 +400,7 @@ class BleClientClass {
|
|
|
402
400
|
service = parseUUID(service);
|
|
403
401
|
characteristic = parseUUID(characteristic);
|
|
404
402
|
await this.queue(async () => {
|
|
405
|
-
if (!(value === null || value ===
|
|
403
|
+
if (!(value === null || value === undefined ? undefined : value.buffer)) {
|
|
406
404
|
throw new Error('Invalid data.');
|
|
407
405
|
}
|
|
408
406
|
let writeValue = value;
|
|
@@ -433,7 +431,7 @@ class BleClientClass {
|
|
|
433
431
|
characteristic = parseUUID(characteristic);
|
|
434
432
|
descriptor = parseUUID(descriptor);
|
|
435
433
|
return this.queue(async () => {
|
|
436
|
-
if (!(value === null || value ===
|
|
434
|
+
if (!(value === null || value === undefined ? undefined : value.buffer)) {
|
|
437
435
|
throw new Error('Invalid data.');
|
|
438
436
|
}
|
|
439
437
|
let writeValue = value;
|
|
@@ -447,22 +445,20 @@ class BleClientClass {
|
|
|
447
445
|
descriptor, value: writeValue }, options));
|
|
448
446
|
});
|
|
449
447
|
}
|
|
450
|
-
async startNotifications(deviceId, service, characteristic, callback) {
|
|
448
|
+
async startNotifications(deviceId, service, characteristic, callback, options) {
|
|
451
449
|
service = parseUUID(service);
|
|
452
450
|
characteristic = parseUUID(characteristic);
|
|
453
451
|
await this.queue(async () => {
|
|
454
452
|
var _a;
|
|
455
453
|
const key = `notification|${deviceId}|${service}|${characteristic}`;
|
|
456
|
-
await ((_a = this.eventListeners.get(key)) === null || _a ===
|
|
454
|
+
await ((_a = this.eventListeners.get(key)) === null || _a === undefined ? undefined : _a.remove());
|
|
457
455
|
const listener = await BluetoothLe.addListener(key, (event) => {
|
|
458
|
-
callback(this.convertValue(event === null || event ===
|
|
456
|
+
callback(this.convertValue(event === null || event === undefined ? undefined : event.value));
|
|
459
457
|
});
|
|
460
458
|
this.eventListeners.set(key, listener);
|
|
461
|
-
await BluetoothLe.startNotifications({
|
|
462
|
-
deviceId,
|
|
459
|
+
await BluetoothLe.startNotifications(Object.assign({ deviceId,
|
|
463
460
|
service,
|
|
464
|
-
characteristic,
|
|
465
|
-
});
|
|
461
|
+
characteristic }, options));
|
|
466
462
|
});
|
|
467
463
|
}
|
|
468
464
|
async stopNotifications(deviceId, service, characteristic) {
|
|
@@ -471,7 +467,7 @@ class BleClientClass {
|
|
|
471
467
|
await this.queue(async () => {
|
|
472
468
|
var _a;
|
|
473
469
|
const key = `notification|${deviceId}|${service}|${characteristic}`;
|
|
474
|
-
await ((_a = this.eventListeners.get(key)) === null || _a ===
|
|
470
|
+
await ((_a = this.eventListeners.get(key)) === null || _a === undefined ? undefined : _a.remove());
|
|
475
471
|
this.eventListeners.delete(key);
|
|
476
472
|
await BluetoothLe.stopNotifications({
|
|
477
473
|
deviceId,
|
|
@@ -579,7 +575,7 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
579
575
|
const filters = this.getFilters(options);
|
|
580
576
|
const device = await navigator.bluetooth.requestDevice({
|
|
581
577
|
filters: filters.length ? filters : undefined,
|
|
582
|
-
optionalServices: options === null || options ===
|
|
578
|
+
optionalServices: options === null || options === undefined ? undefined : options.optionalServices,
|
|
583
579
|
acceptAllDevices: filters.length === 0,
|
|
584
580
|
});
|
|
585
581
|
this.deviceMap.set(device.id, device);
|
|
@@ -596,7 +592,7 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
596
592
|
this.scan = await navigator.bluetooth.requestLEScan({
|
|
597
593
|
filters: filters.length ? filters : undefined,
|
|
598
594
|
acceptAllAdvertisements: filters.length === 0,
|
|
599
|
-
keepRepeatedDevices: options === null || options ===
|
|
595
|
+
keepRepeatedDevices: options === null || options === undefined ? undefined : options.allowDuplicates,
|
|
600
596
|
});
|
|
601
597
|
}
|
|
602
598
|
onAdvertisementReceived(event) {
|
|
@@ -604,7 +600,7 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
604
600
|
const deviceId = event.device.id;
|
|
605
601
|
this.deviceMap.set(deviceId, event.device);
|
|
606
602
|
const isNew = !this.discoveredDevices.has(deviceId);
|
|
607
|
-
if (isNew || ((_a = this.requestBleDeviceOptions) === null || _a ===
|
|
603
|
+
if (isNew || ((_a = this.requestBleDeviceOptions) === null || _a === undefined ? undefined : _a.allowDuplicates)) {
|
|
608
604
|
this.discoveredDevices.set(deviceId, true);
|
|
609
605
|
const device = this.getBleDevice(event.device);
|
|
610
606
|
const result = {
|
|
@@ -614,14 +610,14 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
614
610
|
txPower: event.txPower,
|
|
615
611
|
manufacturerData: mapToObject(event.manufacturerData),
|
|
616
612
|
serviceData: mapToObject(event.serviceData),
|
|
617
|
-
uuids: (_b = event.uuids) === null || _b ===
|
|
613
|
+
uuids: (_b = event.uuids) === null || _b === undefined ? undefined : _b.map(webUUIDToString),
|
|
618
614
|
};
|
|
619
615
|
this.notifyListeners('onScanResult', result);
|
|
620
616
|
}
|
|
621
617
|
}
|
|
622
618
|
async stopLEScan() {
|
|
623
619
|
var _a;
|
|
624
|
-
if ((_a = this.scan) === null || _a ===
|
|
620
|
+
if ((_a = this.scan) === null || _a === undefined ? undefined : _a.active) {
|
|
625
621
|
this.scan.stop();
|
|
626
622
|
}
|
|
627
623
|
this.scan = null;
|
|
@@ -642,7 +638,7 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
642
638
|
const bleDevices = devices
|
|
643
639
|
.filter((device) => {
|
|
644
640
|
var _a;
|
|
645
|
-
return (_a = device.gatt) === null || _a ===
|
|
641
|
+
return (_a = device.gatt) === null || _a === undefined ? undefined : _a.connected;
|
|
646
642
|
})
|
|
647
643
|
.map((device) => {
|
|
648
644
|
this.deviceMap.set(device.id, device);
|
|
@@ -670,7 +666,7 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
670
666
|
catch (error) {
|
|
671
667
|
// cancel pending connect call, does not work yet in chromium because of a bug:
|
|
672
668
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=684073
|
|
673
|
-
await ((_b = device.gatt) === null || _b ===
|
|
669
|
+
await ((_b = device.gatt) === null || _b === undefined ? undefined : _b.disconnect());
|
|
674
670
|
if (error === timeoutError) {
|
|
675
671
|
throw new Error('Connection timeout');
|
|
676
672
|
}
|
|
@@ -692,11 +688,11 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
692
688
|
}
|
|
693
689
|
async disconnect(options) {
|
|
694
690
|
var _a;
|
|
695
|
-
(_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a ===
|
|
691
|
+
(_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === undefined ? undefined : _a.disconnect();
|
|
696
692
|
}
|
|
697
693
|
async getServices(options) {
|
|
698
694
|
var _a, _b;
|
|
699
|
-
const services = (_b = (await ((_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a ===
|
|
695
|
+
const services = (_b = (await ((_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === undefined ? undefined : _a.getPrimaryServices()))) !== null && _b !== undefined ? _b : [];
|
|
700
696
|
const bleServices = [];
|
|
701
697
|
for (const service of services) {
|
|
702
698
|
const characteristics = await service.getCharacteristics();
|
|
@@ -738,12 +734,12 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
738
734
|
}
|
|
739
735
|
async getCharacteristic(options) {
|
|
740
736
|
var _a;
|
|
741
|
-
const service = await ((_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a ===
|
|
742
|
-
return service === null || service ===
|
|
737
|
+
const service = await ((_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === undefined ? undefined : _a.getPrimaryService(options === null || options === undefined ? undefined : options.service));
|
|
738
|
+
return service === null || service === undefined ? undefined : service.getCharacteristic(options === null || options === undefined ? undefined : options.characteristic);
|
|
743
739
|
}
|
|
744
740
|
async getDescriptor(options) {
|
|
745
741
|
const characteristic = await this.getCharacteristic(options);
|
|
746
|
-
return characteristic === null || characteristic ===
|
|
742
|
+
return characteristic === null || characteristic === undefined ? undefined : characteristic.getDescriptor(options === null || options === undefined ? undefined : options.descriptor);
|
|
747
743
|
}
|
|
748
744
|
async discoverServices(_options) {
|
|
749
745
|
throw this.unavailable('discoverServices is not available on web.');
|
|
@@ -759,7 +755,7 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
759
755
|
}
|
|
760
756
|
async read(options) {
|
|
761
757
|
const characteristic = await this.getCharacteristic(options);
|
|
762
|
-
const value = await (characteristic === null || characteristic ===
|
|
758
|
+
const value = await (characteristic === null || characteristic === undefined ? undefined : characteristic.readValue());
|
|
763
759
|
return { value };
|
|
764
760
|
}
|
|
765
761
|
async write(options) {
|
|
@@ -771,7 +767,7 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
771
767
|
else {
|
|
772
768
|
dataView = options.value;
|
|
773
769
|
}
|
|
774
|
-
await (characteristic === null || characteristic ===
|
|
770
|
+
await (characteristic === null || characteristic === undefined ? undefined : characteristic.writeValueWithResponse(dataView));
|
|
775
771
|
}
|
|
776
772
|
async writeWithoutResponse(options) {
|
|
777
773
|
const characteristic = await this.getCharacteristic(options);
|
|
@@ -782,11 +778,11 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
782
778
|
else {
|
|
783
779
|
dataView = options.value;
|
|
784
780
|
}
|
|
785
|
-
await (characteristic === null || characteristic ===
|
|
781
|
+
await (characteristic === null || characteristic === undefined ? undefined : characteristic.writeValueWithoutResponse(dataView));
|
|
786
782
|
}
|
|
787
783
|
async readDescriptor(options) {
|
|
788
784
|
const descriptor = await this.getDescriptor(options);
|
|
789
|
-
const value = await (descriptor === null || descriptor ===
|
|
785
|
+
const value = await (descriptor === null || descriptor === undefined ? undefined : descriptor.readValue());
|
|
790
786
|
return { value };
|
|
791
787
|
}
|
|
792
788
|
async writeDescriptor(options) {
|
|
@@ -798,43 +794,43 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
798
794
|
else {
|
|
799
795
|
dataView = options.value;
|
|
800
796
|
}
|
|
801
|
-
await (descriptor === null || descriptor ===
|
|
797
|
+
await (descriptor === null || descriptor === undefined ? undefined : descriptor.writeValue(dataView));
|
|
802
798
|
}
|
|
803
799
|
async startNotifications(options) {
|
|
804
800
|
const characteristic = await this.getCharacteristic(options);
|
|
805
|
-
characteristic === null || characteristic ===
|
|
806
|
-
characteristic === null || characteristic ===
|
|
807
|
-
await (characteristic === null || characteristic ===
|
|
801
|
+
characteristic === null || characteristic === undefined ? undefined : characteristic.removeEventListener('characteristicvaluechanged', this.onCharacteristicValueChangedCallback);
|
|
802
|
+
characteristic === null || characteristic === undefined ? undefined : characteristic.addEventListener('characteristicvaluechanged', this.onCharacteristicValueChangedCallback);
|
|
803
|
+
await (characteristic === null || characteristic === undefined ? undefined : characteristic.startNotifications());
|
|
808
804
|
}
|
|
809
805
|
onCharacteristicValueChanged(event) {
|
|
810
806
|
var _a, _b;
|
|
811
807
|
const characteristic = event.target;
|
|
812
|
-
const key = `notification|${(_a = characteristic.service) === null || _a ===
|
|
808
|
+
const key = `notification|${(_a = characteristic.service) === null || _a === undefined ? undefined : _a.device.id}|${(_b = characteristic.service) === null || _b === undefined ? undefined : _b.uuid}|${characteristic.uuid}`;
|
|
813
809
|
this.notifyListeners(key, {
|
|
814
810
|
value: characteristic.value,
|
|
815
811
|
});
|
|
816
812
|
}
|
|
817
813
|
async stopNotifications(options) {
|
|
818
814
|
const characteristic = await this.getCharacteristic(options);
|
|
819
|
-
await (characteristic === null || characteristic ===
|
|
815
|
+
await (characteristic === null || characteristic === undefined ? undefined : characteristic.stopNotifications());
|
|
820
816
|
}
|
|
821
817
|
getFilters(options) {
|
|
822
818
|
var _a, _b;
|
|
823
819
|
const filters = [];
|
|
824
|
-
for (const service of (_a = options === null || options ===
|
|
820
|
+
for (const service of (_a = options === null || options === undefined ? undefined : options.services) !== null && _a !== undefined ? _a : []) {
|
|
825
821
|
filters.push({
|
|
826
822
|
services: [service],
|
|
827
|
-
name: options === null || options ===
|
|
828
|
-
namePrefix: options === null || options ===
|
|
823
|
+
name: options === null || options === undefined ? undefined : options.name,
|
|
824
|
+
namePrefix: options === null || options === undefined ? undefined : options.namePrefix,
|
|
829
825
|
});
|
|
830
826
|
}
|
|
831
|
-
if (((options === null || options ===
|
|
827
|
+
if (((options === null || options === undefined ? undefined : options.name) || (options === null || options === undefined ? undefined : options.namePrefix)) && filters.length === 0) {
|
|
832
828
|
filters.push({
|
|
833
829
|
name: options.name,
|
|
834
830
|
namePrefix: options.namePrefix,
|
|
835
831
|
});
|
|
836
832
|
}
|
|
837
|
-
for (const manufacturerData of (_b = options === null || options ===
|
|
833
|
+
for (const manufacturerData of (_b = options === null || options === undefined ? undefined : options.manufacturerData) !== null && _b !== undefined ? _b : []) {
|
|
838
834
|
filters.push({
|
|
839
835
|
manufacturerData: [manufacturerData],
|
|
840
836
|
});
|
|
@@ -853,7 +849,7 @@ class BluetoothLeWeb extends core.WebPlugin {
|
|
|
853
849
|
const bleDevice = {
|
|
854
850
|
deviceId: device.id,
|
|
855
851
|
// use undefined instead of null if name is not available
|
|
856
|
-
name: (_a = device.name) !== null && _a !==
|
|
852
|
+
name: (_a = device.name) !== null && _a !== undefined ? _a : undefined,
|
|
857
853
|
};
|
|
858
854
|
return bleDevice;
|
|
859
855
|
}
|