Android 蓝牙开发之A2DP基本功能
本文主要是Android做为Audio Source端,A2DP的基本操作:包括连接、断开连接、设置优先级、获取优先级、获取A2DP连接状态、获取A2DP连接的设备列表等功能。
1.简介
Audio Source(音频源) 音频的输入端对音频数据进行编码,发送到Sink端。 A2DP全名是Advanced Audio Distribution Profile,高质量音频数据传输的协议,其定义里了传送单声道或立体声等高质量音频(区别于蓝牙SCO链路上传输的普通语音)信息的协议和过程。A2DP的典型应用是将音乐播放器的音频数据发送到耳机或音箱。
A2DP定义了两种角色:
Audio Source(音频源) 音频的输入端对音频数据进行编码,发送到Sink端。
Audio Sink(音频接收器) 接收到音频数据后,进行解码操作还原出音频。
2.A2DP profile
要想操作A2DP相关,首先要获取A2DP代理对象,获取代理对象的方法比较简单,如下:
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if(!mBtAdapter.isEnabled()){
//弹出对话框提示用户是后打开
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, 1);
}
//获取A2DP代理对象
mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);
getProfileProxy并不会直接返回A2DP代理对象,而是通过mListener中回调获取。
private ServiceListener mListener = new ServiceListener() {
@Override
public void onServiceDisconnected(int profile) {
if(profile == BluetoothProfile.A2DP){
mA2dp = null;
}
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if(profile == BluetoothProfile.A2DP){
mA2dp = (BluetoothA2dp) proxy; //转换
}
}
};
成功会回调mListener中的onServiceConnected函数,判断proflie是否为BluetoothProfile.A2DP,转换为BluetoothA2dp对象。通过代理对象即可进行A2DP的相关操作了
3.A2DP操作
A2DP连接首先需要与蓝牙耳机进行配对,如何配对这里就不细说了。
我这里是连接到之前配对过的一个设备。设备名称为:
private final String BT_NAME = "QCY-QY7";
获取该设备,首先获取配对的蓝牙设备,然后遍历这些蓝牙设备,找出蓝牙名称符合条件的设备,就是要操作的设备,
//获取配对的蓝牙设备
Set<BluetoothDevice> bondDevice = mBtAdapter.getBondedDevices();
for(BluetoothDevice device:bondDevice){
//获取指定名称的设备
if(BT_NAME.equals(device.getName())){
mConnectDevice = device;
}
}
mConnectDevice为要操作的设备。
3.1 A2DP连接
private void connectA2dp(BluetoothDevice device){
setPriority(mConnectDevice, 100); //设置priority
try {
//通过反射获取BluetoothA2dp中connect方法(hide的),进行连接。
Method connectMethod =BluetoothA2dp.class.getMethod("connect",
BluetoothDevice.class);
connectMethod.invoke(mA2dp, device);
} catch (Exception e) {
e.printStackTrace();
}
}
BluetoothA2dp中的connect方法是hide的,不能直接访问,需要通过反射的机制获取该方法进行连接。连接成功后手机可以播放音乐,声音就会从蓝牙耳机出来。
3.2 断开连接
private void disConnectA2dp(BluetoothDevice device){
setPriority(mConnectDevice, 0);
try {
//通过反射获取BluetoothA2dp中connect方法(hide的),断开连接。
Method connectMethod =BluetoothA2dp.class.getMethod("disconnect",
BluetoothDevice.class);
connectMethod.invoke(mA2dp, device);
} catch (Exception e) {
e.printStackTrace();
}
}
BluetoothA2dp中的disconnect方法也是hide的,与connect类似.
3.3 设置优先级

