Class: Tgios::BeaconManager

Inherits:
BindingBase show all
Defined in:
lib/tgios/beacon_manager.rb

Constant Summary collapse

BeaconFoundKey =
'Tgios::BeaconManager::BeaconFound'
EnterRegionKey =
'Tgios::BeaconManager::EnterRegion'
ExitRegionKey =
'Tgios::BeaconManager::ExitRegion'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BindingBase

#hook, #prepareForRelease, #unhook

Constructor Details

#initialize(uuid, range_limit = -70,, background = false, tolerance = 5, range_method = :rssi) ⇒ BeaconManager

Returns a new instance of BeaconManager.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tgios/beacon_manager.rb', line 28

def initialize(uuid, range_limit=-70, background=false, tolerance=5, range_method=:rssi)
  @events = {}
  @previous_beacons = []
  @background = background
  @tolerance = (tolerance || 5)

  @uuid = NSUUID.alloc.initWithUUIDString(uuid)
  @range_method = range_method
  @range_limit = range_limit

  @region = CLBeaconRegion.alloc.initWithProximityUUID(@uuid, identifier: uuid.split('-').first)
  @region.notifyOnEntry = true
  @region.notifyOnExit = true
  @region.notifyEntryStateOnDisplay = true

  start_monitor

  UIApplicationWillEnterForegroundNotification.add_observer(self, 'on_enter_foreground:')
  UIApplicationDidEnterBackgroundNotification.add_observer(self, 'on_enter_background:')
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



14
15
16
# File 'lib/tgios/beacon_manager.rb', line 14

def background
  @background
end

#current_beaconObject

Returns the value of attribute current_beacon.



14
15
16
# File 'lib/tgios/beacon_manager.rb', line 14

def current_beacon
  @current_beacon
end

#range_limitObject

Returns the value of attribute range_limit.



14
15
16
# File 'lib/tgios/beacon_manager.rb', line 14

def range_limit
  @range_limit
end

#range_methodObject

Returns the value of attribute range_method.



14
15
16
# File 'lib/tgios/beacon_manager.rb', line 14

def range_method
  @range_method
end

#toleranceObject

Returns the value of attribute tolerance.



14
15
16
# File 'lib/tgios/beacon_manager.rb', line 14

def tolerance
  @tolerance
end

Class Method Details

.beacon_eqs(beacon1, beacon2) ⇒ Object



172
173
174
175
# File 'lib/tgios/beacon_manager.rb', line 172

def self.beacon_eqs(beacon1, beacon2)
  return beacon1 == beacon2 if beacon1.nil? || beacon2.nil?
  beacon1.minor == beacon2.minor && beacon1.major == beacon2.major && beacon1.proximityUUID == beacon2.proximityUUID
end

.defaultObject



24
25
26
# File 'lib/tgios/beacon_manager.rb', line 24

def self.default
  @default
end

.default=(val) ⇒ Object



20
21
22
# File 'lib/tgios/beacon_manager.rb', line 20

def self.default=(val)
  @default = val
end

.supportedObject



181
182
183
# File 'lib/tgios/beacon_manager.rb', line 181

def self.supported
  CLLocationManager.isRangingAvailable
end

Instance Method Details

#beacon_eqs(beacon1, beacon2) ⇒ Object



168
169
170
# File 'lib/tgios/beacon_manager.rb', line 168

def beacon_eqs(beacon1, beacon2)
  self.class.beacon_eqs(beacon1, beacon2)
end

#deallocObject



195
196
197
198
# File 'lib/tgios/beacon_manager.rb', line 195

def dealloc
  onPrepareForRelease
  super
end

#has_event(event) ⇒ Object



150
151
152
# File 'lib/tgios/beacon_manager.rb', line 150

def has_event(event)
  @events.has_key?(event)
end

#location_managerObject



105
106
107
108
109
110
111
112
113
# File 'lib/tgios/beacon_manager.rb', line 105

def location_manager
  @location_manager ||=
      begin
        manager = CLLocationManager.alloc.init
        manager.delegate = self
        request_authorization(manager)
        manager
      end
end

#locationManager(manager, didRangeBeacons: beacons, inRegion: region) ⇒ Object



54
55
56
57
58
59
# File 'lib/tgios/beacon_manager.rb', line 54

def locationManager(manager, didDetermineState: state, forRegion: region)
  NSLog "didDetermineState #{state}"
  if state == CLRegionStateInside
    manager.startRangingBeaconsInRegion(region)
  end
end

#new_fake_beacon(options) ⇒ Object



177
178
179
# File 'lib/tgios/beacon_manager.rb', line 177

def new_fake_beacon(options)
  FakeBeacon.new({uuid: @uuid}.merge(options))
end

#on(event_key, &block) ⇒ Object



49
50
51
52
# File 'lib/tgios/beacon_manager.rb', line 49

def on(event_key,&block)
  @events[event_key] = block.weak!
  self
end

#on_enter_background(noti) ⇒ Object



145
146
147
148
# File 'lib/tgios/beacon_manager.rb', line 145

def on_enter_background(noti)
  NSLog 'on_enter_background'
  stop_monitor unless @background
end

#on_enter_foreground(noti) ⇒ Object



140
141
142
143
# File 'lib/tgios/beacon_manager.rb', line 140

def on_enter_foreground(noti)
  NSLog 'on_enter_foreground'
  self.performSelector('start_monitor', withObject: nil, afterDelay:1)
end

#onPrepareForReleaseObject



185
186
187
188
189
190
191
192
193
# File 'lib/tgios/beacon_manager.rb', line 185

def onPrepareForRelease
  UIApplicationWillEnterForegroundNotification.remove_observer(self)
  UIApplicationDidEnterBackgroundNotification.remove_observer(self)
  stop_monitor
  @location_manager = nil
  @events = nil
  @current_beacon = nil
  @previous_beacons = nil
end

#push_beacon(beacon) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/tgios/beacon_manager.rb', line 154

def push_beacon(beacon)
  if beacon_eqs(beacon, @current_beacon)
    @current_beacon = beacon
  else
    if @previous_beacons.find { |b| !beacon_eqs(beacon, b) }.blank?
      @current_beacon = beacon
    else
      @current_beacon = nil if @previous_beacons.find{ |b| beacon_eqs(@current_beacon, b)}.blank?
    end
  end
  @previous_beacons << beacon
  @previous_beacons.delete_at(0) if @previous_beacons.length > @tolerance
end

#request_authorization(manager) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/tgios/beacon_manager.rb', line 115

def request_authorization(manager)
  if manager.respond_to?(:requestAlwaysAuthorization)
    status = CLLocationManager.authorizationStatus
    if status != KCLAuthorizationStatusAuthorizedAlways
      title = (status == KCLAuthorizationStatusDenied) ? "Location services are off" : "Background location is not enabled"
      message = "To use background location you must turn on 'Always' in the Location Services Settings"

      UIAlertView.alert(title, message: message)
    else
      manager.requestAlwaysAuthorization
    end
  end

end

#start_monitorObject



130
131
132
133
# File 'lib/tgios/beacon_manager.rb', line 130

def start_monitor
  location_manager.startMonitoringForRegion(@region)
  location_manager.requestStateForRegion(@region)
end

#stop_monitorObject



135
136
137
138
# File 'lib/tgios/beacon_manager.rb', line 135

def stop_monitor
  location_manager.stopRangingBeaconsInRegion(@region)
  location_manager.stopMonitoringForRegion(@region)
end