Class: ScanBeacon::BlueZAdvertiser
- Inherits:
-
Object
- Object
- ScanBeacon::BlueZAdvertiser
- Defined in:
- lib/scan_beacon/bluez_advertiser.rb
Instance Attribute Summary collapse
-
#ad ⇒ Object
Returns the value of attribute ad.
-
#addr ⇒ Object
Returns the value of attribute addr.
-
#beacon ⇒ Object
Returns the value of attribute beacon.
-
#parser ⇒ Object
Returns the value of attribute parser.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ BlueZAdvertiser
constructor
A new instance of BlueZAdvertiser.
- #inspect ⇒ Object
- #random_addr ⇒ Object
- #rotate_addr_and_update_ad ⇒ Object
- #start ⇒ Object
- #start_with_random_addr ⇒ Object
- #stop ⇒ Object
- #update_ad ⇒ Object
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
#ad ⇒ Object
Returns the value of attribute ad.
4 5 6 |
# File 'lib/scan_beacon/bluez_advertiser.rb', line 4 def ad @ad end |
#addr ⇒ Object
Returns the value of attribute addr.
4 5 6 |
# File 'lib/scan_beacon/bluez_advertiser.rb', line 4 def addr @addr end |
#beacon ⇒ Object
Returns the value of attribute beacon.
4 5 6 |
# File 'lib/scan_beacon/bluez_advertiser.rb', line 4 def beacon @beacon end |
#parser ⇒ Object
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
#inspect ⇒ Object
44 45 46 |
# File 'lib/scan_beacon/bluez_advertiser.rb', line 44 def inspect "<BlueZAdvertiser ad=#{@ad.inspect}>" end |
#random_addr ⇒ Object
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_ad ⇒ Object
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 |
#start ⇒ Object
31 32 33 |
# File 'lib/scan_beacon/bluez_advertiser.rb', line 31 def start BlueZ.start_advertising @device_id, nil end |
#start_with_random_addr ⇒ Object
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 |
#stop ⇒ Object
40 41 42 |
# File 'lib/scan_beacon/bluez_advertiser.rb', line 40 def stop BlueZ.stop_advertising @device_id end |
#update_ad ⇒ Object
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 |