-
-
Save dsisnero/8d1e4513f10b51568fb786819e3e879e to your computer and use it in GitHub Desktop.
Discover Bluetooth devices nearby using, guess what, fiddle :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fiddle/import' | |
require 'fiddle/types' | |
LIBBT = '/usr/lib/libbluetooth.so.3' | |
LIBC = '/usr/lib/libc.so.6' | |
module Bluetooth | |
extend Fiddle::Importer | |
dlload LIBBT, LIBC | |
typealias "uint8_t", "unsigned char" | |
typealias "uint16_t", "unsigned short" | |
#bdaddr = struct [ | |
# "uint8_t b[6]" | |
#] | |
InquiryInfo = struct [ | |
"uint8_t b[6]", | |
"uint8_t pscan_rep_mode", | |
"uint8_t pscan_period_mode", | |
"uint8_t pscan_mode", | |
"uint8_t dev_class[3]", | |
"uint16_t clock_offset" | |
] | |
extern "void *memset(void *str, int c, size_t n)" | |
extern "int hci_get_route(bdaddr_t *bdaddr)" | |
extern "int hci_open_dev(int dev_id)" | |
extern "int ba2str(const bdaddr_t *ba, char *str)" | |
extern "int hci_inquiry(int dev_id, int len, int max_rsp, const uint8_t *lap, inquiry_info **ii, long flags)" | |
extern "int hci_read_remote_name(int sock, const bdaddr_t *ba, int len, char *name, int timeout)" | |
end | |
ii = Bluetooth::InquiryInfo.malloc | |
max_rsp, len, flags = 0xff, 0x8, 0x1 | |
addr = Fiddle::Pointer.malloc(0x13) | |
name = Fiddle::Pointer.malloc(0xf8) | |
puts "Scanning..." | |
dev_id = Bluetooth.hci_get_route(Fiddle::NULL) | |
sock = Bluetooth.hci_open_dev(dev_id) | |
abort("error opening socket.") if [dev_id, sock].any? { |n| n < 0 } | |
num_rsp = Bluetooth.hci_inquiry(dev_id, len, max_rsp, Fiddle::NULL, ii, flags) | |
abort("hci inquiry failed.") if num_rsp < 0 | |
puts "device(s) found: #{num_rsp}" | |
num_rsp.times.with_index(1) do |_, i| | |
Bluetooth.ba2str(ii.to_ptr.ptr, addr) | |
Bluetooth.memset(name, 0, name.size) | |
if Bluetooth.hci_read_remote_name(sock, ii.to_ptr.ptr, name.size, name, 0) < 0 | |
name[0, name.size] = "[unknown]" | |
end | |
p "[%d] %s -> %s" % [i, addr, name] | |
end | |
##TODO FIXME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment