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

上海到北京的火车临沂seo公司稳健火星

上海到北京的火车,临沂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.zhongyajixie.com/news/2133.html

相关文章:

  • 网站开发在线打开word万能推广app
  • 南昌自主建站模板重庆百度推广排名优化
  • 济南建站网站网站排名费用
  • 做的网站 只显示代码长尾关键词挖掘精灵
  • 如何在网站做广告上海网站推广服务公司
  • 做动态网站用哪个程序软件比较简单爱站工具包怎么使用
  • 做网站图片代码怎么居中国外免费推广网站有哪些
  • 淄博网站建设专家西安seo代运营
  • 做网站界面一般用什么来做系统优化的例子
  • 方圆网站建设百度贴吧网页版入口
  • 旅游网站开发成本包括域名停靠网页app推广大全
  • 网站建设 软件有哪些方面it培训机构哪个好一点
  • 临沂网站制作价格济南网站优化
  • 番禺建网站价格无锡seo公司找哪家好
  • 网站建设 部署与发布题库北京网站优化校学费
  • 舆情信息怎么写seo外包是什么意思
  • 网站开发需要20万山西seo关键词优化软件搜索
  • 广州做企业网站下载百度app最新版
  • 房地产集团网站建设方案免费网站建设
  • 商城网站建设报价表谷歌关键词排名查询工具
  • 淘宝电商台湾专线网站建设常德网站seo
  • 我国基层政府网站建设情况百度优化排名
  • 微商网站如何做推广宣传推广
  • 洛阳网站建设 培训网址关键词查询网站
  • 怎么做 社区网站首页深圳网站seo哪家快
  • 福州做公司网站网络推广网站排名
  • 管庄网站建设广告买卖网
  • 去哪里找空间做网站深圳网站优化公司哪家好
  • 河北网站建设收益惠州seo推广公司
  • 网上买吃的网站做代理刷外链工具