I have made a python script based on this github in order to use ble scanning/advertising.

This script will run a long time and the Bluetooth thread might have a problem which caused them to stop. I wanted to know if there is any way to know if there are still running. (So I can restart them if not)

There might be a hci_command which returns the states of the Wi-Fi chip but I am unable to find it. Do you have any idea on how I can do that ?

To start the le scan, I execute this command on my python script:

hci_send_cmd(sock, OGF_LE_CTL, OCF_LE_SET_SCAN_ENABLE, 0X01)

To start the advertising, I execute this one:

hci_send_cmd(sock, OGF_LE_CTL, OCF_LE_SET_ADVERTISE_ENABLE, 0X01)

Now, I want to regularly check if Bluetooth scanning/advertising are still running.

EDIT: I talked about thread but it doesn't work like this, when you start scanning/advertising it doesn't create thread. I just need to find a way to ask to the Bluetooth chip if it scan and advertise.

  • I added the command I execute to start the scanning/advertising threads. Jul 5, 2019 at 1:49

2 Answers 2


I just tested it with a python script I wrote myself (not the one you used, but thread is thread I guess).

So I used python programname.py >> test.txt &
to fork the process of and send output to a textfile "test.txt" so it won't block our console (I'm assuming you are using the raspberry without a GUI, if not you can ignore the last command and just run the script)

Then I used htop to open task manager and confirmed thread is running.

Now, run a shellscript ( or simple command ) :

ps aux | grep programname

I can confirm that my python script is running besides some other information.

Now just loop this command and wait until the thread is dead.....

Explanation: the | (vertical line, should be altGr + < button) is a pipe which leads the output of the ps program as input to the grep program. You could technically chain on forever with this. The grep command then "grabs" the line associated with it's argument, in this case "progname" and outputs it.

If you want further information about the ps program, have a look here: Unix Stackexchange

In short, these are the parameters we just used:

a = show processes for all users
u = display the process's user / owner
x = also show processes not attached to a terminal

(U might not need all 3 of them, have no time to fiddle around with it, just quickly confirmed it finds the correct python thread and tells you it's pid)


If you only wanted to check the status of the bluetooth manager (blueman I assume), you can go for

systemctl status bluetooth

If the bluetooth deamon is not running (it sometimes seems to not start correctly after boot) and you want to manually activate it, use

sudo systemctl start bluetooth 

I hope you can get your script to work,


  • 1
    Hi saraba, thank you for your response. Also, I don't think it will work in my case. In my main script, I open 2 threads by using the Bluetooth python libraries and the command "hci_send_cmd". Both of those threads are then created automatically by the library and their name in htop/ps aux are both the same name as my python script. Jul 5, 2019 at 1:35
  • And the "systemctl status bluetooth" doesn't give enough information because the bluetooth manager can be running without scanning/advertising BLE. Jul 5, 2019 at 1:53
  • sad to hear that, at least you are on the right way, I guess. can u rewrite the script / library? I believe there is a way to forge a Thread name, or at least force a PID number for a thread.... But don't know , am at work right now. Good luck with that
    – clockw0rk
    Jul 10, 2019 at 12:02

bluetoothctl show, which output the current status of your Bluetooth controller:

sudo bluetoothctl show
Controller DC:A6:32:XX:XX:XX (public)
    Name: pi4-ap1.f1linux.com
    Alias: pi4-ap1.f1linux.com
    Class: 0x00480000
    Powered: yes
    Discoverable: no
    Pairable: yes
    UUID: Headset AG                (00001112-0000-1000-8000-00805f9b34fb)
    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
    UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
    UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
    UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
    UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
    UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
    UUID: Handsfree Audio Gateway   (0000111f-0000-1000-8000-00805f9b34fb)
    Modalias: usb:v1D6Bp0246d12345
    Discovering: no

Then you could screen-scrape the output something like:

sudo bluetoothctl show | awk 'FNR==6' | xargs

Which extracts just the "Discoverable" status:

Discoverable: no


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.