Class: Radbeacon::LeScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/radbeacon/le_scanner.rb

Direct Known Subclasses

Scanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration = 5) ⇒ LeScanner

Returns a new instance of LeScanner.



8
9
10
11
# File 'lib/radbeacon/le_scanner.rb', line 8

def initialize(duration = 5)
  @duration = duration
  @options = {}
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



6
7
8
# File 'lib/radbeacon/le_scanner.rb', line 6

def duration
  @duration
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/radbeacon/le_scanner.rb', line 6

def options
  @options
end

Instance Method Details

#passive_scanObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/radbeacon/le_scanner.rb', line 34

def passive_scan
  devices = Array.new
  if @options[:enable_hcitool_duration] == true
    scan_output = self.scan_command_duration
  else
    scan_output = self.scan_command
  end
  scan_output.each_line do |line|
    result = line.scan(/^([A-F0-9:]{15}[A-F0-9]{2}) (.*)$/)
    if !result.empty?
      mac_address = result[0][0]
      name = result[0][1]
      if !devices.find {|s| s.mac_address == mac_address}
        filter_mac = @options[:filter_mac]
        if !filter_mac or (filter_mac.include?(mac_address) if filter_mac.is_a?(Array))
          device = BluetoothLeDevice.new(mac_address, name)
          devices << device
        end
      end
    end
  end
  devices
end

#scanObject



58
59
60
61
62
63
64
# File 'lib/radbeacon/le_scanner.rb', line 58

def scan
  devices = self.passive_scan
  devices.each do |dev|
    dev.fetch_characteristics
  end
  devices
end

#scan_commandObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/radbeacon/le_scanner.rb', line 13

def scan_command
  rout, wout = IO.pipe
  scan_command_str = "sudo hcitool lescan"
  pid = Process.spawn(scan_command_str, :out => wout)
  begin
    Timeout.timeout(@duration) do
      Process.wait(pid)
    end
  rescue Timeout::Error
    Process.kill('TERM', pid)
  end
  wout.close
  scan_output = rout.readlines.join("")
  rout.close
  scan_output
end

#scan_command_durationObject



30
31
32
# File 'lib/radbeacon/le_scanner.rb', line 30

def scan_command_duration
  `sudo hcitool lescan --duration #{@duration}`
end