google / bumble

Apache License 2.0
261 stars 76 forks source link

Ideal way to terminate gracefully #180

Open pymenow opened 1 year ago

pymenow commented 1 year ago

With the scripts running , what would be a graceful way to terminate and exit ? Here is what i am doing -

  1. Upon Keyboard Interrupt - disconnect device and then device.power_off(). Any thing else I may be missing. Didnt find a suitable example for the exit sequence yet , but any refrence to read up will help.

BTW irrespective of what reason i give for the disconnect device , the disconnect reason is always 22 .

barbibulle commented 1 year ago

Disconnecting established connections and calling power_off is a good way to gracefully terminate. There should be more examples of that in the code base, I'll try to add some. It is normal that you'd observe a reason=22 event when you disconnect: when disconnecting explicitly, you can send a reason code to the peer, and they'll see that code in their disconnection event. Then the caller gets an event for the disconnection on its end, with reason=22 (HCI_CONNECTION_TERMINATED_BY_LOCAL_HOST_ERROR), meaning that you got disconnected because you asked for it. By default, the reason you send to the peer is HCI_REMOTE_USER_TERMINATED_CONNECTION_ERROR, but you can send other reasons if you want as well.

pymenow commented 1 year ago

Thanks, one of the reasons for asking is, after terminating the script, when I scan for devices I do see the device name in the list of devices found.

barbibulle commented 1 year ago

I just checked, and indeed, the power_off() method doesn't reset the controller, so if it was advertising, it will continue to do so, so you'll see it from other devices (but any connection attempt will fail). For now, I recommend doing a reset() before shutting down if you want to avoid that, but I really should have power_off() do the reset automatically, since it doesn't make sense to power off the device yet leave the controller in an active state. PR coming shortly.

pymenow commented 1 year ago

perfect thanks ! I had included reset() , as the power_on() method calls this first .

Here is what I do for graceful exit, now. disconnect devices if connected. device.power_off() device.host.reset()