How do I fix the deprecation warning in this code? Alternatively, are there any other options for doing this?
val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (mBluetoothAdapter.isEnabled) {}
如你看到的here https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getDefaultAdapter(),他们现在推荐这个:
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.getAdapter()
原因似乎是这样的BluetoothAdapter.getDefaultAdapter()
忽略上下文,而更复杂的应用程序可能需要显式引用正确的上下文。
在我看来,这不是弃用它的充分理由,因为我无法想到蓝牙适配器需要基于上下文的现实/常见用例。他们应该保留这两个选项(基于上下文和默认)而不弃用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)