Created
November 8, 2011 16:10
-
-
Save vishnugopal/1348213 to your computer and use it in GitHub Desktop.
Bluetooth Scan & Send
This file contains hidden or 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
# From: http://refactormycode.com/codes/1252-bluetooth-device-class | |
class Device | |
@@devices = {} | |
def self.new mac, *args | |
@@devices[mac] ||= super | |
end | |
MAC_PATTERN = /(?:[0-9a-f]{2}\:){5}[0-9a-f]{2}/ | |
NAME_PATTERN = /[^\n]*/ | |
DEVICE_SCAN_REGEXP = /(#{MAC_PATTERN})\t(#{NAME_PATTERN})/ | |
def self.find_new | |
devices = `hcitool scan --flush` | |
devices.scan(DEVICE_SCAN_REGEXP).map do |(mac, name)| | |
Device.new mac, name | |
end | |
end | |
attr_reader :mac, :name | |
def initialize mac, name | |
@mac, @name = mac, name | |
@sent = {} | |
end | |
def sent? file | |
@sent[file] | |
end | |
def send! file, timeout=8 | |
result = `ussp-push --timeo #{timeout} #{@mac}@ #{file} #{file.name}` | |
@sent[file] = ($?.to_i == 0 && result.scan(/Connection established/)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment