Tom11
Tom11

Reputation: 2519

Bluetooth Receiver response time

I have following receiver (very simple):

bluetoothReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
            stopMusic();
        }
    }
};

ctx.registerReceiver(bluetoothReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED));

Permission:

<uses-permission android:name="android.permission.BLUETOOTH" />

What I want to do, is to stop music when BT headphones disconnect. It works, however the response time is not good - the music continues for about a second from the device, until the event is detected by receiver. I checked Youtube app and there this scenario works almost immediately, without playing any music from the devices' speakers. Any idea how to achieve the broadcast receiver to detect the ACTION_ACL_DISCONNECTED immediately?

Upvotes: 2

Views: 189

Answers (1)

martin
martin

Reputation: 311

Option 1: Try to setup a higher priority for the IntentFilter you are creating

ItentFilter intentFilter= new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED); 
intentFilter.setPriority(999) /// highest possible priority value 
ctx.registerReceiver(bluetoothReceiver, intentFilter);

Filters with a higher-priority should be executed before the application code

See the android documentation for further information.

Option 2: If the broadcast receiver still doesn´t detect the ACTION_ACL_DISCONNECTED immediately and the device is still playing music for a short time, you could also try to setup an intentfilter that listens if the audio outputs are changing(AudioManager.ACTION_AUDIO_BECOMING_NOISY). Here you could stop the device from playing music as well.

Android documentation

Upvotes: 1

Related Questions