문제

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 ?

도움이 되었습니까?

해결책

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top