Skip to content

Instantly share code, notes, and snippets.

@MickaelCruzDB
Created July 13, 2015 07:59
Show Gist options
  • Save MickaelCruzDB/87ba601c3d96b754eb6f to your computer and use it in GitHub Desktop.
Save MickaelCruzDB/87ba601c3d96b754eb6f to your computer and use it in GitHub Desktop.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
// core location manager instance
let locationManager = CLLocationManager()
// define the core location region, defines whic beacons our regions should care about
// uses estimote default UUIDString
// identifier is just arbitrary string
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "B9407F30-F5F8-466E-AFF9-25556B57FE6D"), identifier: "Estimotes")
// define colors based on Estimote Minor values
let colors = [
23873: UIColor(red: 158/255, green: 222/255, blue: 218/255, alpha: 1),// green
51097: UIColor(red: 16/255, green: 194/255, blue: 230/255, alpha: 1),//sky blue
55103: UIColor(red: 16/255, green: 105/255, blue: 230/255, alpha: 1)// dark blue
]
let table1 = [23873,51097,55103]
// holds the closest estimote
var currentBeacon: CLBeacon = CLBeacon()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// ViewController should be the delegate (where it should deliver messages) of the locationManager
locationManager.delegate = self;
// only request authorization if app hasn't already been authorized
if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
}
// start monitoring region for beacons
locationManager.startRangingBeaconsInRegion(region)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// want to be notified when relative beacon is found
func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
// strip unknown beacons to create a new array
// closest beacon comes in first therefore we can assume the first item in array is closest
let knownBeacons = beacons.filter{ $0.proximity != CLProximity.Unknown }
if (knownBeacons.count > 0) {
// closest beacon accuracy value
var closestAccuracy: CLLocationAccuracy = 0.5
// closest beacon, defaults to first beacon
var closestBeacon: CLBeacon = knownBeacons[0] as! CLBeacon
// find closest beacon
for beacon in knownBeacons {
if (beacon.accuracy > -1 && beacon.accuracy < closestAccuracy) {
closestAccuracy = beacon.accuracy
closestBeacon = beacon as! CLBeacon
}
}
if (closestBeacon.minor != currentBeacon.minor) {
println("change")
println(closestBeacon.accuracy)
currentBeacon = closestBeacon
}
self.view.backgroundColor = self.colors[closestBeacon.minor.integerValue]
}
}
func locationManager(manager: CLLocationManager!,
didEnterRegion region: CLRegion!) {
manager.startRangingBeaconsInRegion(region as! CLBeaconRegion)
manager.startUpdatingLocation()
println("You entered the region")
}
func locationManager(manager: CLLocationManager!,
didExitRegion region: CLRegion!) {
manager.stopRangingBeaconsInRegion(region as! CLBeaconRegion)
manager.stopUpdatingLocation()
println("You exited the region")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment