Class: ScanBeacon::BLE112Device
- Inherits:
-
Object
- Object
- ScanBeacon::BLE112Device
- Defined in:
- lib/scan_beacon/ble112_device.rb
Defined Under Namespace
Classes: BLE112Response
Constant Summary collapse
- BG_COMMAND =
define a bunch of constants
0- BG_EVENT =
0x80- BG_MSG_CLASS_SYSTEM =
msg classes
0- BG_MSG_CLASS_CONNECTION =
3- BG_MSG_CLASS_GAP =
6- BG_RESET =
messages
0- BG_DISCONNECT =
0- BG_SET_MODE =
1- BG_DISCOVER =
2- BG_DISCOVER_STOP =
4- BG_SCAN_PARAMS =
7- BG_GAP_DISCOVER_ALL =
constants/enums
2- BG_GAP_NON_DISCOVERABLE =
0- BG_GAP_NON_CONNECTABLE =
0
Instance Method Summary collapse
- #configure_port ⇒ Object
-
#initialize(port = nil) ⇒ BLE112Device
constructor
A new instance of BLE112Device.
- #open ⇒ Object
- #read ⇒ Object
- #reset ⇒ Object
- #start_scan ⇒ Object
- #stop_scan ⇒ Object
Constructor Details
#initialize(port = nil) ⇒ BLE112Device
Returns a new instance of BLE112Device.
23 24 25 |
# File 'lib/scan_beacon/ble112_device.rb', line 23 def initialize(port=nil) @port = port || Dir.glob("/dev/{cu.usbmodem,ttyACM}*")[0] end |
Instance Method Details
#configure_port ⇒ Object
36 37 38 39 40 |
# File 'lib/scan_beacon/ble112_device.rb', line 36 def configure_port if RUBY_PLATFORM =~ /linux/ system("stty -F #{@port} 115200 raw -brkint -icrnl -imaxbel -opost -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke") end end |
#open ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/scan_beacon/ble112_device.rb', line 27 def open configure_port File.open(@port, 'r+b') do |file| @file = file yield self end @file = nil end |
#read ⇒ Object
59 60 61 |
# File 'lib/scan_beacon/ble112_device.rb', line 59 def read BLE112Response.new( bg_read(@file) ) end |
#reset ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/scan_beacon/ble112_device.rb', line 63 def reset open do @file.write([BG_COMMAND, 1, BG_MSG_CLASS_SYSTEM, BG_RESET, 0].pack('C*')) end # give time for the device to reboot. # TODO: figure out a way that doesn't involve sleeping arbitrarily. sleep 1 end |
#start_scan ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/scan_beacon/ble112_device.rb', line 42 def start_scan # disconnect any connections bg_command(@file, BG_MSG_CLASS_CONNECTION, BG_DISCONNECT,0) # turn off adverts bg_command(@file, BG_MSG_CLASS_GAP, BG_SET_MODE, [BG_GAP_NON_DISCOVERABLE, BG_GAP_NON_CONNECTABLE]) # stop previous scan bg_command(@file, BG_MSG_CLASS_GAP, BG_DISCOVER_STOP) # write new scan params bg_command(@file, BG_MSG_CLASS_GAP, BG_SCAN_PARAMS, [200,200, 0], "S<S<C") # start new scan bg_command(@file, BG_MSG_CLASS_GAP, BG_DISCOVER, BG_GAP_DISCOVER_ALL) end |
#stop_scan ⇒ Object
55 56 57 |
# File 'lib/scan_beacon/ble112_device.rb', line 55 def stop_scan bg_command(@file, BG_MSG_CLASS_GAP, BG_DISCOVER_STOP) end |