@iotize/device-com-ble.cordova
Version:
Bluetooth Low Energy (BLE) for IoTize modules Plugin
76 lines • 3.22 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());
});
};
//
// Copyright 2018 IoTize SAS Inc. Licensed under the MIT license.
//
// ble-com-protocol.ts
// device-com-ble.cordova BLE Cordova Plugin
//
import { ConnectionState, } from '@iotize/tap/protocol/api';
import { QueueComProtocol } from '@iotize/tap/protocol/core';
import { defer, throwError } from 'rxjs';
import { catchError, filter, first, tap } from 'rxjs/operators';
import { CordovaBLEError } from './cordova-ble-error';
import { debug } from './logger';
import { getIoTizeBleCordovaPlugin } from './utility';
export class BLEComProtocol extends QueueComProtocol {
constructor(deviceId, comProtocolOptions, cordovaInterfaceOverwrite) {
super();
this.deviceId = deviceId;
this.cordovaInterfaceOverwrite = cordovaInterfaceOverwrite;
if (comProtocolOptions) {
this.options = comProtocolOptions;
}
else {
this.options.connect.timeout = 60000;
}
}
/**
* Lazy reference to iotizeBLE.
* We don't want to reference iotizeBLE in constructor as it may be referenced
* before cordova plugin is loaded
*/
get pluginInterface() {
return this.cordovaInterfaceOverwrite || getIoTizeBleCordovaPlugin();
}
_connect(options) {
debug('_connect', options);
return this.pluginInterface
.connectAndDiscoverTapServices(this.deviceId)
.pipe(tap((state) => {
this.setConnectionState(state);
}), filter((state) => state === ConnectionState.CONNECTED), first());
}
_disconnect(options) {
debug('_disconnect', options);
return defer(() => this.pluginInterface.disConnect(this.deviceId));
}
write(data) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error('Method not implemented.');
});
}
read() {
return __awaiter(this, void 0, void 0, function* () {
throw new Error('Method not implemented.');
});
}
send(data, options) {
return defer(() => this.pluginInterface.send(this.deviceId, data)).pipe(catchError((err) => {
var _a;
if (((_a = err) === null || _a === void 0 ? void 0 : _a.code) ===
CordovaBLEError.Code.NotConnectedError) {
this.setConnectionState(ConnectionState.DISCONNECTED);
}
return throwError(err);
}));
}
}
//# sourceMappingURL=ble-com-protocol.js.map