Class: Rockbox::Api::Bluetooth

Inherits:
Object
  • Object
show all
Defined in:
lib/rockbox/api/bluetooth.rb

Constant Summary collapse

FIELDS =
<<~GQL
  fragment BluetoothDeviceFields on BluetoothDevice {
    address name paired trusted connected rssi
  }
GQL

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Bluetooth

Returns a new instance of Bluetooth.



14
15
16
# File 'lib/rockbox/api/bluetooth.rb', line 14

def initialize(http)
  @http = http
end

Instance Method Details

#connect(address) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rockbox/api/bluetooth.rb', line 35

def connect(address)
  @http.execute(
    "mutation BluetoothConnect($address: String!) { bluetoothConnect(address: $address) }",
    { address: address }
  )
  nil
end

#devicesObject

List paired/known Bluetooth devices (Linux only).



19
20
21
22
# File 'lib/rockbox/api/bluetooth.rb', line 19

def devices
  data = @http.execute("#{FIELDS}\nquery BluetoothDevices { bluetoothDevices { ...BluetoothDeviceFields } }")
  Array(data[:bluetooth_devices]).map { |d| BluetoothDevice.from_hash(d) }
end

#disconnect(address) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rockbox/api/bluetooth.rb', line 43

def disconnect(address)
  @http.execute(
    "mutation BluetoothDisconnect($address: String!) { bluetoothDisconnect(address: $address) }",
    { address: address }
  )
  nil
end

#scan(timeout: nil) ⇒ Object

Scan for nearby devices (Linux only).



25
26
27
28
29
30
31
32
33
# File 'lib/rockbox/api/bluetooth.rb', line 25

def scan(timeout: nil)
  data = @http.execute(<<~GQL, timeout ? { timeout_secs: timeout } : nil)
    #{FIELDS}
    mutation BluetoothScan($timeoutSecs: Int) {
      bluetoothScan(timeoutSecs: $timeoutSecs) { ...BluetoothDeviceFields }
    }
  GQL
  Array(data[:bluetooth_scan]).map { |d| BluetoothDevice.from_hash(d) }
end