daveyman123
Posts: 115
Joined: Fri Dec 12, 2014 2:21 am

python and bluetoothctl

Sat Jan 07, 2017 11:16 pm

How would I go about using BluetoothCTL in a python script

I want to write a script that connects a bluetooth device automatically


Thanks,
DM123

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: python and bluetoothctl

Sat Jan 07, 2017 11:27 pm

1. You could use pexpect to control bluetoothctl from Python.

2. You could use subprocess to run the following bash command:

Code: Select all

echo -e "connect AA:BB:CC:DD:EE \nquit" | bluetoothctl
3. You could run the previous command from a bash script and skip Python altogether.

daveyman123
Posts: 115
Joined: Fri Dec 12, 2014 2:21 am

Re: python and bluetoothctl

Sat Jan 07, 2017 11:40 pm

Thank you for the info. I assume ill use python since im trying to learn it for a class im taking soon.

You wouldnt also happen to know an easy way with python to my raspberry pi with a python script?
I would like to use python to connect from a windows to the Pi and then run the script.

Thanks,
DM123

daveyman123
Posts: 115
Joined: Fri Dec 12, 2014 2:21 am

Re: python and bluetoothctl

Sun Jan 08, 2017 12:21 am

Douglas6 wrote:1. You could use pexpect to control bluetoothctl from Python.

2. You could use subprocess to run the following bash command:

Code: Select all

echo -e "connect AA:BB:CC:DD:EE \nquit" | bluetoothctl
3. You could run the previous command from a bash script and skip Python altogether.
I ended up using this code to connect. Now I need to somehow use pacmd to connect pulseaudio

Code: Select all

from sh import bluetoothctl
mac = "AA:BB:CC:DD:EE"
bluetoothctl("connect",mac)
Only problem is that although it should work theoretically it just hangs up... I have a lot to learn

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: python and bluetoothctl

Sun Jan 08, 2017 1:52 am

I'm afraid I'm not familiar with the 'sh' module. Best of luck!

yash012
Posts: 12
Joined: Thu Mar 09, 2017 12:08 pm

Re: python and bluetoothctl

Tue Mar 21, 2017 6:56 am

Douglas6 wrote:1. You could use pexpect to control bluetoothctl from Python.

2. You could use subprocess to run the following bash command:

Code: Select all

echo -e "connect AA:BB:CC:DD:EE \nquit" | bluetoothctl
3. You could run the previous command from a bash script and skip Python altogether.
I know this is a very old post but when i used this command it return Glib critical source ID was not Found while attempting to remove it.

if you can tell me what to do about this.Thank you.

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: python and bluetoothctl

Tue Mar 21, 2017 7:23 am

You'll need to be more specific. What command is giving you the problem?

yash012
Posts: 12
Joined: Thu Mar 09, 2017 12:08 pm

Re: python and bluetoothctl

Tue Mar 21, 2017 7:31 am

when i entered

Code: Select all

echo -e "connect 77:XX:XX:XX:XX  \nquit" | bluetoothctl
gives me Glib Critical source ID 24 was not found, i then tried "scan on" and "list" but it gave me same error but with Source ID 29 not found or source ID 25 not found

yash012
Posts: 12
Joined: Thu Mar 09, 2017 12:08 pm

Re: python and bluetoothctl

Tue Mar 21, 2017 7:33 am

i was trying the bash command you suggested to connect to bluetoothe device

Return to “Python”