Lint Error in Receiving Bluetooth Adapter

I am following the documentation for low energy Bluetooth devices for scanning BLE devices.

As mentioned in the document, I defined ---

BluetoothAdapter mBluetoothAdapter = null;

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

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

But I get Lint error ---

A call requires API level 18 (the current minimum is 8): android.bluetooth.BluetoothManager # getAdapter

So, I changed my code to

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Is code replacement for the above lint error?

+4
source share
1 answer

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

getSystemService (java.lang.String) BLUETOOTH_SERVICE, BluetoothManager, getAdapter(), BluetoothAdapter.

, getDefaultAdapter().


mBluetoothAdapter,

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        mBluetoothAdapter = bluetoothManager.getAdapter();
} else {
       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
+5

Source: https://habr.com/ru/post/1537498/


All Articles