Obtain BluetoothAdapter through BluetoothManager

BluetoothAdapter.getDefaultAdapter is deprecated and should be replaced
with BluetoothManager.

The AttributionSource is not necessarily set properly with
BluetoothAdapter.getDefaultAdapter, and may depend on which package is
returned first by PackageManager.getPackagesForUid. This has been
causing some flaky failures in presubmit.

Fixes: 332225348
Test: atest
Change-Id: I2f066f1a90a3c6fed7885f4bbee870abf96f664d
This commit is contained in:
Remi NGUYEN VAN 2024-04-01 16:35:24 +09:00
commit 9b7fea5eee
2 changed files with 7 additions and 1 deletions

View file

@ -156,6 +156,7 @@ public abstract class TetheringDependencies {
/**
* Get a reference to BluetoothAdapter to be used by tethering.
*/
@Nullable
public abstract BluetoothAdapter getBluetoothAdapter();
/**

View file

@ -30,6 +30,7 @@ import static android.net.dhcp.IDhcpServer.STATUS_UNKNOWN_ERROR;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.net.IIntResultListener;
@ -377,7 +378,11 @@ public class TetheringService extends Service {
@Override
public BluetoothAdapter getBluetoothAdapter() {
return BluetoothAdapter.getDefaultAdapter();
final BluetoothManager btManager = getSystemService(BluetoothManager.class);
if (btManager == null) {
return null;
}
return btManager.getAdapter();
}
};
}