update: 2016/07/30
reference:
1. Beacon Tech Overview - Estimote Developer
Part 3: Ranging beacons
A. 開啟 ViewController.swift 修改如下:
import UIKit
//@update ################################
// 1. Add the ESTBeaconManagerDelegate protocol
//class ViewController: UIViewController {
class ViewController: UIViewController, ESTBeaconManagerDelegate {
//@add ################################
// Add the property holding the data.
// TODO: replace "<major>:<minor>" strings to match your own beacons
let placesByBeacons = [
"0:5": [
"Heavenly Sandwiches": 50, // read as: it's 50 meters from
// "Heavenly Sandwiches" to the beacon with
// major 0 and minor 5
"Green & Green Salads": 150,
"Mini Panini": 325
],
"0:7": [
"Heavenly Sandwiches": 250,
"Green & Green Salads": 100,
"Mini Panini": 20
]
]
//@add ################################
// 2. Add the beacon manager and the beacon region
let beaconManager = ESTBeaconManager()
let beaconRegion = CLBeaconRegion(
proximityUUID: NSUUID(UUIDString: "FDA50693-A4E2-4FB1-AFCF-C6EB07647825")!,
identifier: "ranged region")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//@add ################################
// 3. Set the beacon manager's delegate
self.beaconManager.delegate = self
//@add ################################
// 4. We need to request this authorization for every beacon manager
// self.beaconManager.requestAlwaysAuthorization()
self.beaconManager.requestWhenInUseAuthorization()
}
//@add ################################
// start ranging as the view controller appears on screen
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.beaconManager.startRangingBeaconsInRegion(self.beaconRegion)
}
//@add ################################
// stop ranging as the view controller disappears on screen
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
self.beaconManager.stopRangingBeaconsInRegion(self.beaconRegion)
}
//@add ################################
// places Near Beacon
func placesNearBeacon(beacon: CLBeacon) -> [String]? {
let beaconKey = "\(beacon.major):\(beacon.minor)"
if let places = self.placesByBeacons[beaconKey] {
//let sortedPlaces = Array(places).sorted { $0.1 < $1.1 }.map { $0.0 }
let sortedPlaces = Array(places).sort { $0.1 < $1.1 }.map { $0.0 }
return sortedPlaces
}
return nil
}
//@add ################################
// didRangeBeacons delegate
func beaconManager(manager: AnyObject, didRangeBeacons beacons: [CLBeacon],
inRegion region: CLBeaconRegion) {
if let nearestBeacon = beacons.first, places = placesNearBeacon(nearestBeacon) {
// TODO: update the UI here
print("************************")
print("UUID:" + region.proximityUUID.UUIDString + "; identifier: " + region.identifier)
print("major: " + nearestBeacon.major.stringValue + "; minor: " + nearestBeacon.minor.stringValue)
print(places) // TODO: remove after implementing the UI
print("")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
-----------------------------------------------------------------------------------------------
B. 結果:
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。