Class: Lignite::Connection::Bluetooth

Inherits:
Lignite::Connection show all
Defined in:
lib/lignite/connection/bluetooth.rb

Constant Summary collapse

AF_BLUETOOTH =
31
BTPROTO_RFCOMM =
3

Instance Method Summary collapse

Methods inherited from Lignite::Connection

create

Constructor Details

#initialize(address = address_from_file) ⇒ Bluetooth

Returns a new instance of Bluetooth.

Parameters:

  • address (String) (defaults to: address_from_file)

    “11:22:33:44:55:66”



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lignite/connection/bluetooth.rb', line 10

def initialize(address = address_from_file)
  @sock = Socket.new(AF_BLUETOOTH, :STREAM, BTPROTO_RFCOMM)
  addr_b = address.split(/:/).map { |x| x.to_i(16) }
  channel = 1
  sockaddr = [AF_BLUETOOTH, 0, *addr_b.reverse, channel, 0].pack("C*")
  # common exceptions:
  # "Errno::EHOSTUNREACH: No route to host": BT is disabled;
  #   use `hciconfig hci0 up`
  # "Errno::EHOSTDOWN: Host is down": Turn the brick on, enable BT
  @sock.connect(sockaddr)
end

Instance Method Details

#address_from_fileObject



22
23
24
25
26
# File 'lib/lignite/connection/bluetooth.rb', line 22

def address_from_file
  fn = "#{ENV['HOME']}/.config/lignite-btaddr"
  s = File.read(fn)
  s.lines.grep(/^[0-9a-fA-F]/).first.strip
end

#read(n) ⇒ Object



28
29
30
# File 'lib/lignite/connection/bluetooth.rb', line 28

def read(n)
  @sock.recv(n)
end

#write(s) ⇒ Object



32
33
34
# File 'lib/lignite/connection/bluetooth.rb', line 32

def write(s)
  @sock.write(s)
end