New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@capacitor-community/bluetooth-le

Package Overview
Dependencies
Maintainers
20
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@capacitor-community/bluetooth-le - npm Package Compare versions

Comparing version 0.0.3 to 0.1.0-14-gaa111a7

android/src/main/java/com/capacitorjs/community/plugins/bluetoothle/Conversion.kt

10

CHANGELOG.md

@@ -0,1 +1,11 @@

## 0.1.0 (2020-12-28)
- add requestLEScan
- add stopLEScan
- add Android scan mode
- add namePrefix filter
- fix getting some events twice
- fix invalid deviceId on Android
- fix device initialization on iOS
## 0.0.3 (2020-12-14)

@@ -2,0 +12,0 @@

34

dist/esm/bleClient.d.ts
import { PluginListenerHandle } from '@capacitor/core';
import { BleDevice, RequestBleDeviceOptions } from './definitions';
import { BleDevice, RequestBleDeviceOptions, ScanResult } from './definitions';
export interface BleClientInterface {

@@ -11,3 +11,3 @@ /**

/**
* Request a peripheral BLE device to interact with. This will scan for available devices according to the filters provided in the options and show a dialog to pick a device.
* Request a peripheral BLE device to interact with. This will scan for available devices according to the filters in the options and show a dialog to pick a device.
* For an example, see [usage](#usage).

@@ -18,4 +18,16 @@ * @param options Device filters, see [RequestBleDeviceOptions](#RequestBleDeviceOptions)

/**
* Start scanning for BLE devices to interact with according to the filters in the options. The callback will be invoked on each device that is found.
* Scanning will continue until `stopLEScan` is called. For an example, see [usage](#usage).
* **NOTE**: Use with care on web platform, the required API is still behind a flag in most browsers.
* @param options
* @param callback
*/
requestLEScan(options: RequestBleDeviceOptions, callback: (result: ScanResult) => void): Promise<void>;
/**
* Stop scanning for BLE devices. For an example, see [usage](#usage).
*/
stopLEScan(): Promise<void>;
/**
* Connect to a peripheral BLE device. For an example, see [usage](#usage).
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice))
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan))
*/

@@ -25,3 +37,3 @@ connect(deviceId: string): Promise<void>;

* Disconnect from a peripheral BLE device. For an example, see [usage](#usage).
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice))
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan))
*/

@@ -31,3 +43,3 @@ disconnect(deviceId: string): Promise<void>;

