From 9b7fea5eeedbe29f62cfa5097a5b84cd142cccdb Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Mon, 1 Apr 2024 16:35:24 +0900 Subject: [PATCH] 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 --- .../networkstack/tethering/TetheringDependencies.java | 1 + .../android/networkstack/tethering/TetheringService.java | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Tethering/src/com/android/networkstack/tethering/TetheringDependencies.java b/Tethering/src/com/android/networkstack/tethering/TetheringDependencies.java index 9dfd225deb..3f860566d3 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetheringDependencies.java +++ b/Tethering/src/com/android/networkstack/tethering/TetheringDependencies.java @@ -156,6 +156,7 @@ public abstract class TetheringDependencies { /** * Get a reference to BluetoothAdapter to be used by tethering. */ + @Nullable public abstract BluetoothAdapter getBluetoothAdapter(); /** diff --git a/Tethering/src/com/android/networkstack/tethering/TetheringService.java b/Tethering/src/com/android/networkstack/tethering/TetheringService.java index aa73819cc3..623f5023d1 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetheringService.java +++ b/Tethering/src/com/android/networkstack/tethering/TetheringService.java @@ -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(); } }; }