Class: ScanBeacon::BlueZAdvertiser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BlueZAdvertiser



6
7
8
9
10
11
12
13
14
# File 'lib/scan_beacon/bluez_advertiser.rb', line 6

def initialize(opts = {})
  @device_id = opts[:device_id] || BlueZ.devices.map {|d| d[:device_id]}[0]
  raise "No available devices" if @device_id.nil?
  BlueZ.device_up @device_id
  @addr = @initial_addr = BlueZ.devices.find {|d| d[:device_id] == @device_id}[:addr]
  self.beacon = opts[:beacon]
  self.parser = opts[:parser]
  self.parser ||= BeaconParser.default_parsers.find {|parser| parser.beacon_type == beacon.beacon_types.first}
end

Instance Attribute Details

#adObject

Returns the value of attribute ad.



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

def ad
  @ad
end

#addrObject

Returns the value of attribute addr.



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

def addr
  @addr
end

#beaconObject

Returns the value of attribute beacon.



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

def beacon
  @beacon
end

#parserObject

Returns the value of attribute parser.



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

def parser
  @parser
end

Instance Method Details

#inspectObject



44
45
46
# File 'lib/scan_beacon/bluez_advertiser.rb', line 44

def inspect
  "<BlueZAdvertiser ad=#{@ad.inspect}>"
end

#random_addrObject



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

def random_addr
  data = @initial_addr + Time.now.to_s
  new_addr = Digest::SHA256.digest(data)[0..5]
  # the most significant bit must not be set!
  new_addr[0] = [(new_addr[0].unpack("C")[0] & 0x7F)].pack("C")
  new_addr.unpack("H2:H2:H2:H2:H2:H2").join(":")
end

#rotate_addr_and_update_adObject



52
53
54
55
56
# File 'lib/scan_beacon/bluez_advertiser.rb', line 52

def rotate_addr_and_update_ad
  self.update_ad
  self.stop
  self.start_with_random_addr
end

#startObject



31
32
33
# File 'lib/scan_beacon/bluez_advertiser.rb', line 31

def start
  BlueZ.start_advertising @device_id, nil
end

#start_with_random_addrObject



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

def start_with_random_addr
  addr = random_addr
  BlueZ.start_advertising @device_id, addr
end

#stopObject



40
41
42
# File 'lib/scan_beacon/bluez_advertiser.rb', line 40

def stop
  BlueZ.stop_advertising @device_id
end

#update_adObject



48
49
50
# File 'lib/scan_beacon/bluez_advertiser.rb', line 48

def update_ad
  self.ad = @parser.generate_ad(@beacon) if @parser && @beacon
end