Class: BLE::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/ble/adapter.rb

Overview

Adapter class

Adapter.list
a = Adapter.new('hci0')
a.start_discover ; sleep(10) ; a.stop_discovery
a.devices

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iface) ⇒ Adapter

Create a new Adapter

Parameters:

  • iface (String)

    name of the Unix device


21
22
23
24
25
26
27
28
29
30
# File 'lib/ble/adapter.rb', line 21

def initialize(iface)
    @iface     = iface.dup.freeze
    @o_adapter = BLUEZ.object("/org/bluez/#{@iface}")
    @o_adapter.introspect
    
    # @o_adapter[I_PROPERTIES]
    #     .on_signal('PropertiesChanged') do |intf, props|
    #     end
    # end
end

Class Method Details

.listArray<String>

Return a list of available unix device name for the adapter installed on the system.

Returns:

  • (Array<String>)

    list of unix device name


12
13
14
15
16
# File 'lib/ble/adapter.rb', line 12

def self.list
    o_bluez = BLUEZ.object('/org/bluez')
    o_bluez.introspect
    o_bluez.subnodes.reject {|adapter| ['test'].include?(adapter) }
end

Instance Method Details

#[](address) ⇒ Device

Note:

The device object returned has a dependency on the adapter.

Return the device corresponding to the given address.

Parameters:

  • address

    MAC address of the device

Returns:


74
75
76
# File 'lib/ble/adapter.rb', line 74

def [](address)
    Device.new(@iface, address)
end

#addressString

The Bluetooth device address.

Returns:

  • (String)

    MAC address of the adapter


40
41
42
# File 'lib/ble/adapter.rb', line 40

def address
    @o_adapter[I_ADAPTER]['Address']
end

#aliasString

The Bluetooth friendly name. In case no alias is set, it will return the system provided name.

Returns:

  • (String)

53
54
55
# File 'lib/ble/adapter.rb', line 53

def alias
    @o_adapter[I_ADAPTER]['Alias']
end

#alias=(val) ⇒ void

This method returns an undefined value.

Set the alias name.

When resetting the alias with an empty string, the property will default back to system name

Parameters:

  • val (String)

    new alias name.


64
65
66
67
# File 'lib/ble/adapter.rb', line 64

def alias=(val)
    @o_adapter[I_ADAPTER]['Alias'] = val.nil? ? '' : val.to_str
    nil
end

#devicesArray<String>

List of devices MAC address that have been discovered.

Returns:

  • (Array<String>)

    List of devices MAC address.


156
157
158
159
160
# File 'lib/ble/adapter.rb', line 156

def devices
    @o_adapter.introspect # Force refresh
    @o_adapter.subnodes.map {|dev| # Format: dev_aa_bb_cc_dd_ee_ff
        dev[4..-1].tr('_', ':') }
end

#filter(uuids, rssi: nil, pathloss: nil, transport: :le) ⇒ self

TODO:

Need to sync with the adapter-api.txt

This method sets the device discovery filter for the caller. When this method is called with nil or an empty list of UUIDs, filter is removed.

Parameters:

  • uuids

    a list of uuid to filter on

  • rssi (defaults to: nil)

    RSSI threshold

  • pathloss (defaults to: nil)

    pathloss threshold

  • transport (:auto, :bredr, :le) (defaults to: :le)

    type of scan to run (default: :le)

Returns:

  • (self)

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ble/adapter.rb', line 90

def filter(uuids, rssi: nil, pathloss: nil, transport: :le)
    unless [:auto, :bredr, :le].include?(transport)
        raise ArgumentError,
              "transport must be one of :auto, :bredr, :le"
    end
    filter = { }
    
    unless uuids.nil? || uuids.empty?
        filter['UUIDs'    ] = DBus.variant('as', uuids)
    end
    unless rssi.nil?
        filter['RSSI'     ] = DBus.variant('n',  rssi)
    end
    unless pathloss.nil?
        filter['Pathloss' ] = DBus.variant('q',  pathloss)
    end
    unless transport.nil?
        filter['Transport'] = DBus.variant('s',  transport.to_s) 
    end
    
    @o_adapter[I_ADAPTER].SetDiscoveryFilter(filter)
    
    self
end

#ifaceString

The Bluetooth interface name

Returns:

  • (String)

    name of the Unix device


34
35
36
# File 'lib/ble/adapter.rb', line 34

def iface
    @iface
end

#nameString

The Bluetooth system name (pretty hostname).

Returns:

  • (String)

46
47
48
# File 'lib/ble/adapter.rb', line 46

def name
    @o_adapter[I_ADAPTER]['Name']
end

#start_discoveryBoolean

Starts the device discovery session. This includes an inquiry procedure and remote device name resolving. Use #stop_discovery to release the sessions acquired. This process will start creating device in the underlying api as new devices are discovered.

Returns:

  • (Boolean)

122
123
124
125
126
127
128
129
130
131
# File 'lib/ble/adapter.rb', line 122

def start_discovery
    @o_adapter[I_ADAPTER].StartDiscovery
    true
rescue DBus::Error => e
    case e.name
    when E_IN_PROGRESS then true
    when E_FAILED      then false
    else raise ScriptError
    end
end

#stop_discoveryBoolean

Note:

The discovery procedure is shared

This method will cancel any previous #start_discovery transaction. between all discovery sessions thus calling #stop_discovery will only release a single session.

Returns:

  • (Boolean)

140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ble/adapter.rb', line 140

def stop_discovery
    @o_adapter[I_ADAPTER].StopDiscovery
    true
rescue DBus::Error => e
    case e.name
    when E_FAILED         then false
    when E_NOT_READY      then false
    when E_NOT_AUTHORIZED then raise NotAuthorized
    else raise ScriptError
    end
    
end