Sha256: 5f615a82bf0b04a82f37946d063ee0ae29b9710464cf58ee6f1b2db2010a3a92
Contents?: true
Size: 1.19 KB
Versions: 1
Compression:
Stored size: 1.19 KB
Contents
require "socket" module Lignite class Connection class Bluetooth < Connection AF_BLUETOOTH = 31 BTPROTO_RFCOMM = 3 # @param address [String] "11:22:33:44:55:66" 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 def self.config_filename "#{ENV['HOME']}/.config/lignite-btaddr" end def self.template_config_filename # TODO: also find it from a gem File.expand_path("../../../../data/lignite-btaddr", __FILE__) end def address_from_file s = File.read(self.class.config_filename) s.lines.grep(/^[0-9a-fA-F]/).first.strip end def read(n) @sock.recv(n) end def write(s) @sock.write(s) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
lignite-0.1.1 | lib/lignite/connection/bluetooth.rb |