Android 9 原生设置关于蓝牙设置(六)

蓝牙A2DP Sink连接

在这里插入图片描述
  打开蓝牙并允许蓝牙可被检测到,然后用手机连接此设备,进行A2DP Sink连接,连接时调用com_android_bluetooth_a2dp_sink.cpp bta2dp_connection_state_callback()
  -->A2dpSinkStateMachine.java onConnectionStateChanged() 发送EVENT_TYPE_CONNECTION_STATE_CHANGED广播
  -->A2dpSinkStateMachine.java Disconnected.processConnectionEvent()
  当前状态是Disconnected,新状态是CONNECTION_STATE_CONNECTING,:
在这里插入图片描述
在这里插入图片描述
  上面代码首先做一些音频相关的处理(这里不分析它),然后发送广播MSG_CONNECTION_STATE_CHANGED、切换状态到Pending。
  -->A2dpSinkStateMachine.java IntentBroadcastHandler.handleMessage()
  -->A2dpSinkStateMachine.java IntentBroadcastHandler.onConnectionStateChanged()
在这里插入图片描述
  发送广播CONNECTION_STATE_CHANGED,不过这个广播最终没有被处理(所以Setting软件界面没有更新)。最后调用Profile.notifyProfileConnectionStateChanged()。

  接着com_android_bluetooth_a2dp_sink.cpp bta2dp_audio_config_callback()被调用:
在这里插入图片描述
  此时状态是Pending:
在这里插入图片描述
  调用processAudioConfigEvent(),通知音频格式的变化。
  

  接着com_android_bluetooth_a2dp_sink.cpp bta2dp_connection_state_callback()再被调用:
在这里插入图片描述
此时状态是Pending:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
  和前面一样调用AudioManager.setBluetoothA2dpDeviceConnectionState(),不过因状态变成Connected,所以会进行一些音频切源操作。然后发送广播,Setting应用并没有处理它。最后将状态切为Connected。
在这里插入图片描述
发送广播,但原生设置代码没有对它处理。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以通过使用 Android 的 BluetoothAdapter 类提供的方法来监听蓝牙的状态。以下是一个示例代码: ``` import android.bluetooth.BluetoothAdapter; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; public class BluetoothStateListener { private BluetoothAdapter bluetoothAdapter; private BluetoothStateReceiver bluetoothStateReceiver; public BluetoothStateListener(Context context) { bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothStateReceiver = new BluetoothStateReceiver(); IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); context.registerReceiver(bluetoothStateReceiver, filter); } public void unregisterReceiver(Context context) { context.unregisterReceiver(bluetoothStateReceiver); } private class BluetoothStateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); switch (state) { case BluetoothAdapter.STATE_ON: // 蓝牙已打开 break; case BluetoothAdapter.STATE_OFF: // 蓝牙已关闭 break; } } } } } ``` 以上代码中,我们通过注册一个 BroadcastReceiver 来监听 BluetoothAdapter.ACTION_STATE_CHANGED 广播事件,当蓝牙状态发生变化时,会触发该广播事件,我们可以在 BroadcastReceiver 的 onReceive 方法中处理蓝牙状态的变化。在使用完后,记得调用 unregisterReceiver 方法来注销 BroadcastReceiver。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值