Class: ScanBeacon::GenericScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/scan_beacon/generic_scanner.rb

Direct Known Subclasses

BLE112Scanner, BlueZScanner, CoreBluetoothScanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ GenericScanner

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

#beaconsObject (readonly)

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, rssi)
  if idx = @beacons.find_index(beacon)
    @beacons[idx].add_type beacon.beacon_types.first
    beacon = @beacons[idx]
  else
    @beacons << beacon
  end
  beacon.add_rssi(rssi)
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, rssi, ad_type)
  beacon = nil
  if @parsers.detect {|parser| beacon = parser.parse(data, ad_type) }
    beacon.mac = device
    add_beacon(beacon, rssi)
  end
end

#each_advertisementObject

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/scan_beacon/generic_scanner.rb', line 36

def each_advertisement
  raise NotImplementedError
end

#scanObject



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, rssi, ad_type = BeaconParser::AD_TYPE_MFG|
    detect_beacon(data, device, rssi, 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