Class: ScanBeacon::BlueZAdvertiser
Instance Attribute Summary collapse
#ad, #advertising, #beacon, #parser
Instance Method Summary
collapse
#ad=, #beacon=, #parser=, #update_ad
Constructor Details
Returns a new instance of 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
#addr ⇒ Object
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
#inspect ⇒ Object
33
34
35
|
# File 'lib/scan_beacon/bluez_advertiser.rb', line 33
def inspect
"<BlueZAdvertiser ad=#{@ad.inspect}>"
end
|
#random_addr ⇒ Object
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]
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_ad ⇒ Object
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
if with_rotation
@addr = random_addr
BlueZ.start_advertising @device_id, addr
else
BlueZ.start_advertising @device_id, nil
end
@advertising=true
end
|
#stop ⇒ Object
28
29
30
31
|
# File 'lib/scan_beacon/bluez_advertiser.rb', line 28
def stop
BlueZ.stop_advertising @device_id
@advertising=false
end
|