I'm currently using pexpect in python to use bluetoothctl. I can get a list of all bluetooth devices, but is there a way to narrow down the list to only audio devices (bluetooth speakers, headphones).
Here is my code so far:
import time
import pexpect
#returns list of all availible bluetooth devices
def scan_bluetooth_agents():
devices = {}
child = pexpect.spawn('sudo bluetoothctl')
child.sendline ('scan on')
time.sleep(10)
child.sendline ('scan off')
line = child.readline()
while b'scan off' not in line:
if b'Device' in line:
line = str(line.replace(b"\r\n", b'')).strip("b'").strip("'")
address, name = line.split('Device ')[1].split(' ', 1)
devices[name] = address
line = child.readline()
child.sendline ('exit')
return devices
#this is the function that needs to return only audio devices
def valid_devices(devices):
output = {}
i = 0
for device in devices:
#if device contrains 'JBL' then output[i] = device
if __name__ == "__main__":
devices = scan_bluetooth_agents()
print('All Devices:')
print(devices)
print('Valid Audio Devices:')
audioDevices = valid_devices(devices)