Class: ScanBeacon::GenericScanner
- Inherits:
-
Object
- Object
- ScanBeacon::GenericScanner
show all
- Defined in:
- lib/scan_beacon/generic_scanner.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of GenericScanner.
6
7
8
9
10
|
# File 'lib/scan_beacon/generic_scanner.rb', line 6
def initialize(opts = {})
@cycle_seconds = opts[:cycle_seconds] || 1
@parsers = BeaconParser.default_parsers
@beacons = []
end
|
Instance Attribute Details
#beacons ⇒ Object
Returns the value of attribute beacons.
4
5
6
|
# File 'lib/scan_beacon/generic_scanner.rb', line 4
def beacons
@beacons
end
|
Instance Method Details
#add_beacon(beacon, rssi) ⇒ Object
48
49
50
51
52
53
54
55
56
|
# File 'lib/scan_beacon/generic_scanner.rb', line 48
def add_beacon(beacon, )
if idx = @beacons.find_index(beacon)
@beacons[idx].add_type beacon.beacon_types.first
beacon = @beacons[idx]
else
@beacons << beacon
end
beacon.()
end
|
#add_parser(parser) ⇒ Object
12
13
14
|
# File 'lib/scan_beacon/generic_scanner.rb', line 12
def add_parser(parser)
@parsers << parser
end
|
#detect_beacon(data, device, rssi, ad_type) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/scan_beacon/generic_scanner.rb', line 40
def detect_beacon(data, device, , ad_type)
beacon = nil
if @parsers.detect {|parser| beacon = parser.parse(data, ad_type) }
beacon.mac = device
add_beacon(beacon, )
end
end
|
#each_advertisement ⇒ Object
36
37
38
|
# File 'lib/scan_beacon/generic_scanner.rb', line 36
def each_advertisement
raise NotImplementedError
end
|
#scan ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/scan_beacon/generic_scanner.rb', line 16
def scan
@beacons = []
next_cycle = Time.now + @cycle_seconds
keep_scanning = true
each_advertisement do |data, device, , ad_type = BeaconParser::AD_TYPE_MFG|
detect_beacon(data, device, , ad_type) unless data.nil?
if Time.now > next_cycle
if block_given?
next_cycle = Time.now + @cycle_seconds
yield @beacons
@beacons = []
else
keep_scanning = false
end
end
end
return @beacons unless block_given?
end
|