android - 现在不推荐使用 BluetoothAdapter.getDefaultAdapter() 我该使用什么?
问题描述
val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (mBluetoothAdapter.isEnabled) {}
解决方案
正如你在这里看到的,他们现在推荐这个:
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.getAdapter()
原因似乎是BluetoothAdapter.getDefaultAdapter()
忽略了上下文,而更复杂的应用程序可能需要显式引用正确的上下文。
在我看来,这不是弃用它的好理由,因为我想不出蓝牙适配器需要基于上下文的现实/频繁用例。他们应该只保留两个选项(基于上下文和默认)而不弃用。
推荐阅读
- .net - SF windows 上的 Linux docker 镜像
- javascript - 带有零且只有零的数字上的 toString().length 始终返回 0
- java - Java SQLite - 无法访问可执行 jar 中的 .db 文件
- sql - 如何在 SQL 中将日期格式匹配到 yyyymmdd
- google-apps-script - 在appsscript中创建logourl图片链接
- android - 如何更改带有滑动抽屉的 ActionBar 的颜色?
- mysql - 如何增加或减少另一个表的值?
- php - 安装 CakePHP 时,您的系统缺少请求的 PHP 扩展 intl
- multithreading - 线程在关联时停止,但不拥有,winform 被隐藏
- xml - 使用 XSD 检查 XML