首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android蓝牙连接a2dp蓝牙耳机

Android蓝牙连接a2dp蓝牙耳机

作者头像
fanfan
发布2019-07-11 13:52:13
2.8K0
发布2019-07-11 13:52:13
举报
文章被收录于专栏:编程思想之路编程思想之路

开发环境:

开发工具: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具体要看蓝牙产品制造商是否使用这个技术

代码介绍

代码结构如下 ![在这里插入图片描述](http://www.demodashi.com/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);

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

  /**
     * 获取到保存的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;
    }

断开设备:

/**
     * 断开当前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());
        }
    }

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

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

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

功能演示

开启蓝牙 ![在这里插入图片描述](http://www.demodashi.com/contentImages/image/20181206/qDfU05rYv27nhTf9D6s.png)

开启扫描并过滤扫描结果

在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述
在这里插入图片描述

连接成功显示

在这里插入图片描述
在这里插入图片描述

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

Demo下载地址

[Demo下载地址](http://www.demodashi.com/demo/14624.html)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月06日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 开发环境:
  • 功能实现:
  • A2dp介绍
  • 代码介绍
  • 功能演示
  • Demo下载地址
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档