UNPKG

nativescript-ibeacon

Version:
227 lines 10.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var nativescript_ibeacon_common_1 = require("./nativescript-ibeacon.common"); var utils = require("utils/utils"); var permissions = require("nativescript-permissions"); var LocationService = (function (_super) { __extends(LocationService, _super); function LocationService(context) { var _this = _super.call(this) || this; _this.beaconManager = null; _this.delegate = null; _this.beaconManagerReady = false; _this.rangeNotifierAdded = false; _this.monitorNotifierAdded = false; _this.pendingBeaconRegion = null; _this.pendingBeaconMonitor = null; console.log("constructor"); _this.context = context; return global.__native(_this); } LocationService.prototype.bind = function () { this.getBeaconManager().bind(this); }; LocationService.prototype.unbind = function () { this.beaconManagerReady = false; this.getBeaconManager().unbind(this); }; LocationService.prototype.getBeaconManager = function () { if (this.beaconManager == null) { this.beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this.context); this.beaconManager.getBeaconParsers().add(new org.altbeacon.beacon.BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); console.log("beaconManager created"); } return this.beaconManager; }; LocationService.prototype.startRanging = function (beaconRegion) { if (this.beaconManagerReady) { console.log("startRanging"); try { console.log("startRangingBeaconsInRegion"); this.getBeaconManager().startRangingBeaconsInRegion(this.getRegionFromBeaconRegion(beaconRegion)); } catch (e) { } } else { console.log("startRanging stopped: beacon manager not ready"); this.pendingBeaconRegion = beaconRegion; this.bind(); } }; LocationService.prototype.stopRanging = function (beaconRegion) { if (this.beaconManagerReady) { console.log("stopRanging"); this.getBeaconManager().stopRangingBeaconsInRegion(this.getRegionFromBeaconRegion(beaconRegion)); } else { console.log("stopRanging stopped: beacon manager not ready"); } }; LocationService.prototype.startMonitoring = function (beaconRegion) { if (this.beaconManagerReady) { console.log("startMonitoring"); this.getBeaconManager().startMonitoringBeaconsInRegion(this.getRegionFromBeaconRegion(beaconRegion)); } else { console.log("startMonitoring stopped: beacon manager not ready"); this.pendingBeaconMonitor = beaconRegion; this.bind(); } }; LocationService.prototype.stopMonitoring = function (beaconRegion) { if (this.beaconManagerReady) { console.log("stopMonitoring"); this.getBeaconManager().stopMonitoringBeaconsInRegion(this.getRegionFromBeaconRegion(beaconRegion)); } else { console.log("stopMonitoring stopped: beacon manager not ready"); } }; LocationService.prototype.onBeaconServiceConnect = function () { console.log("onBeaconServiceConnect"); this.beaconManagerReady = true; var me = this; if (!this.rangeNotifierAdded) { this.rangeNotifierAdded = true; this.getBeaconManager().addRangeNotifier(new org.altbeacon.beacon.RangeNotifier({ didRangeBeaconsInRegion: function (beacons, region) { if (me.delegate) { var beaconsArray = beacons.toArray(); var beaconsList = []; for (var c = 0; c < beaconsArray.length; c++) { var beacon = beaconsArray[c]; beaconsList.push(me.getBeaconFromNativeBeacon(beacon)); } me.delegate.didRangeBeaconsInRegion(me.getBeaconRegionFromRegion(region), beaconsList); } } })); } if (!this.monitorNotifierAdded) { this.monitorNotifierAdded = true; this.getBeaconManager().addMonitorNotifier(new org.altbeacon.beacon.MonitorNotifier({ didEnterRegion: function (region) { console.log("didEnterRegion", me.delegate, region); if (me.delegate && me.delegate.didEnterRegion) { me.delegate.didEnterRegion(me.getBeaconRegionFromRegion(region)); } }, didExitRegion: function (region) { console.log("didExitRegion"); if (me.delegate && me.delegate.didExitRegion) { me.delegate.didExitRegion(me.getBeaconRegionFromRegion(region)); } }, didDetermineStateForRegion: function (state, region) { console.log("didDetermineStateForRegion"); console.log(state); console.log(region); } })); } if (me.delegate && me.delegate.onBeaconManagerReady) { me.delegate.onBeaconManagerReady(); } if (this.pendingBeaconRegion != null) { this.startRanging(this.pendingBeaconRegion); this.pendingBeaconRegion = null; } if (this.pendingBeaconMonitor != null) { this.startMonitoring(this.pendingBeaconMonitor); this.pendingBeaconMonitor = null; } }; LocationService.prototype.getRegionFromBeaconRegion = function (beaconRegion) { var minor = null; if (beaconRegion.minor) { minor = org.altbeacon.beacon.Identifier.fromInt(beaconRegion.minor); } var major = null; if (beaconRegion.major) { major = org.altbeacon.beacon.Identifier.fromInt(beaconRegion.major); } return new org.altbeacon.beacon.Region(beaconRegion.identifier, org.altbeacon.beacon.Identifier.fromUuid(java.util.UUID.fromString(beaconRegion.proximityUUID)), major, minor); }; LocationService.prototype.getBeaconRegionFromRegion = function (region) { var major = null; if (region.getId2()) { major = Number(region.getId2().toString()); } var minor = null; if (region.getId3()) { minor = Number(region.getId3().toString()); } var breagion = new nativescript_ibeacon_common_1.BeaconRegion(region.getUniqueId(), region.getId1().toString(), major, minor); return breagion; }; LocationService.prototype.getBeaconFromNativeBeacon = function (nativeBeacon) { var beacon = new nativescript_ibeacon_common_1.Beacon(nativeBeacon.getId1().toString(), nativeBeacon.getId2().toInt(), nativeBeacon.getId3().toInt()); beacon.distance_proximity = nativeBeacon.getDistance(); beacon.rssi = nativeBeacon.getRssi(); beacon.txPower_accuracy = nativeBeacon.getTxPower(); return beacon; }; LocationService.prototype.getApplicationContext = function () { return this.context; }; LocationService.prototype.unbindService = function (serviceConnection) { this.context.unbindService(serviceConnection); }; LocationService.prototype.bindService = function (intent, serviceConnection, i) { return this.context.bindService(intent, serviceConnection, i); }; return LocationService; }(java.lang.Object)); LocationService = __decorate([ Interfaces([org.altbeacon.beacon.BeaconConsumer]), __metadata("design:paramtypes", [android.content.Context]) ], LocationService); exports.LocationService = LocationService; var NativescriptIbeacon = (function (_super) { __extends(NativescriptIbeacon, _super); function NativescriptIbeacon(beaconCallback) { var _this = _super.call(this, beaconCallback) || this; _this.locationService = null; _this.locationService = new LocationService(utils.ad.getApplicationContext()); _this.locationService.delegate = beaconCallback; return _this; } NativescriptIbeacon.prototype.requestAuthorization = function () { return permissions.requestPermission(this.getPermission(), this.options.androidAuthorisationDescription); }; NativescriptIbeacon.prototype.isAuthorised = function () { return permissions.hasPermission(this.getPermission()); }; NativescriptIbeacon.prototype.getPermission = function () { var permission = null; if (this.options.androidAuthorisationType == nativescript_ibeacon_common_1.BeaconLocationOptionsAndroidAuthType.Coarse) { permission = android.Manifest.permission.ACCESS_COARSE_LOCATION; } else { permission = android.Manifest.permission.ACCESS_FINE_LOCATION; } return permission; }; 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.android.js.map