nativescript-ibeacon
Version:
iBeacon scanning support for Nativescript
188 lines • 8.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var nativescript_ibeacon_common_1 = require("./nativescript-ibeacon.common");
var LocationService = (function (_super) {
__extends(LocationService, _super);
function LocationService() {
var _this = _super.call(this) || this;
_this.delegate = null;
_this.locationManager = null;
_this.authorisationPromise = null;
console.log("constructor");
return _this;
}
LocationService.prototype.requestAuthorization = function (type) {
console.log("requestAuthorization: start");
var me = this;
return new Promise(function (granted, failed) {
console.log("requestAuthorization: in promise");
if (type == nativescript_ibeacon_common_1.BeaconLocationOptionsIOSAuthType.WhenInUse) {
console.log("requestAuthorization: when in use");
me.getLocationManager().requestWhenInUseAuthorization();
}
else {
console.log("requestAuthorization: always");
me.getLocationManager().requestAlwaysAuthorization();
}
me.authorisationPromise = { granted: granted, failed: failed };
});
};
LocationService.prototype.isAuthorised = function () {
var authstate = CLLocationManager.authorizationStatus();
console.log("Auth state = " + authstate);
return !(authstate == 0
|| authstate == 1
|| authstate == 2);
};
LocationService.prototype.getLocationManager = function () {
if (!this.locationManager) {
this.locationManager = new CLLocationManager();
this.locationManager.delegate = this;
}
return this.locationManager;
};
LocationService.prototype.bind = function () {
this.getLocationManager();
if (this.delegate && this.delegate.onBeaconManagerReady) {
this.delegate.onBeaconManagerReady();
}
};
LocationService.prototype.unbind = function () {
this.locationManager = null;
};
LocationService.prototype.startRanging = function (beaconRegion) {
console.log("startRanging");
var region = this.getCLBeaconRegion(beaconRegion);
this.getLocationManager().startRangingBeaconsInRegion(region);
};
LocationService.prototype.stopRanging = function (beaconRegion) {
console.log("stopRanging");
var region = this.getCLBeaconRegion(beaconRegion);
this.getLocationManager().stopRangingBeaconsInRegion(region);
};
LocationService.prototype.startMonitoring = function (beaconRegion) {
console.log("startMonitoring");
var region = this.getCLBeaconRegion(beaconRegion);
this.getLocationManager().startMonitoringForRegion(region);
};
LocationService.prototype.stopMonitoring = function (beaconRegion) {
console.log("stopMonitoring");
var region = this.getCLBeaconRegion(beaconRegion);
this.getLocationManager().stopMonitoringForRegion(region);
};
LocationService.prototype.getCLBeaconRegion = function (beaconRegion) {
var region = new CLBeaconRegion({
proximityUUID: new NSUUID({
UUIDString: beaconRegion.proximityUUID
}),
identifier: beaconRegion.identifier
});
return region;
};
LocationService.prototype.getBeaconRegion = function (clBeaconRegion) {
var beaconRegion = new nativescript_ibeacon_common_1.BeaconRegion(clBeaconRegion.identifier, clBeaconRegion.proximityUUID.UUIDString, clBeaconRegion.major, clBeaconRegion.minor);
return beaconRegion;
};
LocationService.prototype.getBeacon = function (clBeacon) {
var beacon = new nativescript_ibeacon_common_1.Beacon(clBeacon.proximityUUID.UUIDString, clBeacon.major, clBeacon.minor);
beacon.distance_proximity = clBeacon.proximity;
beacon.rssi = clBeacon.rssi;
beacon.txPower_accuracy = clBeacon.accuracy;
return beacon;
};
LocationService.prototype.locationManagerDidEnterRegion = function (manager, region) {
console.log("locationManagerDidEnterRegion");
if (this.delegate && this.delegate.didEnterRegion) {
this.delegate.didEnterRegion(this.getBeaconRegion(region));
}
};
;
LocationService.prototype.locationManagerDidExitRegion = function (manager, region) {
console.log("locationManagerDidExitRegion");
if (this.delegate && this.delegate.didExitRegion) {
this.delegate.didExitRegion(this.getBeaconRegion(region));
}
};
;
LocationService.prototype.locationManagerDidRangeBeaconsInRegion = function (manager, beacons, region) {
var jsBeacons = [];
for (var i = 0; i < beacons.count; i++) {
var beacon = this.getBeacon(beacons[i]);
jsBeacons.push(beacon);
}
if (this.delegate && this.delegate.didRangeBeaconsInRegion) {
this.delegate.didRangeBeaconsInRegion(this.getBeaconRegion(region), jsBeacons);
}
};
LocationService.prototype.locationManagerRangingBeaconsDidFailForRegionWithError = function (manager, region, error) {
console.log("locationManagerRangingBeaconsDidFailForRegionWithError: " + region.identifier + ": " + error.description);
if (this.delegate && this.delegate.didFailRangingBeaconsInRegion && error) {
this.delegate.didFailRangingBeaconsInRegion(this.getBeaconRegion(region), error.code, error.description);
}
};
LocationService.prototype.locationManagerDidChangeAuthorizationStatus = function (manager, status) {
console.log("requestAuthorization: init: " + status);
if (this.authorisationPromise != null && status != 0) {
console.log("requestAuthorization: check");
if (status == 1
|| status == 2) {
console.log("requestAuthorization: failed");
this.authorisationPromise.failed();
}
else {
console.log("requestAuthorization: granted");
this.authorisationPromise.granted();
}
console.log("requestAuthorization: reset");
this.authorisationPromise = null;
}
};
return LocationService;
}(NSObject));
LocationService.ObjCProtocols = [CLLocationManagerDelegate];
exports.LocationService = LocationService;
var NativescriptIbeacon = (function (_super) {
__extends(NativescriptIbeacon, _super);
function NativescriptIbeacon(beaconCallback, options) {
var _this = _super.call(this, beaconCallback, options) || this;
_this.locationService = null;
_this.locationService = new LocationService();
_this.locationService.delegate = beaconCallback;
return _this;
}
NativescriptIbeacon.prototype.requestAuthorization = function () {
console.log("requestAuthorization: " + this.options);
if (this.options) {
console.log("requestAuthorization from options: " + this.options.iOSAuthorisationType.toString());
return this.locationService.requestAuthorization(this.options.iOSAuthorisationType);
}
else {
console.log("requestAuthorization default (WhenInUse)");
return this.locationService.requestAuthorization(nativescript_ibeacon_common_1.BeaconLocationOptionsIOSAuthType.WhenInUse);
}
};
NativescriptIbeacon.prototype.isAuthorised = function () {
return this.locationService.isAuthorised();
};
NativescriptIbeacon.prototype.bind = function () {
this.locationService.bind();
};
NativescriptIbeacon.prototype.unbind = function () {
this.locationService.unbind();
};
NativescriptIbeacon.prototype.startRanging = function (beaconRegion) {
this.locationService.startRanging(beaconRegion);
};
NativescriptIbeacon.prototype.stopRanging = function (beaconRegion) {
this.locationService.stopRanging(beaconRegion);
};
NativescriptIbeacon.prototype.startMonitoring = function (beaconRegion) {
this.locationService.startMonitoring(beaconRegion);
};
NativescriptIbeacon.prototype.stopMonitoring = function (beaconRegion) {
this.locationService.stopMonitoring(beaconRegion);
};
return NativescriptIbeacon;
}(nativescript_ibeacon_common_1.Common));
exports.NativescriptIbeacon = NativescriptIbeacon;
//# sourceMappingURL=nativescript-ibeacon.ios.js.map