-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeaconViewController.swift
131 lines (98 loc) · 3.7 KB
/
BeaconViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// BeaconViewController.swift
// iBeaconReceiver
//
// Created by Andrea Antonioni on 14/10/17.
// Copyright © 2017 Andrea Antonioni. All rights reserved.
//
import UIKit
import CoreLocation
import Alamofire
class BeaconViewController: UIViewController {
let locationManager = CLLocationManager()
var beaconRegion: CLBeaconRegion!
private enum BeaconStatus {
case finded(timer: Timer)
case unknown
mutating func `switch`() {
switch self {
case .finded(let timer):
timer.invalidate()
self = .unknown
case .unknown:
self = .finded(timer: createTimer())
}
}
private func createTimer() -> Timer {
return Timer.scheduledTimer(withTimeInterval: 4, repeats: true, block: { _ in
print("🔔")
NetworkManager.shared.accessRequest()
})
}
}
private var status: BeaconStatus = .unknown
override func viewDidLoad() {
super.viewDidLoad()
self.title = "iBeacon"
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
monitorBeacons()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func monitorBeacons() {
// Match all beacons with the specified UUID
let proximityUUID = UUID(uuidString:
"9C07E0AB-EC3C-4968-9296-59A0579D0678")
let beaconID = "io.andreaantonioni.iBeaconSample"
// Create the region and begin monitoring it.
beaconRegion = CLBeaconRegion(proximityUUID: proximityUUID!,
identifier: beaconID)
self.locationManager.startMonitoring(for: beaconRegion)
locationManager.startUpdatingLocation()
}
}
// MARK: - CLLocationManagerDelegate
extension BeaconViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager,
didStartMonitoringFor region: CLRegion) {
locationManager.requestState(for: region)
}
func locationManager(_ manager: CLLocationManager,
didDetermineState state: CLRegionState,
for region: CLRegion) {
if state == .inside {
locationManager.startRangingBeacons(in: beaconRegion)
}
}
func locationManager(_ manager: CLLocationManager,
didRangeBeacons beacons: [CLBeacon],
in region: CLBeaconRegion) {
guard let nearestBeacon = beacons.first else { return }
switch nearestBeacon.proximity {
case .immediate:
view.backgroundColor = .green
if case .unknown = status {
status.switch()
}
case .near:
view.backgroundColor = .yellow
default:
view.backgroundColor = .red
if case .finded = status {
status.switch()
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
print(error.localizedDescription)
}
func locationManager(_ manager: CLLocationManager, rangingBeaconsDidFailFor region: CLBeaconRegion, withError error: Error) {
print(error.localizedDescription)
}
}