【问题标题】:Bluetooth Receiver response time蓝牙接收器响应时间
【发布时间】:2018-01-09 18:37:36
【问题描述】:

我有以下接收器(非常简单):

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));

权限:

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

我想做的是在BT耳机断开连接时停止音乐。它可以工作,但是响应时间不好 - 音乐从设备持续大约一秒钟,直到接收器检测到事件。我检查了 Youtube 应用程序,这种情况几乎可以立即运行,无需从设备的扬声器播放任何音乐。知道如何让广播接收器立即检测到ACTION_ACL_DISCONNECTED 吗?

【问题讨论】:

    标签: android bluetooth broadcastreceiver


    【解决方案1】:

    选项 1:尝试为您正在创建的 IntentFilter 设置更高的优先级

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

    优先级更高的过滤器应该在应用程序代码之前执行

    See the android documentation for further information.

    选项 2:如果广播接收器仍然没有立即检测到 ACTION_ACL_DISCONNECTED 并且设备仍然在短时间内播放音乐,您还可以尝试设置一个意图过滤器来监听音频输出是否发生变化(AudioManager.ACTION_AUDIO_BECOMING_NOISY )。在这里,您也可以停止设备播放音乐。

    Android documentation

    【讨论】:

    • AudioManager.ACTION_AUDIO_BECOMING_NOISY 成功了。而现在它不仅涵盖了 BT 耳机,还涵盖了所有的音频变化。谢谢!
    猜你喜欢
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多