设置优先级是必要的,否则可能导致连接或断开连接失败等问题。
public void setPriority(BluetoothDevice device, int priority) {
if (mA2dp == null) return;
try {//通过反射获取BluetoothA2dp中setPriority方法(hide的),设置优先级
Method connectMethod =BluetoothA2dp.class.getMethod("setPriority",
BluetoothDevice.class,int.class);
connectMethod.invoke(mA2dp, device, priority);
} catch (Exception e) {
e.printStackTrace();
}
}
3.4 获取优先级
public int getPriority(BluetoothDevice device) {
int priority = 0;
if (mA2dp == null) return priority;
try {//通过反射获取BluetoothA2dp中getPriority方法(hide的),获取优先级
Method connectMethod =BluetoothA2dp.class.getMethod("getPriority",
BluetoothDevice.class);
priority = (Integer) connectMethod.invoke(mA2dp, device);
} catch (Exception e) {
e.printStackTrace();
}
return priority;
}
3.5 获取与某设备A2DP连接状态
mA2dp.getConnectionState(device);
3.6 获取连接设备列表
//返回值类型List<BluetoothDevice>
mA2dp.getConnectedDevices();
3.7 A2DP是否正在发送音频流
//返回值类型boolean,表示设备是否在通过A2DP发送音频流。
mA2dp.isA2dpPlaying(device);
4.状态监听
通过广播接收者监听A2DP连接状态的改变,A2DP播放状态的改变。
private void initReceiver(){
//注册广播接收者监听状态改变
IntentFilter filter = new IntentFilter(BluetoothA2dp.
ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
registerReceiver(mReceiver, filter);
}
广播接收者,通过intent获取状态值。
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG,"onReceive action="+action);
//A2DP连接状态改变
if(action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)){
int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
Log.i(TAG,"connect state="+state);
}else if(action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)){
//A2DP播放状态改变
int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);
Log.i(TAG,"play state="+state);
}
}
};
连接小demo:http://download.csdn.net/detail/vnanyesheshou/9841491
转载出处:http://blog.csdn.net/vnanyesheshou/article/details/71713786
Android 蓝牙开发之A2DP基本功能的更多相关文章
- Android混合开发之WebViewJavascriptBridge实现JS与java安全交互
前言: 为了加快开发效率,目前公司一些功能使用H5开发,这里难免会用到Js与Java函数互相调用的问题,这个Android是提供了原生支持的,不过存在安全隐患,今天我们来学习一种安全方式来满足Js与j ...
- Android混合开发之WebView与Javascript交互
前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...
- Android安全开发之WebView中的地雷
Android安全开发之WebView中的地雷 0X01 About WebView 在Android开发中,经常会使用WebView来实现WEB页面的展示,在Activiry中启动自己的浏览器,或者 ...
- Android混合开发之WebView使用总结
前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...
- Android安全开发之ZIP文件目录遍历
1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在“../”的字符串,攻击者可以利用多个“../”在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原有的文件.如果被覆盖掉的文件是动态链接s ...
- Android驱动开发之Hello实例
Android驱动开发之Hello实例: 驱动部分 modified: kernel/arch/arm/configs/msm8909-1gb_w100_hd720p-perf_defconf ...
- android软件开发之webView.addJavascriptInterface循环渐进【二】
本篇文章由:http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gra ...
- android软件开发之webView.addJavascriptInterface循环渐进【一】
本篇文章由:http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gra ...
- Android NDK开发之C调用Java及原生代码断点调试(二)
上一篇中,我们主要学习了Java调用本地方法,并列举了两大特殊实例来例证我们的论据,还没学习的伙伴必须先去阅读下,本次的学习是直接在上一篇的基础上进行了.点击:Android NDK开发之从Java与 ...
随机推荐
- Python函数式编程——map()、reduce()
文章来源:http://www.pythoner.com/46.html 提起map和reduce想必大家并不陌生,Google公司2003年提出了一个名为MapReduce的编程模型[1],用于处理 ...
- python调试工具----pycharm快捷键及一些常用设置
pycharm快捷键及一些常用设置 Alt+Enter 自动添加包Ctrl+t SVN更新Ctrl+k SVN提交Ctrl + / 注释(取消注释)选择的行Ctrl+Shift+F 高级查找Ctrl+ ...
- Flask实战第42天:注册页面对接短信接口及接口加密
我们来看下之前写的 sms_captcha函数 @bp.route('/sms_captcha/') def sms_captcha(): params = {'code':'abcd'} resul ...
- java 日期validate
public static boolean isValidDate(String str) { boolean convertSuccess=true; // 指定日期格式为四位年/两位月份/两位日期 ...
- WebService数据示例
通过webservice提交xml数据以及soap协议的使用 上次已经给大家分享了简单的webservice的使用,提交给服务器的数据只是简单类型的数据,这次呢换成了xml,并通过一个小例子来讲解so ...
- Android程序设计
Android程序设计-1 要求安装 Android Stuidio 完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号,提交代码运行截图和码云Git链 ...
- python3-开发进阶Django-CBV和FBV及CBV的源码分析
一.CBV和FBV 全称应该是class base views 和function base views理解起来应该就是基于类的视图函数和基于函数的视图函数 FBV 应该是我目前最常用的一种方式了,就 ...
- Hiho : 欧拉路径
欧拉路径 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其实是一块一块骨牌. 主角继续往前走,面 ...
- Nginx 重定向 伪静态 rewrite index.php
参考https://www.kancloud.cn/manual/thinkphp5/177576 thinkphp入口文件同目录下添加.把下面的内容保存为.htaccess文件 <IfModu ...
- GG同步到sqlserver报错一例 Invalid date format
在将Oracle表同步到sqlserver时,在sqlserver端应用数据时,可能会遇到这个报错. 2014-05-17 17:20:24 WARNING OGG-01154 SQL error - ...