@nativescript-community/ble
Version:
Connect to and interact with Bluetooth LE peripherals.
177 lines • 7.45 kB
JavaScript
import Observable from '@nativescript-community/observable';
import { Trace } from '@nativescript/core';
import { BaseError } from 'make-error';
export const BleTraceCategory = 'NativescriptBle';
export var CLogTypes;
(function (CLogTypes) {
CLogTypes[CLogTypes["log"] = 0] = "log";
CLogTypes[CLogTypes["info"] = 1] = "info";
CLogTypes[CLogTypes["warning"] = 2] = "warning";
CLogTypes[CLogTypes["error"] = 3] = "error";
})(CLogTypes || (CLogTypes = {}));
export const CLog = (type, ...args) => {
Trace.write(args.map((a) => (a && typeof a === 'object' ? JSON.stringify(a) : a)).join(' '), BleTraceCategory, type);
};
export class BluetoothError extends BaseError {
constructor(message, properties) {
super(message);
if (properties) {
Object.assign(this, properties);
}
}
toString() {
return `[BluetoothError]:${this.message}${Object.keys(this).map((k) => {
if (k === 'message') {
return '';
}
const value = this[k];
return `,${k}: ${JSON.stringify(value)}`;
})}`;
}
}
export function bluetoothEnabled(target, propertyKey, descriptor) {
const originalMethod = descriptor.value; // save a reference to the original method
// NOTE: Do not use arrow syntax here. Use a function expression in
// order to use the correct value of `this` in this method (see notes below)
descriptor.value = function (...args) {
return this.isBluetoothEnabled()
.then(function (isEnabled) {
if (!isEnabled) {
if (Trace.isEnabled()) {
CLog(CLogTypes.info, `${originalMethod.name} ---- Bluetooth is not enabled.`);
}
return Promise.reject(new Error(BluetoothCommon.msg_not_enabled));
}
return null;
})
.then(() => originalMethod.apply(this, args));
};
return descriptor;
}
// BT assigned-numbers: https://www.bluetooth.com/specifications/assigned-numbers/
const patternBtAssignedNumbers = /0000(.{4})-0000-1000-8000-00805f9b34fb/i;
export function shortenUuidIfAssignedNumber(uuid) {
const lUUID = uuid.toLowerCase();
const matcher = lUUID.match(patternBtAssignedNumbers);
return matcher && matcher.length > 0 ? matcher[1] : lUUID;
}
export function prepareArgs(target, propertyKey, descriptor) {
const originalMethod = descriptor.value; // save a reference to the original method
// NOTE: Do not use arrow syntax here. Use a function expression in
// order to use the correct value of `this` in this method (see notes below)
descriptor.value = function (...args) {
const paramsToCheck = args[0];
if (paramsToCheck.hasOwnProperty) {
['serviceUUIDs', 'serviceUUID', 'characteristicUUID'].forEach(function (k) {
const value = paramsToCheck[k];
if (value) {
if (Array.isArray(value)) {
paramsToCheck[k] = paramsToCheck[k].map((v) => shortenUuidIfAssignedNumber(v));
}
else {
paramsToCheck[k] = shortenUuidIfAssignedNumber(paramsToCheck[k] ?? '');
}
}
});
}
return originalMethod.apply(this, args);
};
return descriptor;
}
export class BluetoothCommon extends Observable {
isGPSEnabled() {
return Promise.resolve(true); // we dont need to check for GPS in the bluetooth iOS module
}
enableGPS() {
return Promise.resolve(); // we dont need to check for GPS in the bluetooth iOS module
}
async hasLocationPermission() {
return true;
}
async requestLocationPermission() {
return true;
}
/**
* Notify events by name and optionally pass data
*/
sendEvent(eventName, data, msg) {
this.notify({
eventName,
object: this,
data,
message: msg
});
}
discoverAll(args) {
return this.discoverServices(args).then((resultS) => Promise.all(resultS.services.map((s) => this.discoverCharacteristics({ serviceUUID: s.UUID, ...args }).then((resultC) => (s.characteristics = resultC.characteristics)))).then(() => ({
services: resultS.services
})));
}
stop() { }
}
/*
* error messages
*/
BluetoothCommon.msg_not_enabled = 'bluetooth_not_enabled';
BluetoothCommon.msg_not_supported = 'bluetooth_not_supported';
BluetoothCommon.msg_cant_open_settings = 'cant_open_settings';
BluetoothCommon.msg_missing_parameter = 'missing_parameter';
BluetoothCommon.msg_no_peripheral = 'peripheral_not_found';
BluetoothCommon.msg_no_service = 'service_not_found';
BluetoothCommon.msg_no_characteristic = 'characteristic_not_found';
BluetoothCommon.msg_peripheral_not_connected = 'peripheral_not_connected';
BluetoothCommon.msg_peripheral_disconnected = 'peripheral_disconnected';
BluetoothCommon.msg_invalid_value = 'invalid_value';
BluetoothCommon.msg_error_function_call = 'error_function_call';
BluetoothCommon.msg_characteristic_cant_notify = 'characteristic_cant_notify';
BluetoothCommon.UUIDKey = 'UUID';
BluetoothCommon.serviceUUIDKey = 'serviceUUID';
BluetoothCommon.peripheralUUIDKey = 'peripheralUUID';
BluetoothCommon.characteristicUUIDKey = 'characteristicUUID';
/*
* String value for hooking into the bluetooth_status_event. This event fires when the bluetooth state changes.
*/
BluetoothCommon.bluetooth_status_event = 'bluetooth_status_event';
/*
* String value for hooking into the device_connected_event. This event fires when a device is connected.
*/
BluetoothCommon.device_connected_event = 'device_connected_event';
/*
* String value for hooking into the device_disconnected_event. This event fires when a device is disconnected.
*/
BluetoothCommon.device_disconnected_event = 'device_disconnected_event';
/*
* String value for hooking into the device_discovered_event. This event fires when a device is discovered.
*/
BluetoothCommon.device_discovered_event = 'device_discovered_event';
export var ScanMode;
(function (ScanMode) {
ScanMode[ScanMode["LOW_LATENCY"] = 0] = "LOW_LATENCY";
ScanMode[ScanMode["BALANCED"] = 1] = "BALANCED";
ScanMode[ScanMode["LOW_POWER"] = 2] = "LOW_POWER";
ScanMode[ScanMode["OPPORTUNISTIC"] = 3] = "OPPORTUNISTIC";
})(ScanMode || (ScanMode = {}));
export var MatchMode;
(function (MatchMode) {
MatchMode[MatchMode["AGGRESSIVE"] = 0] = "AGGRESSIVE";
MatchMode[MatchMode["STICKY"] = 1] = "STICKY";
})(MatchMode || (MatchMode = {}));
export var MatchNum;
(function (MatchNum) {
MatchNum[MatchNum["MAX_ADVERTISEMENT"] = 0] = "MAX_ADVERTISEMENT";
MatchNum[MatchNum["FEW_ADVERTISEMENT"] = 1] = "FEW_ADVERTISEMENT";
MatchNum[MatchNum["ONE_ADVERTISEMENT"] = 2] = "ONE_ADVERTISEMENT";
})(MatchNum || (MatchNum = {}));
export var CallbackType;
(function (CallbackType) {
CallbackType[CallbackType["ALL_MATCHES"] = 0] = "ALL_MATCHES";
CallbackType[CallbackType["FIRST_MATCH"] = 1] = "FIRST_MATCH";
CallbackType[CallbackType["MATCH_LOST"] = 2] = "MATCH_LOST";
})(CallbackType || (CallbackType = {}));
export var Phy;
(function (Phy) {
Phy[Phy["LE_1M"] = 0] = "LE_1M";
Phy[Phy["LE_CODED"] = 1] = "LE_CODED";
Phy[Phy["LE_ALL_SUPPORTED"] = 2] = "LE_ALL_SUPPORTED";
})(Phy || (Phy = {}));
//# sourceMappingURL=index.common.js.map