Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lint error in getting Bluetooth adapter

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 ?

like image 278
My God Avatar asked Apr 22 '14 11:04

My God


1 Answers

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();
}
like image 174
Pankaj Kumar Avatar answered Nov 03 '22 20:11

Pankaj Kumar