EpixP3
Posts: 6
Joined: Sat Apr 18, 2015 7:07 pm

How to turn on Bluetooth Discoveribility in python?

Mon May 25, 2015 6:42 pm

There must be an library or method to turn bluetooth discoveriblity in python, but i need it because when i tried via subprocess, evrything crashed:

Code: Select all

subprocess.call(['sudo hciconfig hci0 piscan'])
And i need a way to enable bluetooth discoveriblity in original python code, not shell. If there is some, send me a response.

Thanks!

twin802
Posts: 4
Joined: Wed May 27, 2015 9:31 pm

Re: How to turn on Bluetooth Discoveribility in python?

Wed May 27, 2015 9:35 pm

Would you like the python program to change to discoverable, using PyBluez? Or are you wanting to change the actual raspberry pi discoverable by your devices?

EpixP3
Posts: 6
Joined: Sat Apr 18, 2015 7:07 pm

Re: How to turn on Bluetooth Discoveribility in python?

Fri May 29, 2015 8:17 am

Thanks for response. I want to turn on discoveriblity in python using bluez.

twin802
Posts: 4
Joined: Wed May 27, 2015 9:31 pm

Re: How to turn on Bluetooth Discoveribility in python?

Fri May 29, 2015 2:55 pm

http://homepages.ius.edu/RWISMAN/C490/h ... etooth.htm

Check out the website, specifically look into the programming of the server. I believe that when the server "listens" on a port, then the bluetooth will become discoverable. But, I never had much luck with bluetooth.

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

Re: How to turn on Bluetooth Discoveribility in python?

Fri May 29, 2015 5:12 pm

There are a number of ways to do this. One is with the subprocess, but I would remove the 'sudo', and run the Python script with sudo. PyBluez is another. Or you could open a control socket and talk to the HCI directly (I might have some code to do that this weekend). Or you could use the BlueZ D-Bus API, but that involves more coding.

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

Re: How to turn on Bluetooth Discoveribility in python?

Fri May 29, 2015 5:15 pm

twin802 wrote:I believe that when the server "listens" on a port, then the bluetooth will become discoverable.
No, that's not correct. Discoverable mode is separate from listening.

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

Re: How to turn on Bluetooth Discoveribility in python?

Fri May 29, 2015 6:10 pm

Actually, the subprocess with 'sudo' works fine, as long as you get the syntax right.

Code: Select all

subprocess.call(['sudo', 'hciconfig', 'hci0', 'piscan'])
This is by far the easiest method

EpixP3
Posts: 6
Joined: Sat Apr 18, 2015 7:07 pm

Re: How to turn on Bluetooth Discoveribility in python?

Wed Jun 03, 2015 9:11 pm

Actually, the subprocess with 'sudo' works fine, as long as you get the syntax right.
CODE: SELECT ALL
subprocess.call(['sudo', 'hciconfig', 'hci0', 'piscan'])

This is by far the easiest method
Thanks! It really worked, it solved my problem!

radiobrain
Posts: 1
Joined: Sun Nov 11, 2012 2:21 am

Re: How to turn on Bluetooth Discoveribility in python?

Sun Dec 27, 2015 7:36 pm

old topic but i came across it looking for a solution to. To do it in bluez5 you do this and most other things through d-bus.

d-feet is a good tool for looking at the d-bus but it doesn't show you everything.
dbus-monitor --system will show you whats going on with the d-bus

python D-Bus library tutorial
http://dbus.freedesktop.org/doc/dbus-py ... orial.html

theres some pretty minimal documentation on these interfaces and examples in the bluez git repo
http://git.kernel.org/cgit/bluetooth/bluez.git/tree/

Code: Select all

import dbus
import dbus.mainloop.glib

global mainloop

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

bus = dbus.SystemBus()

adapter_props = dbus.Interface(bus.get_object('org.bluez', '/org/bluez/hci0'),
                                   "org.freedesktop.DBus.Properties");

adapter_props.Set("org.bluez.Adapter1", "Discoverable", dbus.Boolean(1))
bonus... this is how you send a command to a audio device connected over bluetooth via AVRCP. In this case it is issuing a pause command. 8_50_E6_73_23_4C is the MAC address of the paired and connected BT device

Code: Select all

import dbus
bus = dbus.SystemBus()
player = bus.get_object('org.bluez','/org/bluez/hci0/dev_D8_50_E6_73_23_4C/player0')
#player.Pause(dbus_interface='org.bluez.MediaPlayer1')
BT_Media_iface = dbus.Interface(player, dbus_interface='org.bluez.MediaPlayer1')
BT_Media_iface.Pause()

Karthikvembuli
Posts: 2
Joined: Tue Apr 02, 2019 12:54 pm

Re: How to turn on Bluetooth Discoveribility in python?

Mon Jun 24, 2019 9:46 am

from subprocess import call
import subprocess

subprocess.call(['sudo', 'hciconfig', 'hci0', 'piscan'])

really works fine. Thanks much

Return to “Python”