This has to be the most annoying thing in MacOS. My laptop, soundly sleeping in my backpack, takes over my bluetooth headphones all the time.
I hate that this isn't configurable by default, but bluesnooze solved this for me: https://github.com/odlp/bluesnooze