1

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. – Antoine Boré Jul 5 at 1:49
1

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)

EDIT:

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,

saraba

  • 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. – Antoine Boré Jul 5 at 1:35
  • And the "systemctl status bluetooth" doesn't give enough information because the bluetooth manager can be running without scanning/advertising BLE. – Antoine Boré Jul 5 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 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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