Prevent sound from playing via Bluetooth if mute switch is active

I'm writing an app in which I have a sound being played whenever a particular action occurs.

During testing I noticed that this sounds will always be heard if my phone is connected to a Bluetooth speaker even if the mute switch is active.

Is there a way to prevent this? I feel like my app should respect the mute switch in this case. If I am not connected to the Bluetooth speaker everything works as expected.