Class: BeaconManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(proximityUUID, beaconID) ⇒ BeaconManager

initialize BeaconManager with a proximity ID of beacons being targeted and application beacon ID

Examples:

beaconManager = BeaconManager.new('E2C56DB5-DFFB-48D2-B060-D0F5A71096E0',
                                  'com.company.beacons')


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

#beaconIDObject (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

#proximityUUIDObject (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

Examples:

createRegion(major: 0, minor: 1)


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(options = {})

  minor = options.fetch(:minor, nil)
  major = options.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

Examples:

onChangeRegion { |oldRegion, newRegion| my_code }


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

Examples:

onEnterRegion { my_code }
onEnterRegion(:major = 0) { |region| my_code }
onEnterRegion(:major = 0, :minor = 1) { my_code }


121
122
123
124
125
126
127
# File 'lib/motion-beacon/beaconmanager.rb', line 121

def onEnterRegion(options = {}, &block)
  major = options.fetch(:major, nil)
  minor = options.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

Examples:

onLeaveRegion { my_code }
onLeaveRegion(:major = 0) { |region| my_code }
onLeaveRegion(:major = 0, :minor = 1) { my_code }


134
135
136
137
138
139
140
# File 'lib/motion-beacon/beaconmanager.rb', line 134

def onLeaveRegion(options = {}, &block)
  major = options.fetch(:major, nil)
  minor = options.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

Examples:

onLostRegions { my_code }


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

Examples:

startRangingBeacons(:major = 0, :minor = 1)
startRangingBeacons(:major = 0)
startRangingBeacons


89
90
91
92
93
94
95
96
97
98
# File 'lib/motion-beacon/beaconmanager.rb', line 89

def startRangingBeacons(options = {})
  major = options.fetch(:major, nil)
  minor = options.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

Examples:

stopRangingBeacons(:major = 0, :minor = 1)
stopRangingBeacons(:major = 0)
stopRangingBeacons


105
106
107
108
109
110
111
112
113
114
# File 'lib/motion-beacon/beaconmanager.rb', line 105

def stopRangingBeacons(options = {})
  major = options.fetch(:major, nil)
  minor = options.fetch(:minor, nil)

  regions = findRegions(major, minor)

  regions.each { |region|
    @locationManager.stopRangingBeacons(region)
  }
end