1.首先设置获取各种代理

     defaultAdapter = BluetoothAdapter.getDefaultAdapter();//获取A2DP代理对象defaultAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);//获取HEADSET代理对象defaultAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.HEADSET);
private BluetoothProfile.ServiceListener mListener = new BluetoothProfile.ServiceListener() {@Overridepublic void onServiceDisconnected(int profile) {if(profile == BluetoothProfile.A2DP){mBluetoothA2dp = null;}else if(profile == BluetoothProfile.HEADSET){bluetoothHeadset = null;}}@Overridepublic void onServiceConnected(int profile, BluetoothProfile proxy) {if(profile == BluetoothProfile.A2DP){mBluetoothA2dp = (BluetoothA2dp) proxy; //转换connectedBluetooth();//得到连接设备}else if(profile == BluetoothProfile.HEADSET){bluetoothHeadset = (BluetoothHeadset) proxy;}}};

2.连接一个蓝牙的时候需要断开其他的蓝牙

List<BluetoothDevice> connectedDevices = mBluetoothA2dp.getConnectedDevices();
if (connectedDevices != null && connectedDevices.size() > 0) {for (BluetoothDevice bluetoothDevice2 : connectedDevices) {BluetoothUtils.disConnectA2dp(bluetoothDevice2, mBluetoothA2dp);BluetoothUtils.disConnectHeadset(bluetoothDevice2, bluetoothHeadset);}
}
BluetoothUtils.connectA2dp(device,mBluetoothA2dp);

连接跟断开连接的方法

 /*** 连接蓝牙设备** @param device* @param mA2dp*/public static boolean connectA2dp(BluetoothDevice device, BluetoothA2dp mA2dp) {boolean state = false;if (mA2dp == null) {return false;}setPriority(device, 100, mA2dp); //设置prioritytry {//通过反射获取BluetoothA2dp中connect方法(hide的),进行连接。Method connectMethod = BluetoothA2dp.class.getMethod("connect",BluetoothDevice.class);state = (boolean) connectMethod.invoke(mA2dp, device);} catch (Exception e) {e.printStackTrace();}return state;}/*** 断开 A2dp 连接** @param device 设备* @param mA2dp*/public static boolean disConnectA2dp(BluetoothDevice device, BluetoothA2dp mA2dp) {if (mA2dp == null) {return false;}setPriority(device, 0, mA2dp);boolean connect = false;try {//通过反射获取BluetoothA2dp中connect方法(hide的),断开连接。Method connectMethod = BluetoothA2dp.class.getMethod("disconnect", BluetoothDevice.class);connect = (boolean) connectMethod.invoke(mA2dp, device);} catch (Exception e) {e.printStackTrace();}return connect;}/*** 断开 Headset 连接* @param bluetoothDevice* @param bluetoothHeadset* @return*/public static boolean disConnectHeadset(BluetoothDevice bluetoothDevice, BluetoothHeadset bluetoothHeadset) {if (bluetoothHeadset == null) {return false;}boolean result = false;setHeadsetPriority(bluetoothDevice, 0, bluetoothHeadset);try {result = ((Boolean) BluetoothHeadset.class.getMethod("disconnect", new Class[]{BluetoothDevice.class}).invoke(bluetoothHeadset, new Object[]{bluetoothDevice})).booleanValue();} catch (Exception ex) {ex.printStackTrace();}return result;}public static void setHeadsetPriority(BluetoothDevice bluetoothDevice, int i, BluetoothHeadset bluetoothHeadset) {try {BluetoothHeadset.class.getMethod("setPriority", new Class[]{BluetoothDevice.class, Integer.TYPE}).invoke(bluetoothHeadset, new Object[]{bluetoothDevice, Integer.valueOf(i)});} catch (Exception ex) {ex.printStackTrace();}}/*** 设置优先级** @param device   设备* @param priority 优先级* @param mA2dp*/public static void setPriority(BluetoothDevice device, int priority, BluetoothA2dp mA2dp) {try {//通过反射获取BluetoothA2dp中setPriority方法(hide的),设置优先级Method connectMethod = BluetoothA2dp.class.getMethod("setPriority",BluetoothDevice.class, int.class);connectMethod.invoke(mA2dp, device, priority);} catch (Exception e) {e.printStackTrace();}}

(注:发现如果只是代理了BluetoothA2dp,没有代理BluetoothHeadset ,那么也可以断开蓝牙,但是你再连接其他蓝牙音箱后,可能会出现两个音箱都是连接状态,只不过一个在播放音乐一个没有播放,重启设备后自动连接上的蓝牙音箱也并不一定是关机前正在播放的)

android连接和断开蓝牙音箱的问题相关推荐

  1. Android连接、断开蓝牙扫描枪时屏幕刷新

    场景:使用蓝牙扫描枪连接安卓设备,用于扫描条码. 问题:连接和断开扫描枪时,屏幕都会刷新,造成临时加载的数据丢失. 原因: 扫描枪连接时是将其作为物理输入设备,即物理键盘,而安卓在改变屏幕方向.弹出隐 ...

  2. 解决win10 win11 蓝牙音箱可以连接无法播放声音/蓝牙音箱无法选择使用

    这个靠谱,记录一下 1.快捷键win+r,输入regedit,打开注册表. 2.进入目录:计算机 \HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Bl ...

  3. 如何用电脑连接小爱同学蓝牙音箱

    很多朋友问我电脑连接小爱同学后,发现小爱同学没有声,但是蓝牙显示正常连接,今天我为大家出一个详细的教学. 进入小爱同学的蓝牙设置,将蓝牙可被发现打开: 然后打开电脑蓝牙,进行小爱同学蓝牙配对: 点击电 ...

  4. Android中如何实现蓝牙的配对与连接

    Android中如何实现蓝牙的配对与连接 这段时间在项目中负责做蓝牙的设置模块,蓝牙这部分不算简单,自己先是花了一些时间看系统的蓝牙设置代码,感觉有点熟了才开动的,但期间还是踩了不少坑,有些问题网上也 ...

  5. 解决Mac电脑开机无法自动连接蓝牙音箱问题!

    苹果系统的电脑无法自动连接外置的蓝牙音箱,每次都要手动选择点选蓝牙连接,然后选择声音输出为蓝牙音箱,有点小烦,具体解决方法放在下面,一分钟搞定! 1.安装蓝牙连接工具"blueutil&qu ...

  6. Android蓝牙开发系列文章-蓝牙音箱连接

    经过一段时间的折腾,我的Android Studio终于可以正常工作了,期间遇到的坑记录在了文章<创建Android Studio 3.5第一个工程遇到的坑>. 我们在<Androi ...

  7. android 断开蓝牙连接,如何在android中连接和断开扬声器蓝牙

    我尝试在android中连接speakerBluetooth(我使用UE BOOM Speaker),该设备已配对,然后我将此设备连接到我的Android手机,通过蓝牙播放音乐 . 但我失败了,这段代 ...

  8. android 蓝牙设备断开,android – 如何检查设备的蓝牙连接是否断开?

    我想知道我与设备的蓝牙连接何时断开连接.我发现这个要检查: IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CON ...

  9. android连接蓝牙控制音乐播放器播放暂停/上一曲/下一曲,且断开蓝牙暂停音乐

    客户需求要蓝牙设备可以控制音乐播放器的暂停等操作,当时只做了蓝牙的权限配置,未对这些操作做处理. 1.配置清单文件 <serviceandroid:name=".PlayerServi ...

最新文章

  1. C++ STL: 超详细 容器 deque 以及 适配器queue 和 stack 源码分析
  2. 开发自己的Data Access Application Block[下篇]
  3. 华硕笔记本电池0%充不进电_笔记本电脑电池充不进电如何解决【解决方法】
  4. 阿里CTO张建锋:云拐点已至,All in cloud;贾扬清完整title公开
  5. 15类Android通用流行框架
  6. 入门:现实世界中的推荐系统(术语、技术等)
  7. java比较map_java-比较hashMap值
  8. python文件传输socket_树莓派采用socket方式文件传输(python)
  9. 你网购遭遇钓鱼了吗?瑞星安全随身WiFi率先帮你拦截
  10. Informix日志报错:Could not do a physical-order read to fetch netxt row
  11. 安装tif虚拟打印机
  12. python中字典的几个方法介绍
  13. html5中nav标签(导航链接)的详细介绍
  14. 线性方程组解的数目判定
  15. 查看本机IP地址、测试本机与其他设备是否连通、查本机IP归属地
  16. 高考新生,天津独立院校转设:南开大学滨海学院转设并入南大本部
  17. 2020.04 总结分享
  18. 社区团购小程序+界面diy+分销+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
  19. h5/web遮罩弹窗
  20. 基于java的百度语音识别示例

热门文章

  1. 毫米、微米、英寸、目数对照表
  2. 英寸与毫米的换算依据
  3. 推荐一款快速生成海报的微信小插件
  4. 名帖63 欧阳询 楷书《九成宫醴泉铭》
  5. 无人值守系统安装--自己的系统实用篇
  6. 【转】加油站压力/真空阀(PV阀)的工作原理及安全注意事项
  7. ChatGPT账号注册,中国手机号为什么不行?
  8. WinUSB安装以及与Linux通讯
  9. python创建时间序列_python 时间序列
  10. 最常见的开源游戏引擎