【问题标题】:How to detect if Buetooth is connected?如何检测蓝牙是否连接?
【发布时间】:2016-10-20 17:11:23
【问题描述】:

我使用此代码在蓝牙设备连接或断开连接时收到通知,但是,它不会检查蓝牙设备是否作为音频设备连接

// ...
    IntentFilter filter1 = new IntentFilter(
            BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(
            BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter filter3 = new IntentFilter(
            BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(BTReceiver, filter1);
    this.registerReceiver(BTReceiver, filter2);
    this.registerReceiver(BTReceiver, filter3);
}

// The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver BTReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Do something if connected
            Toast.makeText(getApplicationContext(), "BT Connected",
                    Toast.LENGTH_SHORT).show();
        } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            // Do something if disconnected
            Toast.makeText(getApplicationContext(), "BT Disconnected",
                    Toast.LENGTH_SHORT).show();
        }
        // else if...
    }
};

如何检测 A2DP、btA2dp 音频设备?

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    您应该注册三个广播以跟踪蓝牙设备连接:

    // ...
    
    IntentFilter filter1 = new IntentFilter(
        BluetoothAdapter.ACTION_STATE_CHANGED);
    IntentFilter filter2 = new IntentFilter(
        BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
    IntentFilter filter3 = new IntentFilter(
        BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
    
    // ...
    
    switch (action) {
        case BluetoothAdapter.ACTION_STATE_CHANGED:
            // Bluetooth state changed (turned on/off)
            break;
        case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
            // Bluetooth connection state changed (device got connected/disconnected)
            break;
        case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
            // Bluetooth device gained/lost it's state as the media audio device
            if(intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1) == BluetoothA2dp.STATE_CONNECTED) {
                Toast.makeText(context, "A2DP device connected!", Toast.LENGTH_LONG).show();
            }
            break;
    }
    

    来自BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED的文档:

    此意图将有 3 个附加功能:

    EXTRA_STATE - 配置文件的当前状态。

    EXTRA_PREVIOUS_STATE- 配置文件的先前状态。

    EXTRA_DEVICE - 远程设备。

    EXTRA_STATEEXTRA_PREVIOUS_STATE 可以是 STATE_DISCONNECTED 中的任何一个, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING.

    要检查 A2DP 设备是否在流式传输,请注册BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED 广播。

    来自BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED的文档:

    此意图将有 3 个附加功能:

    EXTRA_STATE - 配置文件的当前状态。

    EXTRA_PREVIOUS_STATE - 配置文件的先前状态。

    EXTRA_DEVICE - 远程设备。

    EXTRA_STATEEXTRA_PREVIOUS_STATE 可以是 STATE_PLAYING 中的任何一个, STATE_NOT_PLAYING.

    【讨论】:

    • 你能邀请我聊天吗?这里很难讨论
    猜你喜欢
    • 1970-01-01
    • 2015-12-22
    • 2013-02-19
    • 2014-04-29
    • 2023-03-15
    • 2012-04-18
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多