正如您可以在这里看到的,他们现在推荐使用:
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.getAdapter()
原因似乎是BluetoothAdapter.getDefaultAdapter()
忽略了Context,而更复杂的应用程序可能需要显式引用正确的Context。
在我看来,这不是废弃它的好理由,因为我想不出一个现实/频繁的用例需要基于Context的BluetoothAdapter。他们应该保留两个选项(基于Context和默认)而不进行废弃。
BluetoothManager bluetoothManager = (BluetoothManager) YourContext.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();