Android蓝牙开发中电话音频(HSP,HFP)和媒体音频(A2DP,AVRCP)到底是个什么鬼

您所在的位置:网站首页 蓝牙不能用于手机音频 Android蓝牙开发中电话音频(HSP,HFP)和媒体音频(A2DP,AVRCP)到底是个什么鬼

Android蓝牙开发中电话音频(HSP,HFP)和媒体音频(A2DP,AVRCP)到底是个什么鬼

2024-07-03 18:40:12| 来源: 网络整理| 查看: 265

Android在蓝牙开发过程中,经常需要蓝牙成功连接后,控制像音箱,蓝牙灯之类的硬件。这时候,打开手机的蓝牙设置,常会看到电话音频和媒体音频,这两个是什么鬼?

从开发Android到现在,我感觉都是懵懵懂懂的,每天和硬件工程师在一起。只能说算是能开发Android app,至于这些协议总是啃了忘,忘了啃。下面说一下,我的理解。如果有不对的地方,烦请留言或者私信告诉我。

蓝牙连接时.jpg

如上图中,一般电话音频是否支持要和嵌入式工程师确认。蓝牙芯片如果不支持电话音频,这里就不会显示出来了。 这里说一下电话音频的协议:HSP和HFP。 先看定义:

HSP(手机规格)– 提供手机(移动电话)与耳机之间通信所需的基本功能。 HFP(免提规格)– 在 HSP 的基础上增加了某些扩展功能,原来只用于从固定车载免提装置来控制移动电话。

这个网上的解释我觉得是最好理解的了。这里要重点说一下连接HSP的核心代码处理了。

public class Hfp { public static final String TAG = "Hfp"; private BluetoothAdapter mBluetoothAdapter; private BluetoothProfile hfpProfile; private static Hfp INSTANCE; private boolean enableHfp = false; private Hfp() { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); enableHfp = mBluetoothAdapter.getProfileProxy(XApplication.getInstance(), new ProfileListener(), BluetoothProfile.HEADSET); } public boolean isEnableHfp(){ return enableHfp; } public synchronized static Hfp getInstance() { if (INSTANCE == null) { INSTANCE = new Hfp(); } return INSTANCE; } public class ProfileListener implements BluetoothProfile.ServiceListener { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { if (profile == BluetoothProfile.HEADSET) { hfpProfile = proxy; } } @Override public void onServiceDisconnected(int profile) { Log.i(TAG, "onServiceDisconnected="+profile); } } public int getHfpState(){ if(mBluetoothAdapter != null){ return mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET); }else{ return -1; } } public BluetoothDevice getSysHfpConnected(){ List devices = DeviceManager.getSysPairedDevices(); for(BluetoothDevice device : devices){ if(getHfpState(device)){ return device; } } return null; } public boolean getHfpState(BluetoothDevice device){ Logg.e(TAG, "hfpProfile_-->" + hfpProfile); if(hfpProfile != null && hfpProfile.getConnectionState(device) == BluetoothProfile.STATE_CONNECTED){ return true; } return false; }

}

———————————————————————————————————— 估计没有写注释很多人还是云里雾里的。说一下具体思路: 构造函数:Hfp() 获取蓝牙适配器:(蓝牙开发中常用代码)

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //判断Hsp的状态 enableHfp = mBluetoothAdapter.getProfileProxy(XApplication.getInstance(), new ProfileListener(), BluetoothProfile.HEADSET); 注意:XApplication.getInstance() XApplication就是你的项目的application,记得绑定在一起。

getHfpState(),getSysHfpConnected(),getHfpState三个方法根据实际情况使用。 至于:public class ProfileListener implements BluetoothProfile.ServiceListener 记得看Google的api,上面都有。官方推荐的获取hsp的代理。

再说一下: A2DP,AVRCP。 找一个能看懂的解释: A2DP(高级音频传送规格)– 允许传输立体声音频信号。 (相比用于 HSP 和 HFP 的单声道加密,质量要好得多)。 AVRCP(音频/视频遥控规格)–用于从控制器(如立体声耳机)向目标设备(如装有 Media Player 的电脑)发送命令(如前跳、暂停和播放)。

而实际开发中,Android工程师要控制的就是A2DP的连接了。逻辑和获取HSP的大致差不多。 构造函数:

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mBluetoothAdapter.getProfileProxy(XApplication.getInstance(), new profileListener(), BluetoothProfile.A2DP); 获取A2DP的代理: public class profileListener implements BluetoothProfile.ServiceListener { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { Log.i(TAG, "connect to a2dp server"); a2dpProfile = proxy; } @Override public void onServiceDisconnected(int profile) { Log.i(TAG, "disconnect to a2dp server"); a2dpProfile = null; } } 判断A2DP的连接状态: public boolean getA2dpState(BluetoothDevice device){ if(a2dpProfile != null && a2dpProfile.getConnectionState(device) == BluetoothProfile.STATE_CONNECTED){ return true; } return false; }

A2DP与设备相连:

public void a2dpConnect(BluetoothDevice device){ if(a2dpProfile != null){ BluetoothA2dp a2dp = (BluetoothA2dp) a2dpProfile; Class


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