@capacitor-community/bluetooth-le
Advanced tools
Comparing version 1.1.3 to 1.2.0
@@ -5,2 +5,16 @@ # Changelog | ||
## [1.2.0](https://github.com/capacitor-community/bluetooth-le/compare/v1.1.3...v1.2.0) (2021-08-21) | ||
### Features | ||
* **all:** add getDevices and getConnectedDevices ([768745c](https://github.com/capacitor-community/bluetooth-le/commit/768745cf38c61a88f356fee5a443e927b50636ef)) | ||
### Bug Fixes | ||
* fix type of ScanResult ([0b1ccfe](https://github.com/capacitor-community/bluetooth-le/commit/0b1ccfec98164252a5fe85f9fae0d33317d5d257)) | ||
* properly remove event listeners when stopping notifications ([145ec13](https://github.com/capacitor-community/bluetooth-le/commit/145ec13d254a07dbf64224c915c6155668a10d91)) | ||
* **web:** fix availability check ([965ff3d](https://github.com/capacitor-community/bluetooth-le/commit/965ff3d52c66232271854338f072bcaf3f19e34c)) | ||
### [1.1.3](https://github.com/capacitor-community/bluetooth-le/compare/v1.1.2...v1.1.3) (2021-08-05) | ||
@@ -7,0 +21,0 @@ |
@@ -150,2 +150,48 @@ { | ||
{ | ||
"name": "getDevices", | ||
"signature": "(deviceIds: string[]) => Promise<BleDevice[]>", | ||
"parameters": [ | ||
{ | ||
"name": "deviceIds", | ||
"docs": "List of device IDs, e.g. saved from a previous app run. No used on web.", | ||
"type": "string[]" | ||
} | ||
], | ||
"returns": "Promise<BleDevice[]>", | ||
"tags": [ | ||
{ | ||
"name": "param", | ||
"text": "deviceIds List of device IDs, e.g. saved from a previous app run. No used on web." | ||
} | ||
], | ||
"docs": "On iOS and web, if you want to connect to a previously connected device without scanning first, you can use `getDevice`.\nUses [retrievePeripherals](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1519127-retrieveperipherals) on iOS and\n[getDevices](https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/getDevices) on web.\nOn Android, you can directly connect to the device with the deviceId.", | ||
"complexTypes": [ | ||
"BleDevice" | ||
], | ||
"slug": "getdevices" | ||
}, | ||
{ | ||
"name": "getConnectedDevices", | ||
"signature": "(services: string[]) => Promise<BleDevice[]>", | ||
"parameters": [ | ||
{ | ||
"name": "services", | ||
"docs": "List of services to filter the devices by. If no service is specified, no devices will be returned. Only applies to iOS.", | ||
"type": "string[]" | ||
} | ||
], | ||
"returns": "Promise<BleDevice[]>", | ||
"tags": [ | ||
{ | ||
"name": "param", | ||
"text": "services List of services to filter the devices by. If no service is specified, no devices will be returned. Only applies to iOS." | ||
} | ||
], | ||
"docs": "Get a list of currently connected devices.\nUses [retrieveConnectedPeripherals](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1518924-retrieveconnectedperipherals) on iOS,\n[getConnectedDevices](https://developer.android.com/reference/android/bluetooth/BluetoothManager#getConnectedDevices(int)) on Android\nand [getDevices](https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/getDevices) on web.", | ||
"complexTypes": [ | ||
"BleDevice" | ||
], | ||
"slug": "getconnecteddevices" | ||
}, | ||
{ | ||
"name": "connect", | ||
@@ -677,3 +723,3 @@ "signature": "(deviceId: string, onDisconnect?: ((deviceId: string) => void) | undefined) => Promise<void>", | ||
"complexTypes": [], | ||
"type": "number" | ||
"type": "number | undefined" | ||
}, | ||
@@ -685,3 +731,3 @@ { | ||
"complexTypes": [], | ||
"type": "number" | ||
"type": "number | undefined" | ||
}, | ||
@@ -688,0 +734,0 @@ { |
@@ -49,2 +49,18 @@ import type { DisplayStrings } from './config'; | ||
/** | ||
* On iOS and web, if you want to connect to a previously connected device without scanning first, you can use `getDevice`. | ||
* Uses [retrievePeripherals](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1519127-retrieveperipherals) on iOS and | ||
* [getDevices](https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/getDevices) on web. | ||
* On Android, you can directly connect to the device with the deviceId. | ||
* @param deviceIds List of device IDs, e.g. saved from a previous app run. No used on web. | ||
*/ | ||
getDevices(deviceIds: string[]): Promise<BleDevice[]>; | ||
/** | ||
* Get a list of currently connected devices. | ||
* Uses [retrieveConnectedPeripherals](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1518924-retrieveconnectedperipherals) on iOS, | ||
* [getConnectedDevices](https://developer.android.com/reference/android/bluetooth/BluetoothManager#getConnectedDevices(int)) on Android | ||
* and [getDevices](https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/getDevices) on web. | ||
* @param services List of services to filter the devices by. If no service is specified, no devices will be returned. Only applies to iOS. | ||
*/ | ||
getConnectedDevices(services: string[]): Promise<BleDevice[]>; | ||
/** | ||
* Connect to a peripheral BLE device. For an example, see [usage](#usage). | ||
@@ -131,2 +147,4 @@ * @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) | ||
stopLEScan(): Promise<void>; | ||
getDevices(deviceIds: string[]): Promise<BleDevice[]>; | ||
getConnectedDevices(services: string[]): Promise<BleDevice[]>; | ||
connect(deviceId: string, onDisconnect?: (deviceId: string) => void): Promise<void>; | ||
@@ -133,0 +151,0 @@ createBond(deviceId: string): Promise<void>; |
@@ -55,2 +55,3 @@ import { Capacitor } from '@capacitor/core'; | ||
await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove()); | ||
this.eventListeners.delete(key); | ||
await BluetoothLe.stopEnabledNotifications(); | ||
@@ -75,6 +76,6 @@ }); | ||
await ((_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove()); | ||
this.scanListener = await BluetoothLe.addListener('onScanResult', (result) => { | ||
result.manufacturerData = this.convertObject(result.manufacturerData); | ||
result.serviceData = this.convertObject(result.serviceData); | ||
result.rawAdvertisement = result.rawAdvertisement ? this.convertValue(result.rawAdvertisement) : undefined; | ||
this.scanListener = await BluetoothLe.addListener('onScanResult', (resultInternal) => { | ||
const result = Object.assign(Object.assign({}, resultInternal), { manufacturerData: this.convertObject(resultInternal.manufacturerData), serviceData: this.convertObject(resultInternal.serviceData), rawAdvertisement: resultInternal.rawAdvertisement | ||
? this.convertValue(resultInternal.rawAdvertisement) | ||
: undefined }); | ||
callback(result); | ||
@@ -93,2 +94,14 @@ }); | ||
} | ||
async getDevices(deviceIds) { | ||
return this.queue(async () => { | ||
const result = await BluetoothLe.getDevices({ deviceIds }); | ||
return result.devices; | ||
}); | ||
} | ||
async getConnectedDevices(services) { | ||
return this.queue(async () => { | ||
const result = await BluetoothLe.getConnectedDevices({ services }); | ||
return result.devices; | ||
}); | ||
} | ||
async connect(deviceId, onDisconnect) { | ||
@@ -201,3 +214,3 @@ await this.queue(async () => { | ||
var _a; | ||
const key = `notification|${service}|${characteristic}`; | ||
const key = `notification|${deviceId}|${service}|${characteristic}`; | ||
await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove()); | ||
@@ -204,0 +217,0 @@ this.eventListeners.delete(key); |
@@ -72,2 +72,8 @@ import type { PluginListenerHandle } from '@capacitor/core'; | ||
} | ||
export interface GetDevicesOptions { | ||
deviceIds: string[]; | ||
} | ||
export interface GetConnectedDevicesOptions { | ||
services: string[]; | ||
} | ||
export interface ReadOptions { | ||
@@ -92,2 +98,5 @@ deviceId: string; | ||
} | ||
export interface GetDevicesResult { | ||
devices: BleDevice[]; | ||
} | ||
export interface ReadResult { | ||
@@ -103,4 +112,4 @@ /** | ||
localName?: string; | ||
rssi: number; | ||
txPower: number; | ||
rssi?: number; | ||
txPower?: number; | ||
manufacturerData?: { | ||
@@ -129,7 +138,7 @@ [key: string]: T; | ||
*/ | ||
rssi: number; | ||
rssi?: number; | ||
/** | ||
* Transmit power in dBm. A value of 127 indicates that it is not available. | ||
*/ | ||
txPower: number; | ||
txPower?: number; | ||
/** | ||
@@ -165,2 +174,4 @@ * Manufacturer data, key is a company identifier and value is the data. | ||
stopLEScan(): Promise<void>; | ||
getDevices(options: GetDevicesOptions): Promise<GetDevicesResult>; | ||
getConnectedDevices(options: GetConnectedDevicesOptions): Promise<GetDevicesResult>; | ||
addListener(eventName: 'onEnabledChanged', listenerFunc: (result: BooleanResult) => void): PluginListenerHandle; | ||
@@ -167,0 +178,0 @@ addListener(eventName: string, listenerFunc: (event: ReadResult) => void): PluginListenerHandle; |
/// <reference types="web-bluetooth" /> | ||
import { WebPlugin } from '@capacitor/core'; | ||
import type { BleDevice, BluetoothLePlugin, BooleanResult, DeviceIdOptions, ReadOptions, ReadResult, RequestBleDeviceOptions, WriteOptions } from './definitions'; | ||
import type { BleDevice, BluetoothLePlugin, BooleanResult, DeviceIdOptions, GetConnectedDevicesOptions, GetDevicesOptions, GetDevicesResult, ReadOptions, ReadResult, RequestBleDeviceOptions, WriteOptions } from './definitions'; | ||
export declare class BluetoothLeWeb extends WebPlugin implements BluetoothLePlugin { | ||
private deviceMap; | ||
private discoverdDevices; | ||
private discoveredDevices; | ||
private scan; | ||
@@ -17,4 +17,6 @@ private requestBleDeviceOptions; | ||
requestLEScan(options?: RequestBleDeviceOptions): Promise<void>; | ||
private onAdvertisemendReceived; | ||
private onAdvertisementReceived; | ||
stopLEScan(): Promise<void>; | ||
getDevices(_options: GetDevicesOptions): Promise<GetDevicesResult>; | ||
getConnectedDevices(_options: GetConnectedDevicesOptions): Promise<GetDevicesResult>; | ||
connect(options: DeviceIdOptions): Promise<void>; | ||
@@ -33,5 +35,5 @@ private onDisconnected; | ||
private getFilters; | ||
private getDevice; | ||
private getDeviceFromMap; | ||
private getBleDevice; | ||
} | ||
export declare const BluetoothLe: BluetoothLeWeb; |
@@ -8,3 +8,3 @@ import { WebPlugin } from '@capacitor/core'; | ||
this.deviceMap = new Map(); | ||
this.discoverdDevices = new Map(); | ||
this.discoveredDevices = new Map(); | ||
this.scan = null; | ||
@@ -17,3 +17,4 @@ this.CONNECTION_TIMEOUT = 10000; | ||
} | ||
if (!navigator.bluetooth.getAvailability()) { | ||
const isAvailable = await navigator.bluetooth.getAvailability(); | ||
if (!isAvailable) { | ||
throw new Error('No Bluetooth radio available.'); | ||
@@ -50,5 +51,5 @@ } | ||
await this.stopLEScan(); | ||
this.discoverdDevices = new Map(); | ||
navigator.bluetooth.removeEventListener('advertisementreceived', this.onAdvertisemendReceived); | ||
navigator.bluetooth.addEventListener('advertisementreceived', this.onAdvertisemendReceived); | ||
this.discoveredDevices = new Map(); | ||
navigator.bluetooth.removeEventListener('advertisementreceived', this.onAdvertisementReceived); | ||
navigator.bluetooth.addEventListener('advertisementreceived', this.onAdvertisementReceived); | ||
this.scan = await navigator.bluetooth.requestLEScan({ | ||
@@ -60,3 +61,3 @@ filters: filters.length ? filters : undefined, | ||
} | ||
onAdvertisemendReceived(event) { | ||
onAdvertisementReceived(event) { | ||
var _a, _b; | ||
@@ -66,5 +67,5 @@ // do not use `this` in event listener | ||
BluetoothLe.deviceMap.set(deviceId, event.device); | ||
const isNew = !BluetoothLe.discoverdDevices.has(deviceId); | ||
const isNew = !BluetoothLe.discoveredDevices.has(deviceId); | ||
if (isNew || ((_a = BluetoothLe.requestBleDeviceOptions) === null || _a === void 0 ? void 0 : _a.allowDuplicates)) { | ||
BluetoothLe.discoverdDevices.set(deviceId, true); | ||
BluetoothLe.discoveredDevices.set(deviceId, true); | ||
const device = BluetoothLe.getBleDevice(event.device); | ||
@@ -90,5 +91,28 @@ const result = { | ||
} | ||
async getDevices(_options) { | ||
const devices = await navigator.bluetooth.getDevices(); | ||
const bleDevices = devices.map((device) => { | ||
this.deviceMap.set(device.id, device); | ||
const bleDevice = this.getBleDevice(device); | ||
return bleDevice; | ||
}); | ||
return { devices: bleDevices }; | ||
} | ||
async getConnectedDevices(_options) { | ||
const devices = await navigator.bluetooth.getDevices(); | ||
const bleDevices = devices | ||
.filter((device) => { | ||
var _a; | ||
return (_a = device.gatt) === null || _a === void 0 ? void 0 : _a.connected; | ||
}) | ||
.map((device) => { | ||
this.deviceMap.set(device.id, device); | ||
const bleDevice = this.getBleDevice(device); | ||
return bleDevice; | ||
}); | ||
return { devices: bleDevices }; | ||
} | ||
async connect(options) { | ||
var _a; | ||
const device = await this.getDevice(options.deviceId); | ||
const device = this.getDeviceFromMap(options.deviceId); | ||
device.removeEventListener('gattserverdisconnected', this.onDisconnected); | ||
@@ -129,7 +153,7 @@ device.addEventListener('gattserverdisconnected', this.onDisconnected); | ||
var _a; | ||
(_a = this.getDevice(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.disconnect(); | ||
(_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.disconnect(); | ||
} | ||
async getCharacteristic(options) { | ||
var _a; | ||
const service = await ((_a = this.getDevice(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.getPrimaryService(options === null || options === void 0 ? void 0 : options.service)); | ||
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)); | ||
return service === null || service === void 0 ? void 0 : service.getCharacteristic(options === null || options === void 0 ? void 0 : options.characteristic); | ||
@@ -201,6 +225,6 @@ } | ||
} | ||
getDevice(deviceId) { | ||
getDeviceFromMap(deviceId) { | ||
const device = this.deviceMap.get(deviceId); | ||
if (device === undefined) { | ||
throw new Error('Device not found. Call "requestDevice" or "requestLEScan" first.'); | ||
throw new Error('Device not found. Call "requestDevice", "requestLEScan" or "getDevices" first.'); | ||
} | ||
@@ -207,0 +231,0 @@ return device; |
@@ -168,2 +168,3 @@ 'use strict'; | ||
await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove()); | ||
this.eventListeners.delete(key); | ||
await BluetoothLe$1.stopEnabledNotifications(); | ||
@@ -188,6 +189,6 @@ }); | ||
await ((_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove()); | ||
this.scanListener = await BluetoothLe$1.addListener('onScanResult', (result) => { | ||
result.manufacturerData = this.convertObject(result.manufacturerData); | ||
result.serviceData = this.convertObject(result.serviceData); | ||
result.rawAdvertisement = result.rawAdvertisement ? this.convertValue(result.rawAdvertisement) : undefined; | ||
this.scanListener = await BluetoothLe$1.addListener('onScanResult', (resultInternal) => { | ||
const result = Object.assign(Object.assign({}, resultInternal), { manufacturerData: this.convertObject(resultInternal.manufacturerData), serviceData: this.convertObject(resultInternal.serviceData), rawAdvertisement: resultInternal.rawAdvertisement | ||
? this.convertValue(resultInternal.rawAdvertisement) | ||
: undefined }); | ||
callback(result); | ||
@@ -206,2 +207,14 @@ }); | ||
} | ||
async getDevices(deviceIds) { | ||
return this.queue(async () => { | ||
const result = await BluetoothLe$1.getDevices({ deviceIds }); | ||
return result.devices; | ||
}); | ||
} | ||
async getConnectedDevices(services) { | ||
return this.queue(async () => { | ||
const result = await BluetoothLe$1.getConnectedDevices({ services }); | ||
return result.devices; | ||
}); | ||
} | ||
async connect(deviceId, onDisconnect) { | ||
@@ -314,3 +327,3 @@ await this.queue(async () => { | ||
var _a; | ||
const key = `notification|${service}|${characteristic}`; | ||
const key = `notification|${deviceId}|${service}|${characteristic}`; | ||
await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove()); | ||
@@ -361,3 +374,3 @@ this.eventListeners.delete(key); | ||
this.deviceMap = new Map(); | ||
this.discoverdDevices = new Map(); | ||
this.discoveredDevices = new Map(); | ||
this.scan = null; | ||
@@ -370,3 +383,4 @@ this.CONNECTION_TIMEOUT = 10000; | ||
} | ||
if (!navigator.bluetooth.getAvailability()) { | ||
const isAvailable = await navigator.bluetooth.getAvailability(); | ||
if (!isAvailable) { | ||
throw new Error('No Bluetooth radio available.'); | ||
@@ -403,5 +417,5 @@ } | ||
await this.stopLEScan(); | ||
this.discoverdDevices = new Map(); | ||
navigator.bluetooth.removeEventListener('advertisementreceived', this.onAdvertisemendReceived); | ||
navigator.bluetooth.addEventListener('advertisementreceived', this.onAdvertisemendReceived); | ||
this.discoveredDevices = new Map(); | ||
navigator.bluetooth.removeEventListener('advertisementreceived', this.onAdvertisementReceived); | ||
navigator.bluetooth.addEventListener('advertisementreceived', this.onAdvertisementReceived); | ||
this.scan = await navigator.bluetooth.requestLEScan({ | ||
@@ -413,3 +427,3 @@ filters: filters.length ? filters : undefined, | ||
} | ||
onAdvertisemendReceived(event) { | ||
onAdvertisementReceived(event) { | ||
var _a, _b; | ||
@@ -419,5 +433,5 @@ // do not use `this` in event listener | ||
BluetoothLe.deviceMap.set(deviceId, event.device); | ||
const isNew = !BluetoothLe.discoverdDevices.has(deviceId); | ||
const isNew = !BluetoothLe.discoveredDevices.has(deviceId); | ||
if (isNew || ((_a = BluetoothLe.requestBleDeviceOptions) === null || _a === void 0 ? void 0 : _a.allowDuplicates)) { | ||
BluetoothLe.discoverdDevices.set(deviceId, true); | ||
BluetoothLe.discoveredDevices.set(deviceId, true); | ||
const device = BluetoothLe.getBleDevice(event.device); | ||
@@ -443,5 +457,28 @@ const result = { | ||
} | ||
async getDevices(_options) { | ||
const devices = await navigator.bluetooth.getDevices(); | ||
const bleDevices = devices.map((device) => { | ||
this.deviceMap.set(device.id, device); | ||
const bleDevice = this.getBleDevice(device); | ||
return bleDevice; | ||
}); | ||
return { devices: bleDevices }; | ||
} | ||
async getConnectedDevices(_options) { | ||
const devices = await navigator.bluetooth.getDevices(); | ||
const bleDevices = devices | ||
.filter((device) => { | ||
var _a; | ||
return (_a = device.gatt) === null || _a === void 0 ? void 0 : _a.connected; | ||
}) | ||
.map((device) => { | ||
this.deviceMap.set(device.id, device); | ||
const bleDevice = this.getBleDevice(device); | ||
return bleDevice; | ||
}); | ||
return { devices: bleDevices }; | ||
} | ||
async connect(options) { | ||
var _a; | ||
const device = await this.getDevice(options.deviceId); | ||
const device = this.getDeviceFromMap(options.deviceId); | ||
device.removeEventListener('gattserverdisconnected', this.onDisconnected); | ||
@@ -482,7 +519,7 @@ device.addEventListener('gattserverdisconnected', this.onDisconnected); | ||
var _a; | ||
(_a = this.getDevice(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.disconnect(); | ||
(_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.disconnect(); | ||
} | ||
async getCharacteristic(options) { | ||
var _a; | ||
const service = await ((_a = this.getDevice(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.getPrimaryService(options === null || options === void 0 ? void 0 : options.service)); | ||
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)); | ||
return service === null || service === void 0 ? void 0 : service.getCharacteristic(options === null || options === void 0 ? void 0 : options.characteristic); | ||
@@ -554,6 +591,6 @@ } | ||
} | ||
getDevice(deviceId) { | ||
getDeviceFromMap(deviceId) { | ||
const device = this.deviceMap.get(deviceId); | ||
if (device === undefined) { | ||
throw new Error('Device not found. Call "requestDevice" or "requestLEScan" first.'); | ||
throw new Error('Device not found. Call "requestDevice", "requestLEScan" or "getDevices" first.'); | ||
} | ||
@@ -560,0 +597,0 @@ return device; |
@@ -164,2 +164,3 @@ var capacitorCommunityBluetoothLe = (function (exports, core, throat) { | ||
await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove()); | ||
this.eventListeners.delete(key); | ||
await BluetoothLe$1.stopEnabledNotifications(); | ||
@@ -184,6 +185,6 @@ }); | ||
await ((_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove()); | ||
this.scanListener = await BluetoothLe$1.addListener('onScanResult', (result) => { | ||
result.manufacturerData = this.convertObject(result.manufacturerData); | ||
result.serviceData = this.convertObject(result.serviceData); | ||
result.rawAdvertisement = result.rawAdvertisement ? this.convertValue(result.rawAdvertisement) : undefined; | ||
this.scanListener = await BluetoothLe$1.addListener('onScanResult', (resultInternal) => { | ||
const result = Object.assign(Object.assign({}, resultInternal), { manufacturerData: this.convertObject(resultInternal.manufacturerData), serviceData: this.convertObject(resultInternal.serviceData), rawAdvertisement: resultInternal.rawAdvertisement | ||
? this.convertValue(resultInternal.rawAdvertisement) | ||
: undefined }); | ||
callback(result); | ||
@@ -202,2 +203,14 @@ }); | ||
} | ||
async getDevices(deviceIds) { | ||
return this.queue(async () => { | ||
const result = await BluetoothLe$1.getDevices({ deviceIds }); | ||
return result.devices; | ||
}); | ||
} | ||
async getConnectedDevices(services) { | ||
return this.queue(async () => { | ||
const result = await BluetoothLe$1.getConnectedDevices({ services }); | ||
return result.devices; | ||
}); | ||
} | ||
async connect(deviceId, onDisconnect) { | ||
@@ -310,3 +323,3 @@ await this.queue(async () => { | ||
var _a; | ||
const key = `notification|${service}|${characteristic}`; | ||
const key = `notification|${deviceId}|${service}|${characteristic}`; | ||
await ((_a = this.eventListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove()); | ||
@@ -357,3 +370,3 @@ this.eventListeners.delete(key); | ||
this.deviceMap = new Map(); | ||
this.discoverdDevices = new Map(); | ||
this.discoveredDevices = new Map(); | ||
this.scan = null; | ||
@@ -366,3 +379,4 @@ this.CONNECTION_TIMEOUT = 10000; | ||
} | ||
if (!navigator.bluetooth.getAvailability()) { | ||
const isAvailable = await navigator.bluetooth.getAvailability(); | ||
if (!isAvailable) { | ||
throw new Error('No Bluetooth radio available.'); | ||
@@ -399,5 +413,5 @@ } | ||
await this.stopLEScan(); | ||
this.discoverdDevices = new Map(); | ||
navigator.bluetooth.removeEventListener('advertisementreceived', this.onAdvertisemendReceived); | ||
navigator.bluetooth.addEventListener('advertisementreceived', this.onAdvertisemendReceived); | ||
this.discoveredDevices = new Map(); | ||
navigator.bluetooth.removeEventListener('advertisementreceived', this.onAdvertisementReceived); | ||
navigator.bluetooth.addEventListener('advertisementreceived', this.onAdvertisementReceived); | ||
this.scan = await navigator.bluetooth.requestLEScan({ | ||
@@ -409,3 +423,3 @@ filters: filters.length ? filters : undefined, | ||
} | ||
onAdvertisemendReceived(event) { | ||
onAdvertisementReceived(event) { | ||
var _a, _b; | ||
@@ -415,5 +429,5 @@ // do not use `this` in event listener | ||
BluetoothLe.deviceMap.set(deviceId, event.device); | ||
const isNew = !BluetoothLe.discoverdDevices.has(deviceId); | ||
const isNew = !BluetoothLe.discoveredDevices.has(deviceId); | ||
if (isNew || ((_a = BluetoothLe.requestBleDeviceOptions) === null || _a === void 0 ? void 0 : _a.allowDuplicates)) { | ||
BluetoothLe.discoverdDevices.set(deviceId, true); | ||
BluetoothLe.discoveredDevices.set(deviceId, true); | ||
const device = BluetoothLe.getBleDevice(event.device); | ||
@@ -439,5 +453,28 @@ const result = { | ||
} | ||
async getDevices(_options) { | ||
const devices = await navigator.bluetooth.getDevices(); | ||
const bleDevices = devices.map((device) => { | ||
this.deviceMap.set(device.id, device); | ||
const bleDevice = this.getBleDevice(device); | ||
return bleDevice; | ||
}); | ||
return { devices: bleDevices }; | ||
} | ||
async getConnectedDevices(_options) { | ||
const devices = await navigator.bluetooth.getDevices(); | ||
const bleDevices = devices | ||
.filter((device) => { | ||
var _a; | ||
return (_a = device.gatt) === null || _a === void 0 ? void 0 : _a.connected; | ||
}) | ||
.map((device) => { | ||
this.deviceMap.set(device.id, device); | ||
const bleDevice = this.getBleDevice(device); | ||
return bleDevice; | ||
}); | ||
return { devices: bleDevices }; | ||
} | ||
async connect(options) { | ||
var _a; | ||
const device = await this.getDevice(options.deviceId); | ||
const device = this.getDeviceFromMap(options.deviceId); | ||
device.removeEventListener('gattserverdisconnected', this.onDisconnected); | ||
@@ -478,7 +515,7 @@ device.addEventListener('gattserverdisconnected', this.onDisconnected); | ||
var _a; | ||
(_a = this.getDevice(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.disconnect(); | ||
(_a = this.getDeviceFromMap(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.disconnect(); | ||
} | ||
async getCharacteristic(options) { | ||
var _a; | ||
const service = await ((_a = this.getDevice(options.deviceId).gatt) === null || _a === void 0 ? void 0 : _a.getPrimaryService(options === null || options === void 0 ? void 0 : options.service)); | ||
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)); | ||
return service === null || service === void 0 ? void 0 : service.getCharacteristic(options === null || options === void 0 ? void 0 : options.characteristic); | ||
@@ -550,6 +587,6 @@ } | ||
} | ||
getDevice(deviceId) { | ||
getDeviceFromMap(deviceId) { | ||
const device = this.deviceMap.get(deviceId); | ||
if (device === undefined) { | ||
throw new Error('Device not found. Call "requestDevice" or "requestLEScan" first.'); | ||
throw new Error('Device not found. Call "requestDevice", "requestLEScan" or "getDevices" first.'); | ||
} | ||
@@ -556,0 +593,0 @@ return device; |
{ | ||
"name": "@capacitor-community/bluetooth-le", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "Capacitor plugin for Bluetooth Low Energy ", | ||
@@ -26,2 +26,4 @@ "main": "dist/plugin.cjs.js", | ||
"test": "jest", | ||
"test:coverage": "jest --coverage", | ||
"test:watch": "jest --watch", | ||
"version:dev": "npm version --no-git-tag-version $(git describe)", | ||
@@ -36,3 +38,3 @@ "release": "standard-version", | ||
"dependencies": { | ||
"@types/web-bluetooth": "^0.0.10", | ||
"@types/web-bluetooth": "^0.0.11", | ||
"throat": "^6.0.1" | ||
@@ -43,3 +45,3 @@ }, | ||
"@capacitor/cli": "^3.1.2", | ||
"@capacitor/core": "^3.0.2", | ||
"@capacitor/core": "^3.1.2", | ||
"@capacitor/docgen": "^0.0.17", | ||
@@ -50,3 +52,3 @@ "@capacitor/ios": "^3.1.2", | ||
"@ionic/swiftlint-config": "^1.1.2", | ||
"@types/jest": "^26.0.24", | ||
"@types/jest": "^27.0.1", | ||
"eslint": "^7.32.0", | ||
@@ -57,6 +59,6 @@ "jest": "^27.0.6", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.56.0", | ||
"rollup": "^2.56.2", | ||
"standard-version": "^9.3.1", | ||
"swiftlint": "^1.0.1", | ||
"ts-jest": "^27.0.4", | ||
"ts-jest": "^27.0.5", | ||
"typescript": "~4.3.5" | ||
@@ -63,0 +65,0 @@ }, |
@@ -53,2 +53,4 @@ <p align="center"><br><img src="https://user-images.githubusercontent.com/236501/85893648-1c92e880-b7a8-11ea-926d-95355b8175c7.png" width="128" height="128" /></p> | ||
- [`stopLEScan()`](#stoplescan) | ||
- [`getDevices(...)`](#getdevices) | ||
- [`getConnectedDevices(...)`](#getconnecteddevices) | ||
- [`connect(...)`](#connect) | ||
@@ -368,2 +370,40 @@ - [`createBond(...)`](#createbond) | ||
### getDevices(...) | ||
```typescript | ||
getDevices(deviceIds: string[]) => Promise<BleDevice[]> | ||
``` | ||
On iOS and web, if you want to connect to a previously connected device without scanning first, you can use `getDevice`. | ||
Uses [retrievePeripherals](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1519127-retrieveperipherals) on iOS and | ||
[getDevices](https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/getDevices) on web. | ||
On Android, you can directly connect to the device with the deviceId. | ||
| Param | Type | Description | | ||
| --------------- | --------------------- | ----------------------------------------------------------------------- | | ||
| **`deviceIds`** | <code>string[]</code> | List of device IDs, e.g. saved from a previous app run. No used on web. | | ||
**Returns:** <code>Promise<BleDevice[]></code> | ||
--- | ||
### getConnectedDevices(...) | ||
```typescript | ||
getConnectedDevices(services: string[]) => Promise<BleDevice[]> | ||
``` | ||
Get a list of currently connected devices. | ||
Uses [retrieveConnectedPeripherals](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1518924-retrieveconnectedperipherals) on iOS, | ||
[getConnectedDevices](<https://developer.android.com/reference/android/bluetooth/BluetoothManager#getConnectedDevices(int)>) on Android | ||
and [getDevices](https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/getDevices) on web. | ||
| Param | Type | Description | | ||
| -------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------ | | ||
| **`services`** | <code>string[]</code> | List of services to filter the devices by. If no service is specified, no devices will be returned. Only applies to iOS. | | ||
**Returns:** <code>Promise<BleDevice[]></code> | ||
--- | ||
### connect(...) | ||
@@ -370,0 +410,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
404538
3661
698
+ Added@types/web-bluetooth@0.0.11(transitive)
- Removed@types/web-bluetooth@0.0.10(transitive)
Updated@types/web-bluetooth@^0.0.11