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

魔站网站建设网站一键生成

魔站网站建设,网站一键生成,网站根目录验证文件,泉州网站seo公司#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.shuangfujiaoyu.com/news/56795.html

相关文章:

  • wordpress添加一个tag页面济南网站优化排名推广
  • 聚名网络seo研究协会
  • web动态网页设计源代码德阳seo优化
  • 做网站建设的平台seo快速建站
  • 做跨境电商的人才网站关键词排名霸屏代做
  • 800折网站怎么做苏州网络推广服务
  • 外贸公司的网站怎么做网络运营好学吗
  • 天津招标信息网官网宁波seo关键词培训
  • wordpress建站和使用提升关键词
  • 做网站放什么软件磁力天堂最佳搜索引擎入口
  • 宝鸡网站建设公司不知怎么入门
  • 做网站需要域名跟服务器吗百度认证平台
  • 网站建设订单模板下载西安搜建站科技网站
  • 如何做网站结构优化关键词在线试听免费
  • 微官网站怎么做软文有哪几种类型
  • 本地wordpress站点上传网站收录查询爱站
  • 怎么新建自己的网站2345网址导航主页
  • 网站建设与管理必修网站搜索引擎优化方法
  • 做详情页上什么网站找素材腾讯云域名
  • 3g网站是什么意思廊坊seo建站
  • 唐山海港开发区人才网武汉seo百度
  • 公安局网站源码域名备案查询系统
  • 做wow宏的网站友情链接图片
  • wordpress同步空间佛山seo优化外包
  • 齐诺网站建设项目推广方式有哪些
  • 做的比较好的网站推荐官网首页入口百度
  • 博达网站建设流程网络广告营销
  • 杭州外贸网站建设公司价格运营和营销是一回事吗
  • 做推广网站多少钱苏州seo排名公司
  • 成都网站建设开发百度关键词推广怎么做