Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1275

Python • Bluetooth device not listed on pacmd list links

$
0
0
I got the following code in python to connect to bluetooth and list all sinks devices
take note that I am running this upon startup using cron job

I got 2 device connected to Pi Zero 2

A bluetooth speacker and Logitecth G733 headset (for mic)

Code:

import subprocessimport time# Function to connect to a Bluetooth device using bluetoothctl#import subprocessdef connect_to_bluetooth_device(mac_address):    try:        # Restart PulseAudio        subprocess.run(['pulseaudio', '--kill'], check=True)        subprocess.run(['pulseaudio', '--start'], check=True)        text = subprocess.run(['pulseaudio','--check', '-v'],check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)        with open('logs.txt','a') as f:            f.write(text.stdout)            f.write(text.stderr)        # Wait for PulseAudio to restart        time.sleep(2)        # Command to connect to the Bluetooth device and get info        command = f"echo 'connect {mac_address}' | bluetoothctl"        # Execute the command to connect        subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)        # Wait for 2 seconds        time.sleep(2)        # Command to get info about the Bluetooth device        command = f"echo 'info {mac_address}\nquit' | bluetoothctl"        # Execute the command to get info        result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)        # Write the result to a log file        with open('logs.txt', 'a') as f:            f.write(result.stdout)            f.write(result.stderr)        # Print the result        print(result.stdout)        print(result.stderr)        print("Successfully connected to Bluetooth device.")    except subprocess.CalledProcessError as e:        print(f"Error: {e}")# Function to list all sinksdef list_all_sinks():    command = "pacmd list-sinks"    try:        output = subprocess.check_output(command, shell=True, text=True)        print("------------ List of All Sinks ------------")        print(output)        print("--------------------------------------------")        # Write sink list to logs.txt        with open("logs.txt", "a") as f:            f.write("------------ List of All Sinks ------------\n")            f.write(output)            f.write("--------------------------------------------\n")    except subprocess.CalledProcessError as e:        print(f"Error: {e}")# Function to execute sample.pydef execute_sample_script():    try:        subprocess.run(["paplay", "/home/admin/sample.wav"], check=True)        print("Sample script executed successfully.")    except subprocess.CalledProcessError as e:        print(f"Error: {e}")# Main functiondef main():    # Bluetooth device MAC address    bluetooth_mac_address = "CC:14:BC:32:F9:39"    # Connect to Bluetooth device    connect_to_bluetooth_device(bluetooth_mac_address)    time.sleep(5)    connect_to_bluetooth_device(bluetooth_mac_address)    time.sleep(2)    # List all sinks    list_all_sinks()    time.sleep(5)    list_all_sinks()    time.sleep(5)    list_all_sinks()    # Execute sample.py script    #execute_sample_script()if __name__ == "__main__":    main()

since im trying to connect to bluetooth here is the log

Code:

I: [pulseaudio] main.c: Daemon running as PID 1196Waiting to connect to bluetoothd...[bluetooth]#[bluetooth]#Agent registered[bluetooth]# info CC:14:BC:32:F9:39Device CC:14:BC:32:F9:39 (public)        Name: EDIFIER MP85        Alias: EDIFIER MP85        Class: 0x00240414        Icon: audio-card        Paired: yes        Trusted: no        Blocked: no        Connected: no        LegacyPairing: no        UUID: Serial Port               (00001101-0000-1000-8000-00805f9b34fb)        UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)        UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)        UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)        UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)        UUID: Vendor specific           (fe010000-1234-5678-abcd-00805f9b34fb)        Modalias: bluetooth:v05D6p000Ad0240[bluetooth]# quit[bluetooth]#I: [pulseaudio] main.c: Daemon running as PID 1293Waiting to connect to bluetoothd...[bluetooth]#[bluetooth]#[EDIFIER MP85]#Agent registered[EDIFIER MP85]# info CC:14:BC:32:F9:39Device CC:14:BC:32:F9:39 (public)        Name: EDIFIER MP85        Alias: EDIFIER MP85        Class: 0x00240414        Icon: audio-card        Paired: yes        Trusted: no        Blocked: no        Connected: yes        LegacyPairing: no        UUID: Serial Port               (00001101-0000-1000-8000-00805f9b34fb)        UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)        UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)        UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)        UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)        UUID: Vendor specific           (fe010000-1234-5678-abcd-00805f9b34fb)        Modalias: bluetooth:v05D6p000Ad0240[EDIFIER MP85]# quit[EDIFIER MP85]#
now on second attempt i was able to connect to the bluetooth and I did make sure pulseaudio is running or the Deamon and here's the problem whem I call the list sinks command I didn;t get the bluetooth audio

Code:

------------ List of All Sinks ------------1 sink(s) available.  * index: 0        name: <alsa_output.usb-Logitech_G733_Gaming_Headset_0000000000000000-00.analog-stereo>        driver: <module-alsa-card.c>        flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY        state: SUSPENDED        suspend cause: IDLE        priority: 9049        volume: front-left: 65536 / 100% / 0.00 dB,   front-right: 65536 / 100% / 0.00 dB                balance 0.00        base volume: 65536 / 100% / 0.00 dB        volume steps: 65537        muted: no        current latency: 0.00 ms        max request: 0 KiB        max rewind: 0 KiB        monitor source: 0        sample spec: s16le 2ch 48000Hz        channel map: front-left,front-right                     Stereo        used by: 0        linked by: 0        fixed latency: 9.96 ms        card: 0 <alsa_card.usb-Logitech_G733_Gaming_Headset_0000000000000000-00>        module: 7        properties:                alsa.resolution_bits = "16"                device.api = "alsa"                device.class = "sound"                alsa.class = "generic"                alsa.subclass = "generic-mix"                alsa.name = "USB Audio"                alsa.id = "USB Audio"                alsa.subdevice = "0"                alsa.subdevice_name = "subdevice #0"                alsa.device = "0"                alsa.card = "1"                alsa.card_name = "G733 Gaming Headset"                alsa.long_card_name = "Logitech G733 Gaming Headset at usb-3f980000.usb-1, full speed"                alsa.driver_name = "snd_usb_audio"                device.bus_path = "platform-3f980000.usb-usb-0:1:1.0"                sysfs.path = "/devices/platform/soc/3f980000.usb/usb1/1-1/1-1:1.0/sound/card1"                udev.id = "usb-Logitech_G733_Gaming_Headset_0000000000000000-00"                device.bus = "usb"                device.vendor.id = "046d"                device.vendor.name = "Logitech, Inc."                device.product.id = "0afe"                device.product.name = "G733 Gaming Headset"                device.serial = "Logitech_G733_Gaming_Headset_0000000000000000"                device.form_factor = "headset"                device.string = "front:1"                device.buffering.buffer_size = "1912"                device.buffering.fragment_size = "380"                device.access_mode = "mmap"                device.profile.name = "analog-stereo"                device.profile.description = "Analog Stereo"                device.description = "G733 Gaming Headset Analog Stereo"                module-udev-detect.discovered = "1"                device.icon_name = "audio-headset-usb"                device.intended_roles = "phone"        ports:                analog-output: Analog Output (priority 9900, latency offset 0 usec, available: unknown)                        properties:        active port: <analog-output>--------------------------------------------
the bluetooth is connected and I want to set it as default output device bluetooth is not here,
however when I login to through putty and run the python script it work as intended...

Statistics: Posted by mugichan — Tue May 07, 2024 7:02 pm



Viewing all articles
Browse latest Browse all 1275

Trending Articles