Class: ScanBeacon::BlueZAdvertiser

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

Instance Attribute Summary collapse

Attributes inherited from GenericAdvertiser

#ad, #advertising, #beacon, #parser

Instance Method Summary collapse

Methods inherited from GenericIndividualAdvertiser

#ad=, #beacon=, #parser=, #update_ad

Constructor Details

#initialize(opts = {}) ⇒ BlueZAdvertiser



6
7
8
9
10
11
12
# 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]
  super(opts)
end

Instance Attribute Details

#addrObject (readonly)

Returns the value of attribute addr.



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

def addr
  @addr
end

Instance Method Details

#inspectObject



33
34
35
# File 'lib/scan_beacon/bluez_advertiser.rb', line 33

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

#random_addrObject



43
44
45
46
47
48
49
# File 'lib/scan_beacon/bluez_advertiser.rb', line 43

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



37
38
39
40
41
# File 'lib/scan_beacon/bluez_advertiser.rb', line 37

def rotate_addr_and_update_ad
  self.update_ad
  self.stop
  self.start(true)
end

#start(with_rotation = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/scan_beacon/bluez_advertiser.rb', line 14

def start(with_rotation = false)
  BlueZ.set_advertisement_bytes @device_id, @ad
  # You must call start advertising any time you change the advertisement bytes
  # otherwise they won't take
  if with_rotation
    @addr = random_addr 
    BlueZ.start_advertising @device_id, addr
  else
    # have to pass nil for addr if we don't want to override it and use the internal
    BlueZ.start_advertising @device_id, nil
  end
  @advertising=true
end

#stopObject



28
29
30
31
# File 'lib/scan_beacon/bluez_advertiser.rb', line 28

def stop
  BlueZ.stop_advertising @device_id
  @advertising=false
end