经典蓝牙的配置文件攻略

来源:互联网 发布:物流线路规划软件 编辑:程序博客网 时间:2024/05/20 01:07

经典蓝牙的配置文件攻略

1. 介绍

这里讲讲经典蓝牙的规范使用,平时我们使用蓝牙电话、语音、文件传输等等都是建立在蓝牙技术联盟,一个以制定蓝牙规范,与推动蓝牙技术为宗旨的跨国组织,所建立的规范中使用的,根据谷歌的基本api文档启动蓝牙,根据不同的需求建立相应的协议连接就ok啦!

2. 源码结构 (API 22)


如果想了解具体的蓝牙源码,可以到以下标记的源码位置去一探究竟:
- 蓝牙协议所在:/packages/apps/Bluetooth/src/com/android/bluetooth/

  • 蓝牙设置所在:/packages/apps/Settings/src/com/android/settings/bluetooth
  • 电话管理所在: /packages/services/Telecomm/src/com/android/server/telecom/
    • 基本流程:settings界面发起,LocalBluetoothAdapter.java过渡,去framework的(BluetoothAdapter.java)转消息后回到packages的AdapterService.java,再走JNI来的external控制蓝牙

3. 蓝牙协议解释

  • Headset profile 提供了移动电话上的Bluetooth耳机支持。Android提供了BluetoothHeadset类,它是一个协议,用来通过IPC(interprocess communication)控制Bluetooth Headset Service。BluetoothHeadset既包含Bluetooth
    Headset profile也包含Hands-Free profile,还包括对AT命令的支持。
  • HFP (Hands-free Profile),免提模式,让蓝牙设备可以控制电话,如接听、挂断、拒接、语音拨号等,拒接、语音拨号要视蓝牙耳机及电话是否支持。
  • HDP(Health Device Profile.),蓝牙医疗设备模式,可以创建支持蓝牙的医疗设备,使用蓝牙通信的应用,例如心率监视器,血液,温度计和秤。
  • AVRCP,音频/视频远程控制配置文件,是用来听歌时暂停,上下歌曲选择的。
  • A2DP(Advanced Audio Distribution Profile),高级音频传输模式。Android提供了BluetoothA2dp类,这是一个通过IPC来控制Bluetooth A2DP的协议。
  • HID (The Human Interface Device),人机交互接口,蓝牙鼠标键盘什么的就是这个了。该协议改编自USB HID Protocol。
  • OPP (Object Push Profile),对象存储规范,最为常见的,文件的传输都是使用此协议。
  • PAN (Personal Area Network),描述了两个或更多个蓝牙设备如何构成一个即时网络,和网络有关还有串行端口功能(SPP),拨号网络功能(DUN)。
  • PBAP (Phonebook Access Profile),电话号码簿访问协议。

4. 实际操作

演示一下蓝牙电话Headset profile

  • 首先添加权限
    <manifest ... >      <uses-permission android:name="android.permission.BLUETOOTH" />      <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />      ...    </manifest>
  • 启动蓝牙
    val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()    mBluetoothAdapter.enable()
  • 设置蓝牙协议监听器,按照蓝牙协议连接连接打开或断开的时候会走该回调,成功会建立一个协议的句柄,用以控制协议所规定的功能。
    var mServiceListener: BluetoothProfile.ServiceListener = object : BluetoothProfile.ServiceListener {        override fun onServiceDisconnected(profile: Int) {            Log.d("just2lab","profile onServiceDisconnected:$profile")            if (profile == getHFPProfileIndex()) {                mBluetoothHeadset = null            }        }        override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {            Log.d("just2lab","profile onServiceConnected:$profile,$proxy")            if (profile == getHFPProfileIndex()) {                mBluetoothHeadset = proxy                L.d("proxy: $profile,mheadset:$mBluetoothHeadset")            }        }    }
  • 开始建立协议,启动协议监听器的回调。如此就可以使用mBluetoothHeadset的各种功能了
    mBluetoothAdapter.getProfileProxy(context, mServiceListener, getHFPProfileIndex())
  • 注意大多数时候回调的协议句柄可能是隐藏类,这时可以考虑反射调用了。
  • 最后别忘了关闭协议
    mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset)

5. BluetoothHeadsetClient的常用方法(反射/拷贝类)

  • connect 连接设备
  • disconnect 断开连接
  • getAudioState 获取音频连接状态
  • connectAudio 连接音频
  • disconnectAudio 断开音频
  • dial 拨号
  • acceptCall 接听
  • rejectCall 拒接
  • terminateCall 结束通话
  • sendDTMF DTMF(Dual Tone Multi Frequency),双音多频,发送给被叫号码的用户信号,就是我们经常使用的“按1,xxx;按2,xxx…”

6. 一些事件广播的监听

  • BluetoothAdapter.ACTION_STATE_CHANGED:监听蓝牙打开与否的状态
  • BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:监听蓝牙设备连接状态
  • BluetoothDevice.ACTION_PAIRING_REQUEST :监听蓝牙设备配对请求,可以设置自动配对等等
  • BluetoothDevice.ACTION_BOND_STATE_CHANGED:监听蓝牙设备绑定状态
  • “android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED” :监听蓝牙电话状态,隐藏类,使用字符串比较方便。