nativescript-akylas-bluetooth
Version:
Connect to and interact with Bluetooth LE peripherals
209 lines • 9.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var common_1 = require("../common");
var ios_main_1 = require("./ios_main");
var CBPeripheralDelegateImpl_1 = require("./CBPeripheralDelegateImpl");
var CBCentralManagerDelegateImpl = (function (_super) {
__extends(CBCentralManagerDelegateImpl, _super);
function CBCentralManagerDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
CBCentralManagerDelegateImpl.new = function () {
return _super.new.call(this);
};
CBCentralManagerDelegateImpl.prototype.initWithCallback = function (owner, callback) {
this._owner = owner;
common_1.CLog(common_1.CLogTypes.info, "CBCentralManagerDelegateImpl.initWithCallback ---- this._owner: " + this._owner);
this._callback = callback;
return this;
};
CBCentralManagerDelegateImpl.prototype.centralManagerDidConnectPeripheral = function (central, peripheral) {
common_1.CLog(common_1.CLogTypes.info, "----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral: " + peripheral);
var UUID = peripheral.identifier.UUIDString;
var peri = this._owner.get().findPeripheral(UUID);
common_1.CLog(common_1.CLogTypes.info, "----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral: cached perio: " + peri);
var cb = this._owner.get()._connectCallbacks[UUID];
delete this._owner.get()._connectCallbacks[UUID];
var delegate = CBPeripheralDelegateImpl_1.CBPeripheralDelegateImpl.new().initWithCallback(this._owner, cb);
CFRetain(delegate);
peri.delegate = delegate;
common_1.CLog(common_1.CLogTypes.info, "----- CBCentralManagerDelegateImpl centralManager:didConnectPeripheral, let's discover service");
this._owner.get().onPeripheralConnected(peripheral);
peri.discoverServices(null);
};
CBCentralManagerDelegateImpl.prototype.centralManagerDidDisconnectPeripheralError = function (central, peripheral, error) {
var UUID = peripheral.identifier.UUIDString;
var cb = this._owner.get()._disconnectCallbacks[UUID];
if (cb) {
cb({
UUID: peripheral.identifier.UUIDString,
name: peripheral.name
});
delete this._owner.get()._disconnectCallbacks[UUID];
}
else {
common_1.CLog(common_1.CLogTypes.info, "***** centralManagerDidDisconnectPeripheralError() no disconnect callback found *****");
}
this._owner.get().onPeripheralDisconnected(peripheral);
peripheral.delegate = null;
};
CBCentralManagerDelegateImpl.prototype.centralManagerDidFailToConnectPeripheralError = function (central, peripheral, error) {
common_1.CLog(common_1.CLogTypes.info, "CBCentralManagerDelegate.centralManagerDidFailToConnectPeripheralError ----", central, peripheral, error);
};
CBCentralManagerDelegateImpl.prototype.centralManagerDidDiscoverPeripheralAdvertisementDataRSSI = function (central, peripheral, advData, RSSI) {
var UUIDString = peripheral.identifier.UUIDString;
common_1.CLog(common_1.CLogTypes.info, "CBCentralManagerDelegateImpl.centralManagerDidDiscoverPeripheralAdvertisementDataRSSI ---- " + peripheral.name + " @ " + UUIDString + " @ " + RSSI + " @ " + advData);
this._owner.get().adddDiscoverPeripheral(peripheral);
var advertismentData = new AdvertismentData(advData);
var payload = {
UUID: UUIDString,
name: peripheral.name,
localName: advertismentData.localName,
RSSI: RSSI,
advertismentData: advertismentData,
state: this._owner.get()._getState(peripheral.state),
manufacturerId: advertismentData.manufacturerId
};
this._owner.get()._advData[UUIDString] = advertismentData;
if (this._owner.get()._onDiscovered) {
this._owner.get()._onDiscovered(payload);
}
this._owner.get().sendEvent(ios_main_1.Bluetooth.device_discovered_event, payload);
};
CBCentralManagerDelegateImpl.prototype.centralManagerDidUpdateState = function (central) {
if (central.state === 2) {
common_1.CLog(common_1.CLogTypes.warning, "CBCentralManagerDelegateImpl.centralManagerDidUpdateState ---- This hardware does not support Bluetooth Low Energy.");
}
this._owner.get().sendEvent(ios_main_1.Bluetooth.bluetooth_status_event, {
state: central.state === 2 ? 'unsupported' : central.state === 5 ? 'on' : 'off'
});
};
CBCentralManagerDelegateImpl.prototype.centralManagerWillRestoreState = function (central, dict) {
common_1.CLog(common_1.CLogTypes.info, "CBCentralManagerDelegateImpl.centralManagerWillRestoreState ---- central: " + central + ", dict: " + dict);
};
CBCentralManagerDelegateImpl.ObjCProtocols = [CBCentralManagerDelegate];
return CBCentralManagerDelegateImpl;
}(NSObject));
exports.CBCentralManagerDelegateImpl = CBCentralManagerDelegateImpl;
var AdvertismentData = (function () {
function AdvertismentData(advData) {
this.advData = advData;
}
Object.defineProperty(AdvertismentData.prototype, "manufacturerData", {
get: function () {
var data = this.advData.objectForKey(CBAdvertisementDataManufacturerDataKey);
if (data) {
return ios_main_1.toArrayBuffer(data.subdataWithRange(NSMakeRange(2, data.length - 2)));
}
return undefined;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "data", {
get: function () {
return ios_main_1.toArrayBuffer(this.advData);
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "manufacturerId", {
get: function () {
var data = this.advData.objectForKey(CBAdvertisementDataManufacturerDataKey);
if (data) {
var manufacturerIdBuffer = ios_main_1.toArrayBuffer(data.subdataWithRange(NSMakeRange(0, 2)));
return new DataView(manufacturerIdBuffer, 0).getUint16(0, true);
}
return -1;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "txPowerLevel", {
get: function () {
return this.advData.objectForKey(CBAdvertisementDataTxPowerLevelKey) || Number.MIN_VALUE;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "localName", {
get: function () {
return this.advData.objectForKey(CBAdvertisementDataLocalNameKey);
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "flags", {
get: function () {
return -1;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "uuids", {
get: function () {
var result = [];
var serviceUuids = this.advData.objectForKey(CBAdvertisementDataServiceUUIDsKey);
if (serviceUuids) {
for (var i = 0; i < serviceUuids.count; i++) {
result.push(serviceUuids[i].toString());
}
}
return result;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "overtflow", {
get: function () {
var result = [];
var serviceUuids = this.advData.objectForKey(CBAdvertisementDataOverflowServiceUUIDsKey);
if (serviceUuids) {
for (var i = 0; i < serviceUuids.count; i++) {
result.push(ios_main_1.CBUUIDToString(serviceUuids[i]));
}
}
return result;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "solicitedServices", {
get: function () {
var result = [];
var serviceUuids = this.advData.objectForKey(CBAdvertisementDataSolicitedServiceUUIDsKey);
if (serviceUuids) {
for (var i = 0; i < serviceUuids.count; i++) {
result.push(ios_main_1.CBUUIDToString(serviceUuids[i]));
}
}
return result;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "connectable", {
get: function () {
return this.advData.objectForKey(CBAdvertisementDataIsConnectable);
},
enumerable: true,
configurable: true
});
Object.defineProperty(AdvertismentData.prototype, "serviceData", {
get: function () {
var result = {};
var obj = this.advData.objectForKey(CBAdvertisementDataServiceDataKey);
if (obj) {
obj.enumerateKeysAndObjectsUsingBlock(function (key, data) {
result[ios_main_1.CBUUIDToString(key)] = ios_main_1.toArrayBuffer(data);
});
}
return result;
},
enumerable: true,
configurable: true
});
return AdvertismentData;
}());
exports.AdvertismentData = AdvertismentData;
//# sourceMappingURL=CBCentralManagerDelegateImpl.js.map