Class: HcitoolsWrapper::Detect

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

Instance Method Summary collapse

Constructor Details

#initialize(bd_address: '', refreshinterval: 60, verbose: false) ⇒ Detect

Returns a new instance of Detect.



38
39
40
41
42
43
44
# File 'lib/hcitools_wrapper.rb', line 38

def initialize(bd_address: '', refreshinterval: 60, verbose: false)

  @bd_address = bd_address      
  @refreshinterval = refreshinterval
  @verbose = verbose

end

Instance Method Details

#startObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hcitools_wrapper.rb', line 46

def start()

  id = @bd_address.split(':').reverse.join(' ')
  found, last_found = false, Time.now - 60
  a = []


  IO.popen('sudo hcidump --raw').each_line do |x| 
    
    found = if found then

      rssi = (x.split.last.hex - 256)
      a << rssi unless a.include? rssi
      h = {bdaddress: @bd_address, rssi: rssi}

      avg = a.max + (a.min - a.max) / 2

      if @verbose then
        puts Time.now.inspect + ':  ' + h.inspect
        puts "max: %s, min: %s, average: %s, a: %s" % \
                                [a.max, a.min, avg, a.sort.inspect]

      end
      
      recent_movement = Time.now - last_found >= @refreshinterval

      # the RSSI will vary by up to 10 when the device is stationary
      if recent_movement or a.length > 10 then

        puts 'movement!' if @verbose
        
        if block_given? then
          yield( a, (rssi == a.min and rssi < -85) ? rssi : avg)
        end

        a = []
      end
    
      last_found = Time.now

      false

    else
      x.include?(id)
    end

  end

end