Class: ScanBeacon::BLE112Scanner

Inherits:
GenericScanner show all
Defined in:
lib/scan_beacon/ble112_scanner.rb

Instance Attribute Summary collapse

Attributes inherited from GenericScanner

#beacons, #parsers

Instance Method Summary collapse

Methods inherited from GenericScanner

#add_beacon, #add_parser, #detect_beacon, #scan

Constructor Details

#initialize(opts = {}) ⇒ BLE112Scanner

Returns a new instance of BLE112Scanner.



6
7
8
9
10
# File 'lib/scan_beacon/ble112_scanner.rb', line 6

def initialize(opts = {})
  super
  @device = opts[:device] || BLE112Device.new(opts[:port])
  @device_addr = @device.open{ @device.get_addr }
end

Instance Attribute Details

#device_addrObject (readonly)

Returns the value of attribute device_addr.



4
5
6
# File 'lib/scan_beacon/ble112_scanner.rb', line 4

def device_addr
  @device_addr
end

Instance Method Details

#each_advertisementObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/scan_beacon/ble112_scanner.rb', line 12

def each_advertisement
  @device.open do |device|
    device.start_scan
    begin
      keep_scanning = true
      while keep_scanning do
        response = device.read
        if response && response.advertisement?
          if response.manufacturer_ad?
            keep_scanning = false if yield(response.advertisement_data, response.mac, response.rssi) == false
          else
            keep_scanning = false if yield(response.advertisement_data[4..-1], response.mac, response.rssi, 0x03) == false
          end
        else
          keep_scanning = false if yield(nil, nil, nil) == false
        end
      end
    ensure
      begin
        device.stop_scan
      rescue StandardError
        # don't crash trying to stop scan - it may be that the device was
        # unplugged
      end
    end
  end
end