nativescript-bluetooth
Version:
Connect to and interact with Bluetooth LE peripherals
213 lines • 9.26 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var nativescript_observable_1 = require("nativescript-observable");
var make_error_1 = require("make-error");
// declare var require;
var BluetoothUtil = /** @class */ (function () {
function BluetoothUtil() {
}
BluetoothUtil.debug = false;
return BluetoothUtil;
}());
exports.BluetoothUtil = BluetoothUtil;
var CLogTypes;
(function (CLogTypes) {
CLogTypes[CLogTypes["info"] = 0] = "info";
CLogTypes[CLogTypes["warning"] = 1] = "warning";
CLogTypes[CLogTypes["error"] = 2] = "error";
})(CLogTypes = exports.CLogTypes || (exports.CLogTypes = {}));
var BluetoothError = /** @class */ (function (_super) {
__extends(BluetoothError, _super);
function BluetoothError(message, properties) {
var _this = _super.call(this, message) || this;
if (properties) {
Object.assign(_this, properties);
}
return _this;
}
BluetoothError.prototype.toString = function () {
return "[BluetoothError]:" + this.message + ", " + this.method + ", " + this.status + ", " + JSON.stringify(this.arguments);
};
return BluetoothError;
}(make_error_1.BaseError));
exports.BluetoothError = BluetoothError;
exports.CLog = function (type) {
if (type === void 0) { type = 0; }
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
if (BluetoothUtil.debug) {
if (type === 0) {
// Info
console.log.apply(console, __spreadArrays(['[nativescript-bluetooth]'], args));
}
else if (type === 1) {
// Warning
console.warn.apply(console, __spreadArrays(['[nativescript-bluetooth]'], args));
}
else if (type === 2) {
console.error.apply(console, __spreadArrays(['[nativescript-bluetooth]'], args));
}
}
};
function bluetoothEnabled(target, propertyKey, descriptor) {
var 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 () {
var _this = this;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return this.isBluetoothEnabled()
.then(function (isEnabled) {
if (!isEnabled) {
exports.CLog(CLogTypes.info, originalMethod.name + " ---- Bluetooth is not enabled.");
return Promise.reject(new Error(BluetoothCommon.msg_not_enabled));
}
return null;
})
.then(function () { return originalMethod.apply(_this, args); });
};
return descriptor;
}
exports.bluetoothEnabled = bluetoothEnabled;
var pattern = /0000(.{4})-0000-1000-8000-00805f9b34fb/;
function prepareArgs(target, propertyKey, descriptor) {
var 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 () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var paramsToCheck = args[0];
if (paramsToCheck.hasOwnProperty) {
['serviceUUID', 'characteristicUUID'].forEach(function (k) {
if (paramsToCheck[k]) {
var matcher = paramsToCheck[k].match(pattern);
// console.log('test regex', paramsToCheck[k], matcher);
paramsToCheck[k] = (matcher && matcher.length > 0 ? matcher[1] : paramsToCheck[k]).toLowerCase();
}
});
}
return originalMethod.apply(this, args);
};
return descriptor;
}
exports.prepareArgs = prepareArgs;
var BluetoothCommon = /** @class */ (function (_super) {
__extends(BluetoothCommon, _super);
function BluetoothCommon() {
return _super !== null && _super.apply(this, arguments) || this;
}
Object.defineProperty(BluetoothCommon.prototype, "debug", {
set: function (value) {
BluetoothUtil.debug = value;
},
enumerable: true,
configurable: true
});
BluetoothCommon.prototype.isGPSEnabled = function () {
return Promise.resolve(true); // we dont need to check for GPS in the bluetooth iOS module
};
BluetoothCommon.prototype.enableGPS = function () {
return Promise.resolve(); // we dont need to check for GPS in the bluetooth iOS module
};
BluetoothCommon.prototype.requestLocationPermission = function () {
return Promise.resolve(true);
};
BluetoothCommon.prototype.hasLocationPermission = function () {
return Promise.resolve(true);
};
/**
* Notify events by name and optionally pass data
*/
BluetoothCommon.prototype.sendEvent = function (eventName, data, msg) {
this.notify({
eventName: eventName,
object: this,
data: data,
message: msg,
});
};
BluetoothCommon.prototype.discoverAll = function (args) {
var _this = this;
return this.discoverServices(args).then(function (resultS) {
return Promise.all(resultS.services.map(function (s) { return _this.discoverCharacteristics(__assign({ serviceUUID: s.UUID }, args)).then(function (resultC) { return (s.characteristics = resultC.characteristics); }); })).then(function () { return ({
services: resultS.services,
}); });
});
};
BluetoothCommon.prototype.stop = function () { };
/*
* 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';
return BluetoothCommon;
}(nativescript_observable_1.default));
exports.BluetoothCommon = BluetoothCommon;
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 = exports.ScanMode || (exports.ScanMode = {}));
var MatchMode;
(function (MatchMode) {
MatchMode[MatchMode["AGGRESSIVE"] = 0] = "AGGRESSIVE";
MatchMode[MatchMode["STICKY"] = 1] = "STICKY";
})(MatchMode = exports.MatchMode || (exports.MatchMode = {}));
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 = exports.MatchNum || (exports.MatchNum = {}));
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 = exports.CallbackType || (exports.CallbackType = {}));
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 = exports.Phy || (exports.Phy = {}));
//# sourceMappingURL=bluetooth.common.js.map