android蓝牙a2dp切换歌曲广播,Android蓝牙A2DP连接实现

## 开发环境:

开发工具:Androidstudio

适配机型:honor8(Android6.0), 坚果R1(Android8.0)

开发功能:Android中蓝牙连接A2DP设备,蓝牙耳机设备

## 功能实现:

本应用提供以下功能:

第一:开启蓝牙

第二:查找过滤掉A2DP设备

第三:连接A2DP设备

第四:断开连接A2DP设备

第五:保存通过本应用连接的A2DP设备

第六:当重新启动应用时加载已连接的设备

## A2dp介绍

A2DP全名是Advanced Audio Distribution Profile 蓝牙音频传输模型协定。 A2DP是能够采用耳机内的芯片来堆栈数据,达到声音的高清晰度。然而并非支持A2DP的耳机就是蓝牙立体声耳机,立体声实现的基本要求是双声道,所以单声道的蓝牙耳机是不能实现立体声的。声音能达到44.1kHz,一般的耳机只能达到8kHz。如果手机支持蓝牙,只要装载A2DP协议,就能使用A2DP耳机了。还有消费者看到技术参数提到蓝牙V1.0 V1.1 V1.2 V2.0——这些是指蓝牙的技术版本,是指通过蓝牙传输的速度,他们是否支持A2DP具体要看蓝牙产品制造商是否使用这个技术

## 代码介绍

代码结构如下

![](/contentImages/image/20181206/mB4Q16mLDbNGEwY1dNa.png)

本应用共包含五个java文件:

Constants:常量值定义文件

Utils:工具类,包含log包装,toast包装,以及sharedpreference的包装

DeviceBean:封装BluetoothDevice, 包含device信息

DeviceListAdapter:ListView的适配器,其中有对list列表,按钮点击事件的处理

DeviceListActivity:应用中的唯一UI界面

其中readMe为说明文件

开启蓝牙代码:

向系统发送请求,开启蓝牙,该过程会请求用户同意开启蓝牙

` Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);`

接下来开启蓝牙扫描,

`btAdapter.startDiscovery();`

在扫描过程中过滤掉非A2DP的设备

`if (btClass.getMajorDeviceClass() != BluetoothClass.Device.Major.AUDIO_VIDEO) {

/**本demo只处理a2dp设备,所以只显示a2dp,过滤掉其他设备*/

break;

}`

之后点击界面的connect按钮连接对应设备

` Method method = BluetoothA2dp.class.getMethod("connect", new Class[]{BluetoothDevice.class});

method.invoke(bluetoothA2dp, device);`

在连接成功后可以断开对应设备

`Method method = BluetoothA2dp.class.getMethod("disconnect", new Class[]{BluetoothDevice.class});

method.invoke(bluetoothA2dp, device);`

当应用退出或者进程被杀死后,重新进入应用时会加载原先已连接的蓝牙设备。

```java

/**

* 获取到保存的a2dp连接

* @param context

* @return

*/

static DeviceBean fetchConnectedDevice(Context context){

DeviceBean deviceBean = null;

SharedPreferences sharedPreferences = context.getSharedPreferences(

Constants.PREF_CONNECTED_DEVICE, Context.MODE_PRIVATE);

String name = sharedPreferences.getString(Constants.PREF_DEVICE_NAME, null);

String address = sharedPreferences.getString(Constants.PREF_DEVICE_ADDRESS, null);

if (address != null) {

deviceBean = new DeviceBean();

deviceBean.setName(name == null ? address : name);

deviceBean.setAddress(address);

deviceBean.setState(BluetoothAdapter.STATE_CONNECTED);

}

return deviceBean;

}

```

断开设备:

```java

/**

* 断开当前a2dp设备

*

* @param device device

*/

private void disconnectA2dp(BluetoothDevice device) {

if (bluetoothA2dp == null || device == null) {

return;

}

try {

Method method = BluetoothA2dp.class.getMethod("disconnect", new Class[]{BluetoothDevice.class});

method.invoke(bluetoothA2dp, device);

} catch (IllegalAccessException e) {

e.printStackTrace();

Utils.logE(TAG, e.getMessage());

} catch (InvocationTargetException e) {

e.printStackTrace();

Utils.logE(TAG, e.getMessage());

} catch (NoSuchMethodException e) {

e.printStackTrace();

Utils.logE(TAG, e.getMessage());

}

}

```

具体代码参考源码,谢谢。

备注,加载已连接的蓝牙设备是只针对该应用,也就是说只加载在该应用中进行连接的设备,对于通过设置连接的设备,不做处理。

本应用只是提供一个雏形,更多功能需求还需要自己去完善

## 功能演示

开启蓝牙

![](/contentImages/image/20181206/qDfU05rYv27nhTf9D6s.png)

开启扫描并过滤扫描结果

![](/contentImages/image/20181206/pXgDHP4mRW9LXiyLrXl.png)

发起连接,在发起连接时,系统会自动为其进行配对操作

![](/contentImages/image/20181206/Mqpe3K0LcLXKS9zarn3.png)

连接成功显示

![](/contentImages/image/20181206/aaCHAlzslFlwC1jycrI.png)

在杀死应用或者重启手机,重新进入应用时依旧可以看到已连接的设备

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值