* Read the value of a characteristic. For an example, see [usage](#usage).
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice))
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan))
* @param service UUID of the service (see [UUID format](#uuid-format))

@@ -39,3 +51,3 @@ * @param characteristic UUID of the characteristic (see [UUID format](#uuid-format))

* Write a value to a characteristic. For an example, see [usage](#usage).
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice))
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan))
* @param service UUID of the service (see [UUID format](#uuid-format))

@@ -48,3 +60,3 @@ * @param characteristic UUID of the characteristic (see [UUID format](#uuid-format))

* Start listening to changes of the value of a characteristic. For an example, see [usage](#usage).
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice))
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan))
* @param service UUID of the service (see [UUID format](#uuid-format))

@@ -57,3 +69,3 @@ * @param characteristic UUID of the characteristic (see [UUID format](#uuid-format))

* Stop listening to the changes of the value of a characteristic. For an example, see [usage](#usage).
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice))
* @param deviceId The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan))
* @param service UUID of the service (see [UUID format](#uuid-format))

@@ -65,8 +77,10 @@ * @param characteristic UUID of the characteristic (see [UUID format](#uuid-format))

declare class BleClientClass implements BleClientInterface {
scanListener: PluginListenerHandle | null;
notifyListeners: Map<string, PluginListenerHandle>;
initialize(): Promise<void>;
requestDevice(options?: RequestBleDeviceOptions): Promise<BleDevice>;
requestLEScan(options: RequestBleDeviceOptions, callback: (result: ScanResult) => void): Promise<void>;
stopLEScan(): Promise<void>;
connect(deviceId: string): Promise<void>;
disconnect(deviceId: string): Promise<void>;
private convertValue;
read(deviceId: string, service: string, characteristic: string): Promise<DataView>;

@@ -76,4 +90,6 @@ write(deviceId: string, service: string, characteristic: string, value: DataView): Promise<void>;

stopNotifications(deviceId: string, service: string, characteristic: string): Promise<void>;
private convertValue;
private convertObject;
}
export declare const BleClient: BleClientClass;
export {};

@@ -15,2 +15,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

constructor() {
this.scanListener = null;
this.notifyListeners = new Map();

@@ -29,2 +30,25 @@ }

}
requestLEScan(options, callback) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
(_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove();
this.scanListener = 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;
callback(result);
});
yield BluetoothLe.requestLEScan(options);
});
}
stopLEScan() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
(_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove();
this.scanListener = null;
yield BluetoothLe.stopLEScan();
});
}
connect(deviceId) {

@@ -40,11 +64,2 @@ return __awaiter(this, void 0, void 0, function* () {

}
convertValue(value) {
if (typeof value === 'string') {
return hexStringToDataView(value);
}
else if (value === undefined) {
return new DataView(new ArrayBuffer(0));
}
return value;
}
read(deviceId, service, characteristic) {

@@ -76,4 +91,6 @@ return __awaiter(this, void 0, void 0, function* () {

startNotifications(deviceId, service, characteristic, callback) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const key = `notification|${deviceId}|${service}|${characteristic}`;
(_a = this.notifyListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove();
const listener = BluetoothLe.addListener(key, (event) => {

@@ -103,4 +120,23 @@ callback(this.convertValue(event === null || event === void 0 ? void 0 : event.value));

}
convertValue(value) {
if (typeof value === 'string') {
return hexStringToDataView(value);
}
else if (value === undefined) {
return new DataView(new ArrayBuffer(0));
}
return value;
}
convertObject(obj) {
if (obj === undefined) {
return undefined;
}
const result = {};
for (const key of Object.keys(obj)) {
result[key] = this.convertValue(obj[key]);
}
return result;
}
}
export const BleClient = new BleClientClass();
//# sourceMappingURL=bleClient.js.map

@@ -13,1 +13,5 @@ export declare function numbersToDataView(value: number[]): DataView;

export declare function numberToUUID(value: number): string;
export declare function webUUIDToString(uuid: string | number): string;
export declare function mapToObject<V>(map?: Map<string | number, V>): {
[key: string]: V;
} | undefined;

@@ -39,2 +39,23 @@ export function numbersToDataView(value) {

}
export function webUUIDToString(uuid) {
if (typeof uuid === 'string') {
return uuid;
}
else if (typeof uuid === 'number') {
return numberToUUID(uuid);
}
else {
throw new Error('Invalid UUID');
}
}
export function mapToObject(map) {
const obj = {};
if (!map) {
return undefined;
}
map.forEach((value, key) => {
obj[key.toString()] = value;
});
return obj;
}
//# sourceMappingURL=conversion.js.map

@@ -21,2 +21,6 @@ import { PluginListenerHandle } from '@capacitor/core';

/**
* Filter devices by name prefix
*/
namePrefix?: string;
/**
* For web, all services that will be used have to be listed under services or optionalServices,

@@ -26,3 +30,33 @@ * e.g. [numberToUUID(0x180f)] (see [UUID format](#uuid-format))

optionalServices?: string[];
/**
* Normally scans will discard the second and subsequent advertisements from a single device.
* If you need to receive them, set allowDuplicates to true (only applicable in `requestLEScan`).
* (default: false)
*/
allowDuplicates?: boolean;
/**
* Android scan mode (default: ScanMode.SCAN_MODE_BALANCED)
*/
scanMode?: ScanMode;
}
/**
* Android scan mode
*/
export declare enum ScanMode {
/**
* Perform Bluetooth LE scan in low power mode. This mode is enforced if the scanning application is not in foreground.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_POWER
*/
SCAN_MODE_LOW_POWER = 0,
/**
* Perform Bluetooth LE scan in balanced power mode. (default) Scan results are returned at a rate that provides a good trade-off between scan frequency and power consumption.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_BALANCED
*/
SCAN_MODE_BALANCED = 1,
/**
* Scan using highest duty cycle. It's recommended to only use this mode when the application is running in the foreground.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_LATENCY
*/
SCAN_MODE_LOW_LATENCY = 2
}
export interface BleDevice {

@@ -49,2 +83,3 @@ /**

}
export declare type Data = DataView | string;
export interface WriteOptions {

@@ -58,3 +93,3 @@ deviceId: string;

*/
value: DataView | string;
value: Data;
}

@@ -66,8 +101,58 @@ export interface ReadResult {

*/
value?: DataView | string;
value?: Data;
}
export interface ScanResultInternal<T = Data> {
device: BleDevice;
rssi: number;
txPower: number;
manufacturerData?: {
[key: string]: T;
};
serviceData?: {
[key: string]: T;
};
uuids?: string[];
rawAdvertisement?: T;
}
export interface ScanResult {
/**
* The device that was found in the scan
*/
device: BleDevice;
/**
* Received Signal Strength Indication
*/
rssi: number;
/**
* Transmit power in dBm. A value of 127 indicates that it is not available.
*/
txPower: number;
/**
* Manufacturer data, key is a company identifier and value is the data
*/
manufacturerData?: {
[key: string]: DataView;
};
/**
* Service data, key is a service UUID and value is the data
*/
serviceData?: {
[key: string]: DataView;
};
/**
* Advertised services
*/
uuids?: string[];
/**
* Raw advertisement data (Android only)
*/
rawAdvertisement?: DataView;
}
export interface BluetoothLePlugin {
initialize(): Promise<void>;
requestDevice(options?: RequestBleDeviceOptions): Promise<BleDevice>;
requestLEScan(options?: RequestBleDeviceOptions): Promise<void>;
stopLEScan(): Promise<void>;
addListener(eventName: string, listenerFunc: (event: ReadResult) => void): PluginListenerHandle;
addListener(eventName: string, listenerFunc: (result: ScanResultInternal) => void): PluginListenerHandle;
connect(options: ConnectOptions): Promise<void>;

@@ -74,0 +159,0 @@ disconnect(options: ConnectOptions): Promise<void>;

@@ -1,2 +0,22 @@

export {};
/**
* Android scan mode
*/
export var ScanMode;
(function (ScanMode) {
/**
* Perform Bluetooth LE scan in low power mode. This mode is enforced if the scanning application is not in foreground.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_POWER
*/
ScanMode[ScanMode["SCAN_MODE_LOW_POWER"] = 0] = "SCAN_MODE_LOW_POWER";
/**
* Perform Bluetooth LE scan in balanced power mode. (default) Scan results are returned at a rate that provides a good trade-off between scan frequency and power consumption.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_BALANCED
*/
ScanMode[ScanMode["SCAN_MODE_BALANCED"] = 1] = "SCAN_MODE_BALANCED";
/**
* Scan using highest duty cycle. It's recommended to only use this mode when the application is running in the foreground.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_LATENCY
*/
ScanMode[ScanMode["SCAN_MODE_LOW_LATENCY"] = 2] = "SCAN_MODE_LOW_LATENCY";
})(ScanMode || (ScanMode = {}));
//# sourceMappingURL=definitions.js.map

@@ -6,6 +6,8 @@ /// <reference types="web-bluetooth" />

private deviceMap;
private scan;
constructor();
initialize(): Promise<void>;
requestDevice(options: RequestBleDeviceOptions): Promise<BleDevice>;
private getDevice;
requestDevice(options?: RequestBleDeviceOptions): Promise<BleDevice>;
requestLEScan(options?: RequestBleDeviceOptions): Promise<void>;
stopLEScan(): Promise<void>;
connect(options: ConnectOptions): Promise<void>;

@@ -18,4 +20,6 @@ disconnect(options: ConnectOptions): Promise<void>;

stopNotifications(options: ReadOptions): Promise<void>;
private getFilters;
private getDevice;
}
declare const BluetoothLe: BluetoothLeWeb;
export { BluetoothLe };

@@ -11,3 +11,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

import { WebPlugin } from '@capacitor/core';
import { hexStringToDataView } from './conversion';
import { hexStringToDataView, mapToObject, webUUIDToString, } from './conversion';
export class BluetoothLeWeb extends WebPlugin {

@@ -20,2 +20,3 @@ constructor() {

this.deviceMap = new Map();
this.scan = null;
}

@@ -33,16 +34,4 @@ initialize() {

requestDevice(options) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let filters = [];
for (let service of (_a = options === null || options === void 0 ? void 0 : options.services) !== null && _a !== void 0 ? _a : []) {
filters.push({
services: [service],
name: options === null || options === void 0 ? void 0 : options.name,
});
}
if ((options === null || options === void 0 ? void 0 : options.name) && filters.length === 0) {
filters.push({
name: options.name,
});
}
const filters = this.getFilters(options);
const device = yield navigator.bluetooth.requestDevice({

@@ -58,9 +47,41 @@ filters: filters.length ? filters : undefined,

}
getDevice(deviceId) {
const device = this.deviceMap.get(deviceId);
if (device === undefined) {
throw new Error('Device not found. Call "requestDevice" first.');
}
return device;
requestLEScan(options) {
return __awaiter(this, void 0, void 0, function* () {
const filters = this.getFilters(options);
yield this.stopLEScan();
navigator.bluetooth.addEventListener('advertisementreceived', (event) => {
var _a;
const isNew = !this.deviceMap.has(event.device.id);
this.deviceMap.set(event.device.id, event.device);
if (isNew || (options === null || options === void 0 ? void 0 : options.allowDuplicates)) {
const device = {
deviceId: event.device.id,
name: event.device.name,
};
const result = {
device,
rssi: event.rssi,
txPower: event.txPower,
manufacturerData: mapToObject(event.manufacturerData),
serviceData: mapToObject(event.serviceData),
uuids: (_a = event.uuids) === null || _a === void 0 ? void 0 : _a.map(webUUIDToString),
};
this.notifyListeners('onScanResult', result);
}
});
this.scan = yield navigator.bluetooth.requestLEScan({
filters: filters.length ? filters : undefined,
acceptAllAdvertisements: filters.length === 0,
keepRepeatedDevices: options === null || options === void 0 ? void 0 : options.allowDuplicates,
});
});
}
stopLEScan() {
return __awaiter(this, void 0, void 0, function* () {
if (this.scan && this.scan.active) {
this.scan.stop();
}
this.scan = null;
});
}
connect(options) {

@@ -123,2 +144,27 @@ var _a;

}
getFilters(options) {
var _a;
let filters = [];
for (let service of (_a = options === null || options === void 0 ? void 0 : options.services) !== null && _a !== void 0 ? _a : []) {
filters.push({
services: [service],
name: options === null || options === void 0 ? void 0 : options.name,
namePrefix: options === null || options === void 0 ? void 0 : options.namePrefix,
});
}
if (((options === null || options === void 0 ? void 0 : options.name) || (options === null || options === void 0 ? void 0 : options.namePrefix)) && filters.length === 0) {
filters.push({
name: options.name,
namePrefix: options.namePrefix,
});
}
return filters;
}
getDevice(deviceId) {
const device = this.deviceMap.get(deviceId);
if (device === undefined) {
throw new Error('Device not found. Call "requestDevice" or "requestLEScan" first.');
}
return device;
}
}

@@ -125,0 +171,0 @@ const BluetoothLe = new BluetoothLeWeb();

var capacitorCommunityBluetoothLe = (function (exports, core) {
'use strict';
/**
* Android scan mode
*/
(function (ScanMode) {
/**
* Perform Bluetooth LE scan in low power mode. This mode is enforced if the scanning application is not in foreground.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_POWER
*/
ScanMode[ScanMode["SCAN_MODE_LOW_POWER"] = 0] = "SCAN_MODE_LOW_POWER";
/**
* Perform Bluetooth LE scan in balanced power mode. (default) Scan results are returned at a rate that provides a good trade-off between scan frequency and power consumption.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_BALANCED
*/
ScanMode[ScanMode["SCAN_MODE_BALANCED"] = 1] = "SCAN_MODE_BALANCED";
/**
* Scan using highest duty cycle. It's recommended to only use this mode when the application is running in the foreground.
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_LATENCY
*/
ScanMode[ScanMode["SCAN_MODE_LOW_LATENCY"] = 2] = "SCAN_MODE_LOW_LATENCY";
})(exports.ScanMode || (exports.ScanMode = {}));
function numbersToDataView(value) {

@@ -42,2 +63,23 @@ return new DataView(Uint8Array.from(value).buffer);

}
function webUUIDToString(uuid) {
if (typeof uuid === 'string') {
return uuid;
}
else if (typeof uuid === 'number') {
return numberToUUID(uuid);
}
else {
throw new Error('Invalid UUID');
}
}
function mapToObject(map) {
const obj = {};
if (!map) {
return undefined;
}
map.forEach((value, key) => {
obj[key.toString()] = value;
});
return obj;
}

@@ -60,2 +102,3 @@ var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {

this.deviceMap = new Map();
this.scan = null;
}

@@ -73,16 +116,4 @@ initialize() {

requestDevice(options) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let filters = [];
for (let service of (_a = options === null || options === void 0 ? void 0 : options.services) !== null && _a !== void 0 ? _a : []) {
filters.push({
services: [service],
name: options === null || options === void 0 ? void 0 : options.name,
});
}
if ((options === null || options === void 0 ? void 0 : options.name) && filters.length === 0) {
filters.push({
name: options.name,
});
}
const filters = this.getFilters(options);
const device = yield navigator.bluetooth.requestDevice({

@@ -98,9 +129,41 @@ filters: filters.length ? filters : undefined,

}
getDevice(deviceId) {
const device = this.deviceMap.get(deviceId);
if (device === undefined) {
throw new Error('Device not found. Call "requestDevice" first.');
}
return device;
requestLEScan(options) {
return __awaiter(this, void 0, void 0, function* () {
const filters = this.getFilters(options);
yield this.stopLEScan();
navigator.bluetooth.addEventListener('advertisementreceived', (event) => {
var _a;
const isNew = !this.deviceMap.has(event.device.id);
this.deviceMap.set(event.device.id, event.device);
if (isNew || (options === null || options === void 0 ? void 0 : options.allowDuplicates)) {
const device = {
deviceId: event.device.id,
name: event.device.name,
};
const result = {
device,
rssi: event.rssi,
txPower: event.txPower,
manufacturerData: mapToObject(event.manufacturerData),
serviceData: mapToObject(event.serviceData),
uuids: (_a = event.uuids) === null || _a === void 0 ? void 0 : _a.map(webUUIDToString),
};
this.notifyListeners('onScanResult', result);
}
});
this.scan = yield navigator.bluetooth.requestLEScan({
filters: filters.length ? filters : undefined,
acceptAllAdvertisements: filters.length === 0,
keepRepeatedDevices: options === null || options === void 0 ? void 0 : options.allowDuplicates,
});
});
}
stopLEScan() {
return __awaiter(this, void 0, void 0, function* () {
if (this.scan && this.scan.active) {
this.scan.stop();
}
this.scan = null;
});
}
connect(options) {

@@ -163,2 +226,27 @@ var _a;

}
getFilters(options) {
var _a;
let filters = [];
for (let service of (_a = options === null || options === void 0 ? void 0 : options.services) !== null && _a !== void 0 ? _a : []) {
filters.push({
services: [service],
name: options === null || options === void 0 ? void 0 : options.name,
namePrefix: options === null || options === void 0 ? void 0 : options.namePrefix,
});
}
if (((options === null || options === void 0 ? void 0 : options.name) || (options === null || options === void 0 ? void 0 : options.namePrefix)) && filters.length === 0) {
filters.push({
name: options.name,
namePrefix: options.namePrefix,
});
}
return filters;
}
getDevice(deviceId) {
const device = this.deviceMap.get(deviceId);
if (device === undefined) {
throw new Error('Device not found. Call "requestDevice" or "requestLEScan" first.');
}
return device;
}
}

@@ -180,2 +268,3 @@ const BluetoothLe = new BluetoothLeWeb();

constructor() {
this.scanListener = null;
this.notifyListeners = new Map();

@@ -194,2 +283,25 @@ }

}
requestLEScan(options, callback) {
var _a;
return __awaiter$1(this, void 0, void 0, function* () {
(_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove();
this.scanListener = 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;
callback(result);
});
yield BluetoothLe$1.requestLEScan(options);
});
}
stopLEScan() {
var _a;
return __awaiter$1(this, void 0, void 0, function* () {
(_a = this.scanListener) === null || _a === void 0 ? void 0 : _a.remove();
this.scanListener = null;
yield BluetoothLe$1.stopLEScan();
});
}
connect(deviceId) {

@@ -205,11 +317,2 @@ return __awaiter$1(this, void 0, void 0, function* () {

}
convertValue(value) {
if (typeof value === 'string') {
return hexStringToDataView(value);
}
else if (value === undefined) {
return new DataView(new ArrayBuffer(0));
}
return value;
}
read(deviceId, service, characteristic) {

@@ -241,4 +344,6 @@ return __awaiter$1(this, void 0, void 0, function* () {

startNotifications(deviceId, service, characteristic, callback) {
var _a;
return __awaiter$1(this, void 0, void 0, function* () {
const key = `notification|${deviceId}|${service}|${characteristic}`;
(_a = this.notifyListeners.get(key)) === null || _a === void 0 ? void 0 : _a.remove();
const listener = BluetoothLe$1.addListener(key, (event) => {

@@ -268,2 +373,21 @@ callback(this.convertValue(event === null || event === void 0 ? void 0 : event.value));

}
convertValue(value) {
if (typeof value === 'string') {
return hexStringToDataView(value);
}
else if (value === undefined) {
return new DataView(new ArrayBuffer(0));
}
return value;
}
convertObject(obj) {
if (obj === undefined) {
return undefined;
}
const result = {};
for (const key of Object.keys(obj)) {
result[key] = this.convertValue(obj[key]);
}
return result;
}
}

@@ -279,5 +403,7 @@ const BleClient = new BleClientClass();

exports.hexStringToDataView = hexStringToDataView;
exports.mapToObject = mapToObject;
exports.numberToUUID = numberToUUID;
exports.numbersToDataView = numbersToDataView;
exports.textToDataView = textToDataView;
exports.webUUIDToString = webUUIDToString;

@@ -284,0 +410,0 @@ Object.defineProperty(exports, '__esModule', { value: true });

{
"name": "@capacitor-community/bluetooth-le",
"version": "0.0.3",
"version": "0.1.0-14-gaa111a7",
"description": "Capacitor Bluetooth Low Energy Plugin",
"main": "dist/plugin.js",
"main": "dist/index.js",
"browser": "dist/plugin.js",
"unpkg": "dist/plugin.js",
"module": "dist/esm/index.js",

@@ -27,7 +29,7 @@ "types": "dist/esm/index.d.ts",

"@capacitor/core": "^2.4.5",
"@capacitor/docgen": "^0.0.10",
"@capacitor/docgen": "^0.0.14",
"@capacitor/ios": "^2.4.5",
"@ionic/prettier-config": "^1.0.1",
"@ionic/swiftlint-config": "^1.1.2",
"@rollup/plugin-node-resolve": "^10.0.0",
"@rollup/plugin-node-resolve": "^11.0.1",
"@types/jest": "^26.0.19",

@@ -34,0 +36,0 @@ "jest": "^26.6.3",

@@ -9,4 +9,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>

<p align="center">
<img src="https://img.shields.io/maintenance/yes/2020?style=flat-square" />
<!-- <a href="https://github.com/capacitor-community/bluetooth-le/actions?query=workflow%3A%22CI%22"><img src="https://img.shields.io/github/workflow/status/capacitor-community/bluetooth-le/CI?style=flat-square" /></a> -->
<img src="https://img.shields.io/maintenance/yes/2021?style=flat-square" />
<a href="https://github.com/capacitor-community/bluetooth-le/actions?query=workflow%3A%22CI%22"><img src="https://img.shields.io/github/workflow/status/capacitor-community/bluetooth-le/CI?style=flat-square" /></a>
<a href="https://www.npmjs.com/package/@capacitor-community/bluetooth-le"><img src="https://img.shields.io/npm/l/@capacitor-community/bluetooth-le?style=flat-square" /></a>

@@ -17,3 +17,3 @@ <br>

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
<a href="#contributors-"><img src="https://img.shields.io/badge/all%20contributors-0-orange?style=flat-square" /></a>
<a href="#contributors-"><img src="https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square" /></a>
<!-- ALL-CONTRIBUTORS-BADGE:END -->

@@ -24,5 +24,5 @@ </p>

| Maintainer | GitHub | Social |
| -----------| -------| -------|
| Patrick Wespi | [pwespi](https://github.com/pwespi) | |
| Maintainer | GitHub | Social |
| ------------- | ----------------------------------- | ------ |
| Patrick Wespi | [pwespi](https://github.com/pwespi) | |

@@ -41,2 +41,4 @@ ## Introduction

- [`requestDevice(...)`](#requestdevice)
- [`requestLEScan(...)`](#requestlescan)
- [`stopLEScan()`](#stoplescan)
- [`connect(...)`](#connect)

@@ -49,2 +51,3 @@ - [`disconnect(...)`](#disconnect)

- [Interfaces](#interfaces)
- [Enums](#enums)

@@ -120,3 +123,3 @@ </docgen-index>

You can configure the strings that are displayed in the device selection dialog on iOS and Android:
You can configure the strings that are displayed in the device selection dialog on iOS and Android when using `requestDevice()`:

@@ -196,3 +199,3 @@ `./capacitor.config.json`:

services: [HEART_RATE_SERVICE],
optionalServices: [BATTERY_SERVICE],
optionalServices: [BATTERY_SERVICE, POLAR_PMD_SERVICE],
});

@@ -261,4 +264,32 @@

For a full app example, see the `./example` folder of this repository.
An example of using the scanning API:
```typescript
import { BleClient, numberToUUID } from '@capacitor-community/bluetooth-le';
const HEART_RATE_SERVICE = numberToUUID(0x180d);
export async function scan() {
try {
await BleClient.initialize();
await BleClient.requestLEScan(
{
services: [HEART_RATE_SERVICE],
},
result => {
console.log('received new scan result', result);
},
);
setTimeout(async () => {
await BleClient.stopLEScan();
console.log('stopped scanning');
}, 5000);
} catch (error) {
console.error(error);
}
}
```
## API

@@ -287,3 +318,3 @@

Request a peripheral BLE device to interact with. This will scan for available devices according to the filters provided in the options and show a dialog to pick a device.
Request a peripheral BLE device to interact with. This will scan for available devices according to the filters in the options and show a dialog to pick a device.
For an example, see [usage](#usage).

@@ -299,2 +330,29 @@

### requestLEScan(...)
```typescript
requestLEScan(options: RequestBleDeviceOptions, callback: (result: ScanResult) => void) => Promise<void>
```
Start scanning for BLE devices to interact with according to the filters in the options. The callback will be invoked on each device that is found.
Scanning will continue until `stopLEScan` is called. For an example, see [usage](#usage).
**NOTE**: Use with care on web platform, the required API is still behind a flag in most browsers.
| Param | Type |
| -------------- | --------------------------------------------------------------------------- |
| **`options`** | <code><a href="#requestbledeviceoptions">RequestBleDeviceOptions</a></code> |
| **`callback`** | <code>(result: <a href="#scanresult">ScanResult</a>) =&gt; void</code> |
---
### stopLEScan()
```typescript
stopLEScan() => Promise<void>
```
Stop scanning for BLE devices. For an example, see [usage](#usage).
---
### connect(...)

@@ -308,5 +366,5 @@

| Param | Type | Description |
| -------------- | ------------------- | --------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice)) |
| Param | Type | Description |
| -------------- | ------------------- | -------------------------------------------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) |

@@ -323,5 +381,5 @@ ---

| Param | Type | Description |
| -------------- | ------------------- | --------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice)) |
| Param | Type | Description |
| -------------- | ------------------- | -------------------------------------------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) |

@@ -338,7 +396,7 @@ ---

| Param | Type | Description |
| -------------------- | ------------------- | --------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice)) |
| **`service`** | <code>string</code> | UUID of the service (see [UUID format](#uuid-format)) |
| **`characteristic`** | <code>string</code> | UUID of the characteristic (see [UUID format](#uuid-format)) |
| Param | Type | Description |
| -------------------- | ------------------- | -------------------------------------------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) |
| **`service`** | <code>string</code> | UUID of the service (see [UUID format](#uuid-format)) |
| **`characteristic`** | <code>string</code> | UUID of the characteristic (see [UUID format](#uuid-format)) |

@@ -359,3 +417,3 @@ **Returns:** <code>Promise&lt;<a href="#dataview">DataView</a>&gt;</code>

| -------------------- | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice)) |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) |
| **`service`** | <code>string</code> | UUID of the service (see [UUID format](#uuid-format)) |

@@ -375,8 +433,8 @@ | **`characteristic`** | <code>string</code> | UUID of the characteristic (see [UUID format](#uuid-format)) |

| Param | Type | Description |
| -------------------- | ----------------------------------------------------------------- | --------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice)) |
| **`service`** | <code>string</code> | UUID of the service (see [UUID format](#uuid-format)) |
| **`characteristic`** | <code>string</code> | UUID of the characteristic (see [UUID format](#uuid-format)) |
| **`callback`** | <code>(value: <a href="#dataview">DataView</a>) =&gt; void</code> | Callback function to use when the value of the characteristic changes |
| Param | Type | Description |
| -------------------- | ----------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) |
| **`service`** | <code>string</code> | UUID of the service (see [UUID format](#uuid-format)) |
| **`characteristic`** | <code>string</code> | UUID of the characteristic (see [UUID format](#uuid-format)) |
| **`callback`** | <code>(value: <a href="#dataview">DataView</a>) =&gt; void</code> | Callback function to use when the value of the characteristic changes |

@@ -393,7 +451,7 @@ ---

| Param | Type | Description |
| -------------------- | ------------------- | --------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice)) |
| **`service`** | <code>string</code> | UUID of the service (see [UUID format](#uuid-format)) |
| **`characteristic`** | <code>string</code> | UUID of the characteristic (see [UUID format](#uuid-format)) |
| Param | Type | Description |
| -------------------- | ------------------- | -------------------------------------------------------------------------------------------------------------- |
| **`deviceId`** | <code>string</code> | The ID of the device to use (obtained from [requestDevice](#requestDevice) or [requestLEScan](#requestLEScan)) |
| **`service`** | <code>string</code> | UUID of the service (see [UUID format](#uuid-format)) |
| **`characteristic`** | <code>string</code> | UUID of the characteristic (see [UUID format](#uuid-format)) |

@@ -414,8 +472,23 @@ ---

| Prop | Type | Description |
| ---------------------- | --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`services`** | <code>string[]</code> | Filter devices by service UUIDs. UUIDs have to be specified as 128 bit UUID strings in lowercase, e.g. ['0000180d-0000-1000-8000-00805f9b34fb'] There is a helper function to convert numbers to UUIDs. e.g. [numberToUUID(0x180f)]. (see [UUID format](#uuid-format)) |
| **`name`** | <code>string</code> | Filter devices by name |
| **`optionalServices`** | <code>string[]</code> | For web, all services that will be used have to be listed under services or optionalServices, e.g. [numberToUUID(0x180f)] (see [UUID format](#uuid-format)) |
| Prop | Type | Description |
| ---------------------- | --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`services`** | <code>string[]</code> | Filter devices by service UUIDs. UUIDs have to be specified as 128 bit UUID strings in lowercase, e.g. ['0000180d-0000-1000-8000-00805f9b34fb'] There is a helper function to convert numbers to UUIDs. e.g. [numberToUUID(0x180f)]. (see [UUID format](#uuid-format)) |
| **`name`** | <code>string</code> | Filter devices by name |
| **`namePrefix`** | <code>string</code> | Filter devices by name prefix |
| **`optionalServices`** | <code>string[]</code> | For web, all services that will be used have to be listed under services or optionalServices, e.g. [numberToUUID(0x180f)] (see [UUID format](#uuid-format)) |
| **`allowDuplicates`** | <code>boolean</code> | Normally scans will discard the second and subsequent advertisements from a single device. If you need to receive them, set allowDuplicates to true (only applicable in `requestLEScan`). (default: false) |
| **`scanMode`** | <code><a href="#scanmode">ScanMode</a></code> | Android scan mode (default: <a href="#scanmode">ScanMode.SCAN_MODE_BALANCED</a>) |
#### ScanResult
| Prop | Type | Description |
| ---------------------- | ----------------------------------------------------------------- | ------------------------------------------------------------------------- |
| **`device`** | <code><a href="#bledevice">BleDevice</a></code> | The device that was found in the scan |
| **`rssi`** | <code>number</code> | Received Signal Strength Indication |
| **`txPower`** | <code>number</code> | Transmit power in dBm. A value of 127 indicates that it is not available. |
| **`manufacturerData`** | <code>{ [key: string]: <a href="#dataview">DataView</a>; }</code> | Manufacturer data, key is a company identifier and value is the data |
| **`serviceData`** | <code>{ [key: string]: <a href="#dataview">DataView</a>; }</code> | Service data, key is a service UUID and value is the data |
| **`uuids`** | <code>string[]</code> | Advertised services |
| **`rawAdvertisement`** | <code><a href="#dataview">DataView</a></code> | Raw advertisement data (Android only) |
#### DataView

@@ -463,2 +536,12 @@

### Enums
#### ScanMode
| Members | Value | Description |
| --------------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`SCAN_MODE_LOW_POWER`** | <code>0</code> | Perform Bluetooth LE scan in low power mode. This mode is enforced if the scanning application is not in foreground. https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_POWER |
| **`SCAN_MODE_BALANCED`** | <code>1</code> | Perform Bluetooth LE scan in balanced power mode. (default) Scan results are returned at a rate that provides a good trade-off between scan frequency and power consumption. https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_BALANCED |
| **`SCAN_MODE_LOW_LATENCY`** | <code>2</code> | Scan using highest duty cycle. It's recommended to only use this mode when the application is running in the foreground. https://developer.android.com/reference/android/bluetooth/le/ScanSettings#SCAN_MODE_LOW_LATENCY |
</docgen-api>

@@ -476,1 +559,21 @@

```
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://github.com/pwespi"><img src="https://avatars2.githubusercontent.com/u/24232962?v=4?s=100" width="100px;" alt=""/><br /><sub><b>pwespi</b></sub></a><br /><a href="https://github.com/capacitor-community/bluetooth-le/commits?author=pwespi" title="Code">💻</a></td>
</tr>
</table>
<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc