Diagnosis
That's the 'Audio System Priority' feature in action. When it detects a signal from the TV (via the HDMI ARC or optical port), it auto-switches, assuming you want to watch TV.
How to Fix It
The fix is to kill that feature. On your TV, go into the settings menu for the HDMI-CEC or BRAVIA Sync (Sony TVs) or similar control function. Turn off the setting that lets the TV control the audio system. This stops the TV from sending a wake-up signal that steals the input.
If That Didn't Work
A simpler nuclear option: physically disconnect the HDMI ARC or optical cable from the soundbar when you want a long Bluetooth listening session. No signal from TV, no auto-switching.
Heads Up
Turning this off means you'll have to manually switch the soundbar input back to TV when you want to watch something, using the remote.