Last active
September 25, 2016 05:44
-
-
Save aurozhkov/26cdbc2d26a0e875e552b910bb98e901 to your computer and use it in GitHub Desktop.
iBeacons finding
This file contains 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
import CoreLocation | |
let beaconUUIDs = ["f7826da6-4fa2-4e98-8024-bc5b71e0893e"] | |
let locationManager = CLLocationManager() | |
locationManager.delegate = self | |
for uuid in beaconUUIDs { | |
let beaconRegion = CLBeaconRegion(proximityUUID: NSUUID(UUIDString:uuid)!, identifier: uuid) | |
manager.startMonitoringForRegion(beaconRegion) | |
} | |
public func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, | |
forRegion region: CLRegion) { | |
if let region = region as? CLBeaconRegion { | |
if state == .Inside { | |
manager.startRangingBeaconsInRegion(region) | |
} else if state == .Outside { | |
manager.stopRangingBeaconsInRegion(region) | |
//мы вышли из зоны всех beacons | |
} | |
} | |
} | |
public func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], | |
inRegion region: CLBeaconRegion) { | |
var sortedBeacons = beacons.filter({ $0.proximity != .Unknown }) | |
sortedBeacons.sortInPlace({ | |
let firstBeaconProximity = $0.proximity.rawValue | |
let secondBeaconProximity = $1.proximity.rawValue | |
return firstBeaconProximity < secondBeaconProximity | |
|| (firstBeaconProximity == secondBeaconProximity && $0.accuracy < $1.accuracy) | |
}) | |
//мы находимся ближе всего к sortedBeacons.first | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment