Class: BeaconManager
- Inherits:
-
Object
- Object
- BeaconManager
- Defined in:
- lib/motion-beacon/beaconmanager.rb
Overview
wrapper over CLLocationManager and CLBeaconRegion
Defined Under Namespace
Classes: CallbackKey
Constant Summary collapse
- REGION_TIMEOUT =
timeout for deactivating regions in [ms]
2000
- UPDATE_TIMEOUT =
tick update timeout in [ms]
500
Instance Attribute Summary collapse
-
#beaconID ⇒ Object
readonly
unique identifier to all your beacons for using in your application @example com.company.beaconsApp.
-
#proximityUUID ⇒ Object
readonly
the unique ID of the beacons being targeted.
Instance Method Summary collapse
-
#advertiseSelf(major, minor) ⇒ Object
advertise this device as a beacon.
-
#createRegion(options = {}) ⇒ Object
create a beacon region that targets beacons with specified major value and minor value.
-
#initialize(proximityUUID, beaconID) ⇒ BeaconManager
constructor
initialize BeaconManager with a proximity ID of beacons being targeted and application beacon ID.
-
#onChangeRegion(&block) ⇒ Object
add event handler for detection of changing the region.
-
#onEnterRegion(options = {}, &block) ⇒ Object
add event handler for detection of entering the beacon region.
-
#onLeaveRegion(options = {}, &block) ⇒ Object
add event handler for detection of leaving the beacon region.
-
#onLostRegions(&block) ⇒ Object
add event handler for detection of loosing any regions this is called when there are no close regions in range.
-
#startRangingBeacons(options = {}) ⇒ Object
start the delivery of notifications for beacons in the specified region.
-
#stopRangingBeacons(options = {}) ⇒ Object
stops the delivery of notifications for the specified beacon regions.
Constructor Details
#initialize(proximityUUID, beaconID) ⇒ BeaconManager
initialize BeaconManager with a proximity ID of beacons being targeted and application beacon ID
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 |
# File 'lib/motion-beacon/beaconmanager.rb', line 24 def initialize(proximityUUID, beaconID) @locationManager = CLLocationManager.alloc.init @locationManager.delegate = self @proximityUUID = if proximityUUID.is_a? NSUUID then id elsif proximityUUID.is_a? String then NSUUID.alloc.initWithUUIDString(proximityUUID) else raise 'Invalid Beacon proximity UUID!' end @beaconID = if beaconID.is_a? String then beaconID else raise 'Invalid Beacon app ID!' end @regions = [] @activeRegions = [] @activeRegion = nil @callbacks = Hash.new { |hash, key| hash[key] = [] } every UPDATE_TIMEOUT.millisec do self.onTimer end end |
Instance Attribute Details
#beaconID ⇒ Object (readonly)
unique identifier to all your beacons for using in your application
@example
com.company.beaconsApp
12 13 14 |
# File 'lib/motion-beacon/beaconmanager.rb', line 12 def beaconID @beaconID end |
#proximityUUID ⇒ Object (readonly)
the unique ID of the beacons being targeted
7 8 9 |
# File 'lib/motion-beacon/beaconmanager.rb', line 7 def proximityUUID @proximityUUID end |
Instance Method Details
#advertiseSelf(major, minor) ⇒ Object
advertise this device as a beacon
81 82 |
# File 'lib/motion-beacon/beaconmanager.rb', line 81 def advertiseSelf(major, minor) end |
#createRegion(options = {}) ⇒ Object
create a beacon region that targets beacons with specified major value and minor value
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/motion-beacon/beaconmanager.rb', line 55 def createRegion( = {}) minor = .fetch(:minor, nil) major = .fetch(:major, nil) id = @beaconID; if not major.nil? id = id + ':' + if minor.nil? then major.to_s else minor.to_s end end region = if major.nil? then CLBeaconRegion.alloc.initWithProximityUUID(@proximityUUID, identifier: id) elsif minor.nil? then CLBeaconRegion.alloc.initWithProximityUUID(@proximityUUID, major: major, identifier: id) else CLBeaconRegion.alloc.initWithProximityUUID(@proximityUUID, major: major, minor: minor, identifier: id) end region.notifyOnEntry = true region.notifyOnExit = true region.notifyEntryStateOnDisplay = true @regions.push(region) end |
#onChangeRegion(&block) ⇒ Object
add event handler for detection of changing the region
145 146 147 148 |
# File 'lib/motion-beacon/beaconmanager.rb', line 145 def onChangeRegion(&block) key = CallbackKey.new(0, 0, :change) @callbacks[key].push(block.weak!) end |
#onEnterRegion(options = {}, &block) ⇒ Object
add event handler for detection of entering the beacon region
121 122 123 124 125 126 127 |
# File 'lib/motion-beacon/beaconmanager.rb', line 121 def onEnterRegion( = {}, &block) major = .fetch(:major, nil) minor = .fetch(:minor, nil) key = CallbackKey.new(major, minor, :enter) @callbacks[key].push(block.weak!) end |
#onLeaveRegion(options = {}, &block) ⇒ Object
add event handler for detection of leaving the beacon region
134 135 136 137 138 139 140 |
# File 'lib/motion-beacon/beaconmanager.rb', line 134 def onLeaveRegion( = {}, &block) major = .fetch(:major, nil) minor = .fetch(:minor, nil) key = CallbackKey.new(major, minor, :leave) @callbacks[key].push(block.weak!) end |
#onLostRegions(&block) ⇒ Object
add event handler for detection of loosing any regions this is called when there are no close regions in range
154 155 156 157 |
# File 'lib/motion-beacon/beaconmanager.rb', line 154 def onLostRegions(&block) key = CallbackKey.new(0, 0, :lost) @callbacks[key].push(block.weak!) end |
#startRangingBeacons(options = {}) ⇒ Object
start the delivery of notifications for beacons in the specified region
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/motion-beacon/beaconmanager.rb', line 89 def startRangingBeacons( = {}) major = .fetch(:major, nil) minor = .fetch(:minor, nil) regions = findRegions(major, minor) regions.each { |region| @locationManager.startRangingBeaconsInRegion(region) } end |
#stopRangingBeacons(options = {}) ⇒ Object
stops the delivery of notifications for the specified beacon regions
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/motion-beacon/beaconmanager.rb', line 105 def stopRangingBeacons( = {}) major = .fetch(:major, nil) minor = .fetch(:minor, nil) regions = findRegions(major, minor) regions.each { |region| @locationManager.stopRangingBeacons(region) } end |