Question

I am following documentation to Bluetooth Low Energy devices to scan BLE devices.

As mentioned in the doc, I defined ---

BluetoothAdapter mBluetoothAdapter = null;

final BluetoothManager bluetoothManager = 
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = bluetoothManager.getAdapter(); //Lint Error..

But I am getting a Lint error---

Call requires API level 18 (current min is 8): android.bluetooth.BluetoothManager#getAdapter

So I changed my code to--

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Is the code replacement for the above lint error ?

Was it helpful?

Solution

You can call BluetoothAdapter.getDefaultAdapter(). BluetoothManager documentation says that

Use getSystemService(java.lang.String) with BLUETOOTH_SERVICE to create an BluetoothManager, then call getAdapter() to obtain the BluetoothAdapter.

Alternatively, you can just call the static helper getDefaultAdapter().


Or you can check build version and initialize mBluetoothAdapter, like below

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        mBluetoothAdapter = bluetoothManager.getAdapter();
} else {
       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top