motion-beacon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 09750b5492cc2c3e8fb72d58ae99a90c246265ea
4
+ data.tar.gz: 3a9920655fdbece2c6d2be36d405e56f6ccebab8
5
+ SHA512:
6
+ metadata.gz: e4c9318ccd291aa94a7272c2af1b4ec9c060f17ab318dc6f402a28763706f9143d4afdcd1bf206eb39f77660bc9e10fe2f91f738b0a3a4c9bf618533acbaf5ab
7
+ data.tar.gz: 1974bd9fc9192bcb423bdc54b1c1cfa07c584e7e04c94c3f83842cc6c58c3f6d4edef77e9cd9056f168e1cca59129607ebe86fea5178dc3ae67eb902acff4fe7
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ .DS_Store
21
+ build
data/.repl_history ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in motion-beacon.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,12 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ sugarcube-timer (1.3.11)
5
+ rake (10.1.0)
6
+
7
+ PLATFORMS
8
+ ruby
9
+
10
+ DEPENDENCIES
11
+ sugarcube-timer
12
+ rake
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Vaclav Samec
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # MotionBeacon
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'motion-beaon'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install motion-beacon
17
+
18
+ ## Usage
19
+
20
+ Inlcude MotionBeacon module in your app_delegate.rb:
21
+
22
+ include MotionBeacon
23
+
24
+ Once MotionBeacon module is included, you can start using it:
25
+
26
+
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift("/Library/RubyMotion/lib")
3
+ require 'motion/project/template/ios'
4
+ require 'bundler'
5
+ require 'sugarcube-timer'
6
+
7
+ $:.unshift("./lib/")
8
+ require './lib/motion-beacon'
9
+
10
+ Motion::Project::App.setup do |app|
11
+ app.frameworks += ['CoreLocation']
12
+ end
@@ -0,0 +1,37 @@
1
+ include SugarCube::Timer
2
+
3
+ class AppDelegate
4
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
5
+
6
+ after 2.sec do
7
+
8
+ @beaconManager = BeaconManager.new('E2C56DB5-DFFB-48D2-B060-D0F5A71096E0', 'com.codium.iBeaconsDemo')
9
+
10
+ @beaconManager.createRegion(major: 0, minor: 1)
11
+ @beaconManager.startRangingBeacons(major: 0, minor: 1)
12
+ @beaconManager.createRegion(major: 0, minor: 2)
13
+ @beaconManager.startRangingBeacons(major: 0, minor: 2)
14
+
15
+ @beaconManager.onEnterRegion { |region|
16
+
17
+ NSLog("Region entered: #{region.to_s}")
18
+ }
19
+
20
+ @beaconManager.onLeaveRegion { |region|
21
+ NSLog("Region leaved: " + region.to_s)
22
+ }
23
+
24
+ @beaconManager.onChangeRegion { |oldRegion, newRegion|
25
+ NSLog("Region change, old: [#{oldRegion.to_s}] new: [#{newRegion}]")
26
+ }
27
+
28
+ @beaconManager.onLostRegions {
29
+ NSLog("Regions lost")
30
+ }
31
+
32
+ end
33
+
34
+ true
35
+ end
36
+ end
37
+
@@ -0,0 +1,315 @@
1
+ include SugarCube::Timer
2
+
3
+ # wrapper over CLLocationManager and CLBeaconRegion
4
+ class BeaconManager
5
+
6
+ # the unique ID of the beacons being targeted
7
+ attr_reader :proximityUUID
8
+
9
+ # unique identifier to all your beacons for using in your application
10
+ # @example
11
+ # com.company.beaconsApp
12
+ attr_reader :beaconID
13
+
14
+ # timeout for deactivating regions in [ms]
15
+ REGION_TIMEOUT = 2000
16
+
17
+ # tick update timeout in [ms]
18
+ UPDATE_TIMEOUT = 500
19
+
20
+ # initialize BeaconManager with a proximity ID of beacons being targeted and application beacon ID
21
+ # @example
22
+ # beaconManager = BeaconManager.new('E2C56DB5-DFFB-48D2-B060-D0F5A71096E0',
23
+ # 'com.company.beacons')
24
+ def initialize(proximityUUID, beaconID)
25
+ @locationManager = CLLocationManager.alloc.init
26
+ @locationManager.delegate = self
27
+
28
+ @proximityUUID = if proximityUUID.is_a? NSUUID then
29
+ id
30
+ elsif proximityUUID.is_a? String then
31
+ NSUUID.alloc.initWithUUIDString(proximityUUID)
32
+ else
33
+ raise 'Invalid Beacon proximity UUID!'
34
+ end
35
+
36
+ @beaconID = if beaconID.is_a? String then
37
+ beaconID
38
+ else
39
+ raise 'Invalid Beacon app ID!'
40
+ end
41
+
42
+ @regions = []
43
+ @activeRegions = []
44
+ @activeRegion = nil
45
+ @callbacks = Hash.new { |hash, key| hash[key] = [] }
46
+
47
+ every UPDATE_TIMEOUT.millisec do
48
+ self.onTimer
49
+ end
50
+ end
51
+
52
+ # create a beacon region that targets beacons with specified major value and minor value
53
+ # @example
54
+ # createRegion(major: 0, minor: 1)
55
+ def createRegion(options = {})
56
+
57
+ minor = options.fetch(:minor, nil)
58
+ major = options.fetch(:major, nil)
59
+
60
+ id = @beaconID;
61
+ if not major.nil?
62
+ id = id + ':' + if minor.nil? then major.to_s else minor.to_s end
63
+ end
64
+
65
+ region = if major.nil? then
66
+ CLBeaconRegion.alloc.initWithProximityUUID(@proximityUUID, identifier: id)
67
+ elsif minor.nil? then
68
+ CLBeaconRegion.alloc.initWithProximityUUID(@proximityUUID, major: major, identifier: id)
69
+ else
70
+ CLBeaconRegion.alloc.initWithProximityUUID(@proximityUUID, major: major, minor: minor, identifier: id)
71
+ end
72
+
73
+ region.notifyOnEntry = true
74
+ region.notifyOnExit = true
75
+ region.notifyEntryStateOnDisplay = true
76
+
77
+ @regions.push(region)
78
+ end
79
+
80
+ # advertise this device as a beacon
81
+ def advertiseSelf(major, minor)
82
+ end
83
+
84
+ # start the delivery of notifications for beacons in the specified region
85
+ # @example
86
+ # startRangingBeacons(:major = 0, :minor = 1)
87
+ # startRangingBeacons(:major = 0)
88
+ # startRangingBeacons
89
+ def startRangingBeacons(options = {})
90
+ major = options.fetch(:major, nil)
91
+ minor = options.fetch(:minor, nil)
92
+
93
+ regions = findRegions(major, minor)
94
+
95
+ regions.each { |region|
96
+ @locationManager.startRangingBeaconsInRegion(region)
97
+ }
98
+ end
99
+
100
+ # stops the delivery of notifications for the specified beacon regions
101
+ # @example
102
+ # stopRangingBeacons(:major = 0, :minor = 1)
103
+ # stopRangingBeacons(:major = 0)
104
+ # stopRangingBeacons
105
+ def stopRangingBeacons(options = {})
106
+ major = options.fetch(:major, nil)
107
+ minor = options.fetch(:minor, nil)
108
+
109
+ regions = findRegions(major, minor)
110
+
111
+ regions.each { |region|
112
+ @locationManager.stopRangingBeacons(region)
113
+ }
114
+ end
115
+
116
+ # add event handler for detection of entering the beacon region
117
+ # @example
118
+ # onEnterRegion { my_code }
119
+ # onEnterRegion(:major = 0) { |region| my_code }
120
+ # onEnterRegion(:major = 0, :minor = 1) { my_code }
121
+ def onEnterRegion(options = {}, &block)
122
+ major = options.fetch(:major, nil)
123
+ minor = options.fetch(:minor, nil)
124
+
125
+ key = CallbackKey.new(major, minor, :enter)
126
+ @callbacks[key].push(block.weak!)
127
+ end
128
+
129
+ # add event handler for detection of leaving the beacon region
130
+ # @example
131
+ # onLeaveRegion { my_code }
132
+ # onLeaveRegion(:major = 0) { |region| my_code }
133
+ # onLeaveRegion(:major = 0, :minor = 1) { my_code }
134
+ def onLeaveRegion(options = {}, &block)
135
+ major = options.fetch(:major, nil)
136
+ minor = options.fetch(:minor, nil)
137
+
138
+ key = CallbackKey.new(major, minor, :leave)
139
+ @callbacks[key].push(block.weak!)
140
+ end
141
+
142
+ # add event handler for detection of changing the region
143
+ # @example
144
+ # onChangeRegion { |oldRegion, newRegion| my_code }
145
+ def onChangeRegion(&block)
146
+ key = CallbackKey.new(0, 0, :change)
147
+ @callbacks[key].push(block.weak!)
148
+ end
149
+
150
+ # add event handler for detection of loosing any regions
151
+ # this is called when there are no close regions in range
152
+ # @example
153
+ # onLostRegions { my_code }
154
+ def onLostRegions(&block)
155
+ key = CallbackKey.new(0, 0, :lost)
156
+ @callbacks[key].push(block.weak!)
157
+ end
158
+
159
+ private
160
+ CallbackKey = Struct.new(:major, :minor, :type)
161
+
162
+
163
+ protected
164
+ def onTimer
165
+ @activeRegions.each { |region|
166
+ region.timeout += UPDATE_TIMEOUT
167
+ }
168
+
169
+ # if @activeRegions.count > 0 then
170
+ # NSLog("onTimer.timeout: " + @activeRegions[0].timeout.to_s)
171
+ # end
172
+
173
+ @activeRegions.delete_if { |region| region.timeout > REGION_TIMEOUT }
174
+
175
+ self.updateRegions
176
+ end
177
+
178
+ protected
179
+ def updateRegions
180
+ if not @activeRegions.empty? then
181
+
182
+ # if @activeRegions.count > 1 then
183
+ # NSLog("region0: #{@activeRegions[0].distance.to_s} region1: #{@activeRegions[1].distance.to_s}")
184
+ # end
185
+
186
+ sorted = @activeRegions.sort { |a, b| a.distance <=> b.distance }
187
+
188
+ if sorted[0] != @activeRegion then
189
+ if not @activeRegion.nil? then
190
+ regionLeaved(@activeRegion)
191
+ end
192
+ regionChanged(@activeRegion, sorted[0])
193
+ @activeRegion = sorted[0]
194
+ regionEntered(@activeRegion)
195
+ end
196
+
197
+ elsif not @activeRegion.nil? then
198
+ self.regionLeaved(@activeRegion)
199
+ self.regionChanged(@activeRegion, nil)
200
+ @activeRegion = nil
201
+ self.regionsLost
202
+ end
203
+ end
204
+
205
+ def regionEntered(region)
206
+ # NSLog("regionEntered: callbacks: #{@callbacks.count.to_s}")
207
+ @callbacks.each { |key, value|
208
+
209
+ if (key.type == :enter) and
210
+ ((key.major.nil?) or
211
+ (key.minor.nil? and key.major == region.major) or
212
+ (region.major == key.major and region.minor == key.minor) or
213
+ (region.hasBeacon?(key.major, key.minor))) then
214
+
215
+ if not value[0].nil? then
216
+ value[0].call(region)
217
+ end
218
+ end
219
+ }
220
+
221
+ end
222
+
223
+ def regionLeaved(region)
224
+ # NSLog("onRegionLeav: callbacks: #{@callbacks.count.to_s}")
225
+ @callbacks.each { |key, value|
226
+
227
+ if (key.type == :leave) and
228
+ ((key.major.nil?) or
229
+ (key.minor.nil? and key.major == region.major) or
230
+ (region.major == key.major and region.minor == key.minor) or
231
+ (region.hasBeacon?(key.major, key.minor))) then
232
+
233
+ if not value[0].nil? then
234
+ value[0].call(region)
235
+ end
236
+ end
237
+ }
238
+ end
239
+
240
+ def regionChanged(regionOld, regionNew)
241
+ @callbacks.each { |key, value|
242
+
243
+ if key.type == :change then
244
+
245
+ if not value[0].nil? then
246
+ value[0].call(regionOld, regionNew)
247
+ end
248
+ end
249
+ }
250
+ end
251
+
252
+ def regionsLost
253
+ @callbacks.each { |key, value|
254
+
255
+ if key.type == :lost
256
+ if not value[0].nil?
257
+ value[0].call
258
+ end
259
+ end
260
+ }
261
+ end
262
+
263
+ private
264
+
265
+ def findRegions(major, minor)
266
+ regionsFound = []
267
+
268
+ if major.nil?
269
+ return @regions
270
+ end
271
+
272
+ @regions.each { |region|
273
+ if region.major == major then
274
+ if region.minor == minor or minor.nil? then
275
+ regionsFound.push(region)
276
+ end
277
+ end
278
+ }
279
+
280
+ regionsFound
281
+ end
282
+
283
+ def locationManager(locationManager, didRangeBeacons: beacons, inRegion: region)
284
+
285
+ # NSLog("didRangeBeacons: " + beacons.count.to_s + " valid: " + beaconsValid?(beacons).to_s)
286
+
287
+ if not beacons.empty? and beaconsValid?(beacons) then
288
+
289
+ # NSLog("didRangeBeacons: " + beacons.count.to_s + " valid: " + beaconsValid?(beacons).to_s)
290
+
291
+ regionIndex = @activeRegions.index(region)
292
+
293
+ if regionIndex.nil? then
294
+ region.beacons = beacons
295
+ region.timeout = 0
296
+ @activeRegions.push(region)
297
+ else
298
+ @activeRegions[regionIndex].beacons = beacons
299
+ @activeRegions[regionIndex].timeout = 0
300
+ end
301
+
302
+ self.updateRegions
303
+
304
+ end
305
+ end
306
+
307
+ def locationManager(locationManager, rangingBeaconsDidFailForRegion: region, withError: error)
308
+ NSLog("rangingBeaconsDidFailForRegion: " + region.to_s + " " + error.to_s)
309
+ end
310
+
311
+ def beaconsValid?(beacons)
312
+ beacons.any? { |beacon| beacon.accuracy > -1 and beacon.rssi != 0 }
313
+ end
314
+ end
315
+
@@ -0,0 +1,47 @@
1
+ # extended BeaconRegion class with list of beacons and helper methods
2
+ class CLBeaconRegion
3
+
4
+ attr_accessor :beacons
5
+ attr_accessor :timeout
6
+
7
+ def distance
8
+ if @beacons.empty? then
9
+ return 99999
10
+ else
11
+ beaconMin = @beacons.min { |a, b| a.accuracy <=> b.accuracy }
12
+ beaconMin.accuracy
13
+ end
14
+ end
15
+
16
+ def hasBeacon?(major, minor)
17
+ @beacons.each { |beacon|
18
+ if not major.nil? then
19
+ if minor.nil? then
20
+ if beacon.major == major then
21
+ return true
22
+ end
23
+ else
24
+ if beacon.major == major and beacon.minor == minor then
25
+ return true
26
+ end
27
+ end
28
+ end
29
+ }
30
+ false
31
+ end
32
+
33
+ def to_s
34
+ "region: [major]: " + major.to_s +
35
+ " [minor]: " + minor.to_s
36
+ end
37
+
38
+ def printBeacons
39
+ @beacons.each { |beacon|
40
+ NSLog("beacon: [major]: " + beacon.major.to_s +
41
+ " [minor]: " + beacon.minor.to_s +
42
+ " [proximity]: " + beacon.proximity.to_s +
43
+ " [accuracy]: " + beacon.accuracy.to_s +
44
+ " [rssi]: " + beacon.rssi.to_s)
45
+ }
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Motion
2
+ module Beacon
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ Motion::Project::App.setup do |app|
6
+ Dir.glob(File.join(File.dirname(__FILE__), 'motion-beacon/*.rb')).each do |file|
7
+ app.files.unshift(file)
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/motion-beacon/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["CODIUM"]
6
+ gem.email = ["vaclav.s@codium.co"]
7
+ gem.description = "detect iBeacons with simple events"
8
+ gem.summary = "Goal of this gem is to add a simple but powerful event system for iBeacon detection."
9
+ gem.homepage = "https://bitbucket.org/vencacodium/motion-beacon"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ #gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "motion-beacon"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Motion::Beacon::VERSION
17
+ gem.add_dependency "sugarcube-timer"
18
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-beacon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - CODIUM
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sugarcube-timer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: detect iBeacons with simple events
28
+ email:
29
+ - vaclav.s@codium.co
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - .repl_history
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - app/app_delegate.rb
42
+ - lib/motion-beacon.rb
43
+ - lib/motion-beacon/beaconmanager.rb
44
+ - lib/motion-beacon/clbeaconregion.rb
45
+ - lib/motion-beacon/version.rb
46
+ - motion-beacon.gemspec
47
+ homepage: https://bitbucket.org/vencacodium/motion-beacon
48
+ licenses: []
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.0.3
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Goal of this gem is to add a simple but powerful event system for iBeacon
70
+ detection.
71
+ test_files: []