Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
Last active October 7, 2018 03:05
Show Gist options
  • Save gr33n7007h/d86c3eaa3faa2ef3d1cf065db3518491 to your computer and use it in GitHub Desktop.
Save gr33n7007h/d86c3eaa3faa2ef3d1cf065db3518491 to your computer and use it in GitHub Desktop.
How to connect to a rfcomm/l2cap server in Ruby
#/usr/bin/env ruby
require 'socket'
addr = "00:11:22:33:44:55"
rf_chan = 23
l2_psm = 15
# rfcomm client
s = Socket.new 31, 1, 3 # (31 = AF_BLUETOOTH, 1 = SOCK_STREAM, 3 = BTPROTO_RCOMM)
sockaddr = [31, 0, *addr.split(':').reverse_each.map(&:hex), rf_chan, 0].pack('C*')
s.connect sockaddr
# perform operations
s.close
#l2cap client
s = Socket.new 31, 5, 0 # (31 = AF_BLUETOOTH, 5 = SOCK_SEQPACKET, 0 = BTPROTO_L2CAP)
sockaddr = [31, 0, l2_psm, 0, *addr.split(':').reverse_each.map(&:hex), 0, 0, 0, 0].pack('C*')
s.connect sockaddr
# perform operations
s.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment