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

android 如何判断已配对的蓝牙是否打开了互联网访问开关

最近遇到一个需求,要判断已配对的蓝牙是否打开了互联网访问的开关。 经查看源码,得出以下方法。

1. 首先要判断蓝牙是否打开
2. 已打开的蓝牙是否已配对
3. 验证是否真正打开

在这里插入图片描述

	/*** 是否打开蓝牙互联网访问*/@SuppressLint("MissingPermission")fun isOpenBleNet(context: Context, bleMac:String): Boolean {val bluetoothManager: BluetoothManager? = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?val mBtAdapter = bluetoothManager?.adapter ?: return falseval isOpen= isBleBoundByMac(mBtAdapter,bleMac)return if (isOpen) {//互联网开关是否打开val panState = mBtAdapter.getProfileConnectionState(5)Log.d("TAG", "-----设备已配对,互联网访问状态: ${panState}")panState == BluetoothProfile.STATE_CONNECTED} else {false}}/***  判断是否已连接配对的蓝牙** @param context* @param strCurBtMac* @return*/@SuppressLint("MissingPermission")fun isBleBoundByMac(mBtAdapter: BluetoothAdapter, strCurBtMac: String?):Boolean {if(!mBtAdapter.isEnabled) return falseval set: Set<BluetoothDevice> = mBtAdapter.bondedDevicesvar device: BluetoothDevice? = nullfor (dev in set) {if (dev.address.equals(strCurBtMac, true)) {device = devbreak}}if (device == null) {return false}//得到BluetoothDevice的Class对象val bluetoothDeviceClass: Class<BluetoothDevice> = BluetoothDevice::class.javatry { //得到连接状态的方法val method: Method = bluetoothDeviceClass.getDeclaredMethod("isConnected")//打开权限method.isAccessible = truereturn method.invoke(device) as Boolean} catch (e: Exception) {e.printStackTrace()}return false}
http://www.lryc.cn/news/205644.html

相关文章:

  • 在Linux上实现ECAT主站
  • Spring Cloud之服务熔断与降级(Hystrix)
  • HashMap 哈希碰撞、负载因子、插入方式、扩容倍数
  • 【Unity3D】Unity与Android交互
  • 信号去噪算法
  • GPT带我学-设计模式-10观察者模式
  • JDK - 常用的设计模式
  • 华为OD机考算法题:寻找最大价值的矿堆
  • wf-docker集群搭建(未完结)
  • uni-app 在 APP 端的版本强制更新与热更新
  • 实在智能受邀参加第14届珠中江数字化应用大会,AI赋能智能制造,共话“湾区经验”
  • Qt 窗口的尺寸
  • 游戏数据分析对于运营游戏平台的重要性
  • 微信群发消息的正确打开方式,让你的社交更高效!
  • HTML5语义化标签 header 的详解
  • SpringCloud复习:(2)@LoadBalanced注解的工作原理
  • vue钩子函数以及例子
  • redis场用命令及其Java操作
  • UG\NX二次开发 同时设置多个对象的高亮状态 UF_DISP_set_highlights
  • Qt+树莓派4B 手动设置系统日期和时间
  • 用大顶堆和小顶堆实现优先队列
  • PDCA项目开发环境搭建说明
  • Git简明教程
  • 数据结构顺序表(C语言版)
  • 新手如何备考学习PMP?
  • [卷积神经网络]FasterNet论文解析
  • 知识图谱+推荐系统 文献阅读
  • shell_39.Linux参数测试
  • 3D模型格式转换工具HOOPS Exchange助力SIMCON搭建注塑项目
  • Linux_虚拟内存机制