class Lignite::Connection::Bluetooth

A {Connection} over Bluetooth

Constants

AF_BLUETOOTH
BTPROTO_RFCOMM

Public Class Methods

config_filename() click to toggle source
# File lib/lignite/connection/bluetooth.rb, line 28
def self.config_filename
  "#{ENV["HOME"]}/.config/lignite-btaddr"
end
new(address = address_from_file) click to toggle source

@param address [String] “11:22:33:44:55:66”

Calls superclass method Lignite::Connection::new
# File lib/lignite/connection/bluetooth.rb, line 11
def initialize(address = address_from_file)
  super()
  @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":
  #   - No BT adapter
  #   - BT is disabled; use `hciconfig hci0 up`
  # "Errno::EHOSTDOWN: Host is down":
  #   - Turn the brick on
  #   - enable BT on the brick
  #   - disconnect other programming apps
  @sock.connect(sockaddr)
end
template_config_filename() click to toggle source
# File lib/lignite/connection/bluetooth.rb, line 32
def self.template_config_filename
  # TODO: also find it from a gem
  File.expand_path("../../../../data/lignite-btaddr", __FILE__)
end

Public Instance Methods

address_from_file() click to toggle source
# File lib/lignite/connection/bluetooth.rb, line 37
def address_from_file
  s = File.read(self.class.config_filename)
  s.lines.grep(/^[0-9a-fA-F]/).first.strip
end
close() click to toggle source
Calls superclass method Lignite::Connection#close
# File lib/lignite/connection/bluetooth.rb, line 50
def close
  @sock.shutdown
  super
end
read(n) click to toggle source
# File lib/lignite/connection/bluetooth.rb, line 42
def read(n)
  @sock.recv(n)
end
write(s) click to toggle source
# File lib/lignite/connection/bluetooth.rb, line 46
def write(s)
  @sock.write(s)
end