1

I am unable to fix this error on my flutter app.

    > Task :permission_handler:compileDebugJavaWithJavac FAILED
/Users/sumeetpujari/Developer/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-6.1.3/android/src/main/java/com/baseflow/permissionhandler/ServiceManager.java:154: warning: [deprecation] getDefaultAdapter() in BluetoothAdapter has been deprecated
        final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                                                                  ^
error: warnings found and -Werror specified
1 error
1 warning
warnings found and -Werror specified

The code :

    @SuppressLint("MissingPermission")
private boolean isBluetoothServiceEnabled() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return bluetoothAdapter.isEnabled();
Sumeet Pujari
  • 181
  • 1
  • 5
  • 19

2 Answers2

1

There is a workaround you need to check the SDK version and based on it make changes in the app.

if(Build.VERSION.SDK_INT >= 31) {
 bluetoothAdapter = BluetoothAdapter.getAdapter();
}else{
  bluetoothManager = BluetoothManager.getDefaultAdapter();
}

Try something like this

Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
  • https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#getDefaultAdapter() – Ankit Tale Nov 09 '21 at 07:47
  • 3
    there is no `BluetoothAdapter.getAdapter()` method available on any Android version, you probably meant to use `BluetoothManager.getAdapter()` – snachmsm Nov 09 '21 at 08:42
  • how can I solve this problem? I can't find any solution for this deprecated case – Triple K Feb 03 '22 at 08:43
1

To get the default BluetoothAdapter you have to get it from the BluetoothManager system service. The sample below is from SomeActivity. In this case this refers to an instance of an activity.

BluetoothManager bluetoothManager = (BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE);
if (bluetoothManager == null) {
  /* Error handling */
}

BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
  /* Error handling */
}

I have tested it with Android API 26 to API31.

dniHze
  • 2,162
  • 16
  • 22