gpt4 book ai didi

android - 现在不推荐使用 BluetoothAdapter.getDefaultAdapter() 我该使用什么?

转载 作者:行者123 更新时间:2023-12-04 23:36:16 31 4
gpt4 key购买 nike

如何修复此代码中的弃用警告?或者,还有其他选择吗?
enter image description here

   val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (mBluetoothAdapter.isEnabled) {}

最佳答案

如您所见here ,他们现在推荐这个:

val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.getAdapter()
原因似乎是 BluetoothAdapter.getDefaultAdapter()忽略上下文,而更复杂的应用程序可能需要显式引用正确的上下文。
在我看来,这不是弃用它的好理由,因为我想不出蓝牙适配器需要基于上下文的现实/频繁用例。他们应该只保留两个选项(基于上下文和默认)而不弃用。

关于android - 现在不推荐使用 BluetoothAdapter.getDefaultAdapter() 我该使用什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69122978/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com