当前位置: 首页 > news >正文

朝阳网站建设开发手工活外包加工官方网

朝阳网站建设开发,手工活外包加工官方网,网站建设硬件需求,建网站的目的#Android9 查看连接多个蓝牙耳机查看使用中的蓝牙耳机 文章目录 一、主要api:二、BluetoothA2dp 对象的获取三、获取 BluetoothDevice 对象,四、其他: Android 9.0之后,支持一台手机可以同时连接多个蓝牙设备。 但是判断那个蓝牙…

#Android9 查看连接多个蓝牙耳机查看使用中的蓝牙耳机

文章目录

    • 一、主要api:
    • 二、BluetoothA2dp 对象的获取
    • 三、获取 BluetoothDevice 对象,
    • 四、其他:

Android 9.0之后,支持一台手机可以同时连接多个蓝牙设备。

但是判断那个蓝牙设备是使用中,需要经过一些复杂判断才知道!

一、主要api:

 boolean a2dpPlaying = BluetoothA2dp.isA2dpPlaying(BluetoothDevice);

看起来不难,但是 BluetoothA2dp 和 BluetoothDevice 如何获取到?

BluetoothA2dp 获取需要连接服务,BluetoothDevice 遍历所有蓝牙即可。

二、BluetoothA2dp 对象的获取

BluetoothA2dp mA2dp;public void initA2dpAdapter() {BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (mBluetoothAdapter.isEnabled()) { //判断蓝牙是否开启//获取A2DP代理对象if (mA2dp == null) { //不能重复连接服务,否则会报错mBluetoothAdapter.getProfileProxy(getContext(), mBluetoothProfileListener, BluetoothProfile.A2DP);}} else {LogUtil.error("getA2dpAdapter error. bluetooth is not Enabled");}}//getProfileProxy并不会直接返回A2DP代理对象,而是通过mListener中回调获取。private BluetoothProfile.ServiceListener mBluetoothProfileListener = new BluetoothProfile.ServiceListener() {@Overridepublic void onServiceDisconnected(int profile) {if (profile == BluetoothProfile.A2DP) {LogUtil.error("A2dp onServiceDisconnected ");mA2dp = null;}}// getProfileProxy 到 连接成功一般需要几十毫秒!@Overridepublic void onServiceConnected(int profile, BluetoothProfile proxy) {if (profile == BluetoothProfile.A2DP) {LogUtil.error("A2dp onServiceConnected ");mA2dp = (BluetoothA2dp) proxy; //转换 ,这个就是  BluetoothA2dp 对象//不显示蓝牙界面连接耳机会出现多个连接状态的蓝牙耳机,断开非使用中的蓝牙耳机judgeIsConnectManyBluetooth();}}};

三、获取 BluetoothDevice 对象,

	//判断所有的耳机设备是否存在多个连接状态的private boolean isConnectManyBluetooth() {//未从已连接设备列表中找到断开的api,从所有的设备列表中进行判断连接状态后断开LocalBluetoothManager mBluetoothManager = LocalBluetoothManager.getInstance(context, mOnInitCallback);Collection<CachedBluetoothDevice> cachedDevices = mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy();LogUtil.debug("cachedDevices size = " + cachedDevices.size());//更新蓝牙列表UIint connectDevices = 0;for (CachedBluetoothDevice cachedDevice : cachedDevices) {String name = cachedDevice.getDevice().getName();if (cachedDevice.isConnected()) {LogUtil.debug("cachedDevices name = " + name);connectDevices++;}if (connectDevices > 1) {return true;}}return false;}//判断是否多个蓝牙连接状态,如果是就断开非使用中的蓝牙private void judgeIsConnectManyBluetooth() {if (isConnectManyBluetooth()) {//获取当前使用中的蓝牙耳机对象,断开其他耳机对象LocalBluetoothManager mBluetoothManager = LocalBluetoothManager.getInstance(context, mOnInitCallback);Collection<CachedBluetoothDevice> cachedDevices = mBluetoothManager.getCachedDeviceManager().getCachedDevicesCopy();LogUtil.debug("cachedDevices size = " + cachedDevices.size());//更新蓝牙列表UIfor (CachedBluetoothDevice cachedDevice : cachedDevices) {String name = cachedDevice.getDevice().getName();if (mA2dp != null && cachedDevice.getDevice() != null) {boolean a2dpPlaying = mA2dp.isA2dpPlaying(cachedDevice.getDevice());  //判断蓝牙是否使用中!if (a2dpPlaying) {LogUtil.debug("cachedDevices a2dpPlaying name = " + name);disconnectOtherExceptOne(name, cachedDevice.getDevice().getBluetoothClass().getMajorDeviceClass());}}}}}

LocalBluetoothManager 这个类是SettingLib 里面的类,如果不是系统源码或者系统应用是调用不到的!可以用反射!

上面是实际项目中的一段代码,其实具体获取 BluetoothDevice api只有个两句:

CachedBluetoothDevice cachedDevice;//单个蓝牙对象
BluetoothDevice  bluetoothDevice = cachedDevice.getDevice();

四、其他:

     //已连接/绑定设备列表Set<BluetoothDevice> bondedDevices = mBluetoothManager.getBluetoothAdapter().getBondedDevices();

BluetoothDevice 对象没有啥连接和断开操作的方法!只有一下配置信息。

http://www.yayakq.cn/news/21778/

相关文章:

  • 甜品网站建设方案天峨县建设局网站
  • 上海公司注册一站式企业服务网页设计对版式的要求
  • .net 快速网站开发erp系统页面
  • 男男互做网站内销机械做哪个网站好
  • 无锡建设机械网站中山 网站推广
  • h5网站案例免费手机端网站模板
  • 淘宝建设网站常见问题重庆忠县网站建设报价
  • 各网站文风杭州公司网站建设哪家好
  • 中介网站开发风景网页制作模板
  • 如何让网站收录公司名网站流量检测
  • 做网站备案必须是个人还是公司建个企业网站一年需要多少钱
  • 上海专业建站公闽侯县网站建设
  • intitle 做网站技成培训网
  • 企业企业网站建设网站开发如何共用菜单栏
  • 柳州哪家公司做网站好中电云主机怎样登入创建的网站
  • 做喷绘可以在那个网站找如何做网站服务器映射
  • 网站开发方式南昌公司注册
  • 班组安全建设 网站怎么做网站文字优化
  • 做php网站阿里云服务器flash网站模板中心
  • 免费网站怎么制作邢台123网
  • 企业网站上线一站式营销推广平台
  • 建设工程信息在什么网站发布wordpress多站点详细设置(图解)
  • 制作一个网站并上传访问WordPress防止机器注册
  • jsp网站开发需要哪些技术网络服务有哪些
  • 网站建设明细报价表企业网站的建设与实现论文
  • dw做的网站如何让文字换行我做的网站上有需要别人直接下载的东西 怎么做到这一步
  • 网站建设方案的所属行业是小说网站防盗做的好处
  • 速卖通网站怎么做推广网站 框架
  • 网站 支付宝 接口应用商城app开发下载
  • 公司网站 源码建筑公司网站模板免费下载