ukBaz / python-bluezero

A simple Python interface to Bluez
MIT License
387 stars 112 forks source link

Stopping of Bluetooth Advertising #332

Closed ArthurKun21 closed 3 years ago

ArthurKun21 commented 3 years ago

I was using this example

I would like to know if there is a command there that can stop the bluetooth advertising?

The use case of this is that when certain event happen, python will open its bluetooth and wait for further instruction, once the command was receive it will then turn off

ukBaz commented 3 years ago

Currently the peripheral module does not have an API to stop the advertisement of the peripheral once created although there are lower level API's to allow that to happen. As you can see with the following code from publish, the advertisement is registered and unregistered.:

https://github.com/ukBaz/python-bluezero/blob/392a115ace80a6d7257b76708ec24b99ec9e2e5d/bluezero/peripheral.py#L130-L148

I am not sure exactly what you would want to "turn off" but you could do a quick proof of concept to remove the advert with the following added to the example.

ble_uart.ad_manager.unregister_advertisement(ble_uart.advert)

If you wanted to add an unpublish to the peripheral module, my assumption would be that it would be something like:

    def unpublish(self):
        """Remove peripheral and advertisement"""
        self.mainloop.quit()
        self.ad_manager.unregister_advertisement(self.advert)

Please feedback if that helps.

Also, I am happy to look at a pull request if you create something you believe would be generally applicable.