@iotize/device-com-ble.cordova
Version:
Bluetooth Low Energy (BLE) for IoTize modules Plugin
170 lines • 6.81 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { bufferToHexString, hexStringToBuffer as hexStringToBufferCore, } from "@iotize/common/byte-converter";
import { safeEnumValue } from "@iotize/common/utility";
import { ConnectionState } from "@iotize/tap/protocol/api";
import { Subject, defer } from "rxjs";
import { switchMap } from "rxjs/operators";
import { map } from "rxjs/operators";
import { CordovaBLEError } from "./cordova-ble-error";
function hexStringToBuffer(str) {
try {
return hexStringToBufferCore(str);
}
catch (err) {
throw CordovaBLEError.invalidNativeCallResult(str, err);
}
}
export class IoTizeBleCordovaPlugin {
checkAvailable() {
return this.execSingleResult("checkAvailable", []);
}
requestEnableBle() {
return this.execSingleResult("enable", []);
}
startScan(requestDeviceOptions) {
return this.execMultipleResult("startScan", [
JSON.stringify(requestDeviceOptions),
]);
}
stopScan() {
return this.execSingleResult("stopScan", []);
}
connect(deviceId, enableBleIfNot = true) {
return this.askBleEnable(enableBleIfNot).pipe(switchMap(() => {
return this.execMultipleResult("connect", [deviceId]).pipe(map((state) => {
if (!(state in ConnectionState)) {
console.warn(`Plugin native code returned an invalid connection state: "${state}".`);
}
return safeEnumValue(ConnectionState, state);
}));
}));
}
askBleEnable(askEnable) {
return defer(() => __awaiter(this, void 0, void 0, function* () {
if (askEnable) {
if (!(yield this.checkAvailable())) {
yield this.requestEnableBle();
}
}
}));
}
requestMTU(deviceId, mtu) {
return this.execSingleResult("requestMTU", [deviceId, mtu]);
}
connectAndDiscoverTapServices(deviceId, enableBleIfNot = true) {
return this.askBleEnable(enableBleIfNot).pipe(switchMap(() => {
return this.execMultipleResult("connectAndDiscoverTapServices", [deviceId]).pipe(map((state) => {
if (!(state in ConnectionState)) {
console.warn(`Plugin native code returned an invalid connection state: "${state}".`);
}
return safeEnumValue(ConnectionState, state);
}));
}));
}
disConnect(deviceId) {
return this.execSingleResult("disConnect", [deviceId]);
}
close(deviceId) {
return this.execSingleResult("close", [deviceId]);
}
isConnected(deviceId) {
return this.execSingleResult("isConnected", [deviceId]);
}
send(deviceId, data) {
return __awaiter(this, void 0, void 0, function* () {
const hexString = yield this.execSingleResult("sendRequest", [
deviceId,
bufferToHexString(data),
]);
return hexStringToBuffer(hexString);
});
}
getLastError() {
return this.execSingleResult("getLastError", []);
}
characteristicStartNotification(deviceId, serviceId, characId) {
return this.execSingleResult("characteristicStartNotification", [
deviceId,
serviceId,
characId,
]);
}
characteristicChanged(deviceId, serviceId, characId) {
return this.execMultipleResult("characteristicChanged", [
deviceId,
serviceId,
characId,
]).pipe(map((hexString) => {
return hexStringToBuffer(hexString);
}));
}
characteristicStopNotification(deviceId, serviceId, characId) {
return this.execSingleResult("characteristicStopNotification", [
deviceId,
serviceId,
characId,
]);
}
characteristicReadValue(deviceId, serviceId, characId) {
return __awaiter(this, void 0, void 0, function* () {
const hexString = yield this.execSingleResult("characteristicRead", [deviceId, serviceId, characId]);
return hexStringToBuffer(hexString);
});
}
characteristicWrite(deviceId, serviceId, characId, data) {
return this.execSingleResult("characteristicWrite", [
deviceId,
serviceId,
characId,
bufferToHexString(data),
]);
}
characteristicWriteWithoutResponse(deviceId, serviceId, characId, data) {
return this.execSingleResult("characteristicWriteWithoutResponse", [
deviceId,
serviceId,
characId,
bufferToHexString(data),
]);
}
discoverServices(deviceId) {
return __awaiter(this, void 0, void 0, function* () {
const services = yield this.execSingleResult("discoverServices", [deviceId]);
return services;
});
}
execSingleResult(methodName, args) {
return new Promise((resolve, reject) => {
cordova.exec(resolve, (errObject) => {
reject(objectErrorToError(errObject));
}, "BLECom", methodName, args);
});
}
execMultipleResult(methodName, args) {
const subject = new Subject();
cordova.exec((data) => {
subject.next(data);
}, (err) => {
subject.error(objectErrorToError(err));
}, "BLECom", methodName, args);
return subject.asObservable();
}
}
function objectErrorToError(errObject) {
if (typeof errObject !== "object" || !errObject.code || !errObject.message) {
return CordovaBLEError.invalidErrorResult(errObject);
}
if (!CordovaBLEError.isValidErrorCode(errObject.code)) {
return CordovaBLEError.invalidErrorCode(errObject);
}
return new CordovaBLEError(errObject.message, CordovaBLEError.Code[errObject.code]);
}
//# sourceMappingURL=iotize-ble-cordova-plugin.js.map