――I want to get a Bluetooth Adapter to do various things with Bluetooth
--Use Bluetooth Manager
//API Level 23 or higher
BluetoothManager bluetoothManager = Context.getSystemService(BluetoothManager.class)
//API Level 1 or higher
BluetoothManager bluetoothManager = (BluetoothManager) getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
--Use the static method of BluetoothAdapter
//Null may be returned
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
If you look at the Official Reference,
To get a BluetoothAdapter representing the local Bluetooth adapter, call the getAdapter() function on BluetoothManager. On JELLY_BEAN_MR1 and below you will need to use the static getDefaultAdapter() method instead.
Basically the first way, JELLY_BEAN_MR1 or less, that is, Android 4.2.2 or less, let's do it in the second way.
Recommended Posts