BluetoothAdapter.getDefaultAdapter() 已弃用,现在我该用什么?
What do I use now that BluetoothAdapter.getDefaultAdapter() is deprecated?
如何解决此代码中的弃用警告?或者,还有其他选择吗?
val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (mBluetoothAdapter.isEnabled) {}
如您所见here,他们现在推荐这个:
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.getAdapter()
原因似乎是 BluetoothAdapter.getDefaultAdapter()
忽略了上下文,而更复杂的应用程序可能需要明确引用正确的上下文。
在我看来,这不是弃用它的充分理由,因为我想不出 realistic/frequent BluetoothAdapter 需要基于 Context 的用例。他们应该只保留两个选项(基于上下文和默认)而不弃用。
BluetoothManager bluetoothManager = (BluetoothManager) YourContext.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
如何解决此代码中的弃用警告?或者,还有其他选择吗?
val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (mBluetoothAdapter.isEnabled) {}
如您所见here,他们现在推荐这个:
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.getAdapter()
原因似乎是 BluetoothAdapter.getDefaultAdapter()
忽略了上下文,而更复杂的应用程序可能需要明确引用正确的上下文。
在我看来,这不是弃用它的充分理由,因为我想不出 realistic/frequent BluetoothAdapter 需要基于 Context 的用例。他们应该只保留两个选项(基于上下文和默认)而不弃用。
BluetoothManager bluetoothManager = (BluetoothManager) YourContext.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();