Last active
November 13, 2017 15:47
-
-
Save hansemannn/778621c11d3d1ef45f78e70f276aa354 to your computer and use it in GitHub Desktop.
Discover iBeacons with Hyperloop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CLBeaconRegion = require("CoreLocation/CLBeaconRegion"), | |
NSUUID = require("Foundation/NSUUID"), | |
CLLocationManager = require("CoreLocation/CLLocationManager"), | |
CLProximityFar = require("CoreLocation").CLProximityFar, | |
CLProximityNear = require("CoreLocation").CLProximityNear, | |
CLProximityImmediate = require("CoreLocation").CLProximityImmediate, | |
CLProximityUnknown = require("CoreLocation").CLProximityUnknown, | |
UILocalNotification = require("UIKit/UILocalNotification"), | |
UIApplication = require("UIKit/UIApplication"), | |
locationManager; | |
(function constructor(args) { | |
var LocationManagerDelegate_ = require("subclasses/locationmanagerdelegate"); | |
var delegate = new LocationManagerDelegate_(); | |
delegate.didEnterRegion = function(manager, region) { | |
locationManager.startRangingBeaconsInRegion(region); | |
locationManager.startUpdatingLocation(); | |
presentNotification("You just entered the current region!"); | |
}; | |
delegate.didExitRegion = function(manager, region) { | |
locationManager.stopRangingBeaconsInRegion(region); | |
locationManager.stopUpdatingLocation(); | |
presentNotification("You just exited the current region!"); | |
}; | |
delegate.didRangeBeacons = function(manager, beacons, region) { | |
var message = ""; | |
if(beacons.count > 0) { | |
var nearestBeacon = beacons.firstObject; | |
switch(nearestBeacon.proximity) { | |
case CLProximityFar: | |
message = "You are far away from the beacon"; | |
break; | |
case CLProximityNear: | |
message = "You are near the beacon"; | |
break; | |
case CLProximityImmediate: | |
message = "You are in the immediate proximity of the beacon"; | |
break; | |
case CLProximityUnknown: | |
return; | |
} | |
} else { | |
message = "No beacons are nearby"; | |
} | |
presentNotification(message); | |
}; | |
locationManager = new CLLocationManager(); | |
locationManager.setDelegate(delegate); | |
locationManager.requestAlwaysAuthorization(); | |
})(arguments[0] || {}); | |
function startDiscovery() { | |
var region = CLBeaconRegion.alloc().initWithProximityUUIDMajorMinorIdentifier(NSUUID.alloc().initWithUUIDString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), 1, 1, "com.appcelerator.beacons"); | |
locationManager.startMonitoringForRegion(region); | |
} | |
function presentNotification(title) { | |
var notification = new UILocalNotification(); | |
notification.alertBody = title; | |
notification.soundName = "Default"; | |
UIApplication.sharedApplication().presentLocalNotificationNow(notification); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var LocationManagerDelegate = Hyperloop.defineClass('LocationManagerDelegate', 'NSObject', ['CLLocationManagerDelegate']); | |
LocationManagerDelegate.addMethod({ | |
selector: 'locationManager:didEnterRegion:', | |
arguments: ['CLLocationManager', 'CLRegion'], | |
callback: function(manager, region) { | |
if (this.didEnterRegion) { | |
this.didEnterRegion(manager, region); | |
} | |
} | |
}); | |
LocationManagerDelegate.addMethod({ | |
selector: 'locationManager:didExitRegion:', | |
arguments: ['CLLocationManager', 'CLRegion'], | |
callback: function(manager, region) { | |
if (this.didExitRegion) { | |
this.didExitRegion(manager, region); | |
} | |
} | |
}); | |
LocationManagerDelegate.addMethod({ | |
selector: 'locationManager:didRangeBeacons:inRegion:', | |
arguments: ['CLLocationManager', 'NSArray', 'CLBeaconRegion'], | |
callback: function(manager, beacons, region) { | |
if (this.didRangeBeacons) { | |
this.didRangeBeacons(manager, beacons, region); | |
} | |
} | |
}); | |
module.exports = LocationManagerDelegate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment