Revert "Make BluetoothLe singleton"
This reverts commit db112e18f774be043532631ebf60dcc0a9048139.
Reason for revert: b/299240783
Bug: 299240783
Change-Id: I2b8f76192fe112f74fd75333ba9ff84d4f5188ad
diff --git a/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricAdvertiseTest.kt b/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricAdvertiseTest.kt
index f27a1e5..8c6a2e1 100644
--- a/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricAdvertiseTest.kt
+++ b/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricAdvertiseTest.kt
@@ -34,7 +34,7 @@
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
class RobolectricAdvertiseTest {
private val context: Context = RuntimeEnvironment.getApplication()
- private var bluetoothLe = BluetoothLe.getInstance(context)
+ private var bluetoothLe = BluetoothLe(context)
@Test
fun advertiseSuccess() = runTest {
diff --git a/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricGattClientTest.kt b/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricGattClientTest.kt
index ab35eda..ac2372c 100644
--- a/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricGattClientTest.kt
+++ b/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricGattClientTest.kt
@@ -39,7 +39,6 @@
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
-import org.junit.After
import org.junit.Assert
import org.junit.Assert.assertTrue
import org.junit.Before
@@ -99,16 +98,11 @@
@Before
fun setUp() {
- bluetoothLe = BluetoothLe.getInstance(context)
+ bluetoothLe = BluetoothLe(context)
clientAdapter = StubClientFrameworkAdapter(bluetoothLe.client.fwkAdapter)
bluetoothLe.client.fwkAdapter = clientAdapter
}
- @After
- fun tearDown() {
- bluetoothLe.client.fwkAdapter = clientAdapter.baseAdapter
- }
-
@Test
fun connectGatt() = runTest {
val device = createDevice("00:11:22:33:44:55")
@@ -372,7 +366,7 @@
}
class StubClientFrameworkAdapter(
- internal val baseAdapter: GattClient.FrameworkAdapter
+ private val baseAdapter: GattClient.FrameworkAdapter
) : GattClient.FrameworkAdapter {
var gattServices: List<FwkService> = listOf()
var callback: BluetoothGattCallback? = null
diff --git a/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricGattServerTest.kt b/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricGattServerTest.kt
index 07e8583..741377c 100644
--- a/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricGattServerTest.kt
+++ b/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricGattServerTest.kt
@@ -41,7 +41,6 @@
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
-import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
@@ -91,16 +90,11 @@
@Before
fun setUp() {
- bluetoothLe = BluetoothLe.getInstance(context)
+ bluetoothLe = BluetoothLe(context)
serverAdapter = StubServerFrameworkAdapter(bluetoothLe.server.fwkAdapter)
bluetoothLe.server.fwkAdapter = serverAdapter
}
- @After
- fun tearDown() {
- bluetoothLe.server.fwkAdapter = serverAdapter.baseAdapter
- }
-
@Test
fun openGattServer() = runTest {
val device = createDevice("00:11:22:33:44:55")
@@ -504,7 +498,7 @@
}
class StubServerFrameworkAdapter(
- val baseAdapter: GattServer.FrameworkAdapter
+ private val baseAdapter: GattServer.FrameworkAdapter
) : GattServer.FrameworkAdapter {
val shadowGattServer: ShadowBluetoothGattServer
get() = shadowOf(gattServer)
diff --git a/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricScanTest.kt b/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricScanTest.kt
index a983408..189873a 100644
--- a/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricScanTest.kt
+++ b/bluetooth/bluetooth-testing/src/test/kotlin/androidx/bluetooth/testing/RobolectricScanTest.kt
@@ -39,7 +39,7 @@
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
class RobolectricScanTest {
private val context: Context = RuntimeEnvironment.getApplication()
- private var bluetoothLe = BluetoothLe.getInstance(context)
+ private var bluetoothLe = BluetoothLe(context)
private companion object {
private const val TIMEOUT_MS: Long = 2_000
}
diff --git a/bluetooth/bluetooth/api/current.txt b/bluetooth/bluetooth/api/current.txt
index 2618c72..004a624 100644
--- a/bluetooth/bluetooth/api/current.txt
+++ b/bluetooth/bluetooth/api/current.txt
@@ -61,16 +61,11 @@
}
public final class BluetoothLe {
+ ctor public BluetoothLe(android.content.Context context);
method @RequiresPermission("android.permission.BLUETOOTH_ADVERTISE") public suspend Object? advertise(androidx.bluetooth.AdvertiseParams advertiseParams, optional kotlin.jvm.functions.Function2<? super java.lang.Integer,? super kotlin.coroutines.Continuation<? super kotlin.Unit>,?>? block, optional kotlin.coroutines.Continuation<? super kotlin.Unit>);
method @RequiresPermission("android.permission.BLUETOOTH_CONNECT") public suspend <R> Object? connectGatt(androidx.bluetooth.BluetoothDevice device, kotlin.jvm.functions.Function2<? super androidx.bluetooth.BluetoothLe.GattClientScope,? super kotlin.coroutines.Continuation<? super R>,?> block, kotlin.coroutines.Continuation<? super R>);
- method public static androidx.bluetooth.BluetoothLe getInstance(android.content.Context context);
method public suspend <R> Object? openGattServer(java.util.List<androidx.bluetooth.GattService> services, kotlin.jvm.functions.Function2<? super androidx.bluetooth.BluetoothLe.GattServerConnectScope,? super kotlin.coroutines.Continuation<? super R>,?> block, kotlin.coroutines.Continuation<? super R>);
method @RequiresPermission("android.permission.BLUETOOTH_SCAN") public kotlinx.coroutines.flow.Flow<androidx.bluetooth.ScanResult> scan(optional java.util.List<androidx.bluetooth.ScanFilter> filters);
- field public static final androidx.bluetooth.BluetoothLe.Companion Companion;
- }
-
- public static final class BluetoothLe.Companion {
- method public androidx.bluetooth.BluetoothLe getInstance(android.content.Context context);
}
public static interface BluetoothLe.GattClientScope {
diff --git a/bluetooth/bluetooth/api/restricted_current.txt b/bluetooth/bluetooth/api/restricted_current.txt
index 2618c72..004a624 100644
--- a/bluetooth/bluetooth/api/restricted_current.txt
+++ b/bluetooth/bluetooth/api/restricted_current.txt
@@ -61,16 +61,11 @@
}
public final class BluetoothLe {
+ ctor public BluetoothLe(android.content.Context context);
method @RequiresPermission("android.permission.BLUETOOTH_ADVERTISE") public suspend Object? advertise(androidx.bluetooth.AdvertiseParams advertiseParams, optional kotlin.jvm.functions.Function2<? super java.lang.Integer,? super kotlin.coroutines.Continuation<? super kotlin.Unit>,?>? block, optional kotlin.coroutines.Continuation<? super kotlin.Unit>);
method @RequiresPermission("android.permission.BLUETOOTH_CONNECT") public suspend <R> Object? connectGatt(androidx.bluetooth.BluetoothDevice device, kotlin.jvm.functions.Function2<? super androidx.bluetooth.BluetoothLe.GattClientScope,? super kotlin.coroutines.Continuation<? super R>,?> block, kotlin.coroutines.Continuation<? super R>);
- method public static androidx.bluetooth.BluetoothLe getInstance(android.content.Context context);
method public suspend <R> Object? openGattServer(java.util.List<androidx.bluetooth.GattService> services, kotlin.jvm.functions.Function2<? super androidx.bluetooth.BluetoothLe.GattServerConnectScope,? super kotlin.coroutines.Continuation<? super R>,?> block, kotlin.coroutines.Continuation<? super R>);
method @RequiresPermission("android.permission.BLUETOOTH_SCAN") public kotlinx.coroutines.flow.Flow<androidx.bluetooth.ScanResult> scan(optional java.util.List<androidx.bluetooth.ScanFilter> filters);
- field public static final androidx.bluetooth.BluetoothLe.Companion Companion;
- }
-
- public static final class BluetoothLe.Companion {
- method public androidx.bluetooth.BluetoothLe getInstance(android.content.Context context);
}
public static interface BluetoothLe.GattClientScope {
diff --git a/bluetooth/bluetooth/src/androidTest/java/androidx/bluetooth/BluetoothLeTest.kt b/bluetooth/bluetooth/src/androidTest/java/androidx/bluetooth/BluetoothLeTest.kt
index 8e1f074..822fd61 100644
--- a/bluetooth/bluetooth/src/androidTest/java/androidx/bluetooth/BluetoothLeTest.kt
+++ b/bluetooth/bluetooth/src/androidTest/java/androidx/bluetooth/BluetoothLeTest.kt
@@ -59,7 +59,7 @@
context = ApplicationProvider.getApplicationContext()
bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothAdapter = bluetoothManager.adapter
- bluetoothLe = BluetoothLe.getInstance(context)
+ bluetoothLe = BluetoothLe(context)
Assume.assumeNotNull(bluetoothAdapter)
Assume.assumeTrue(bluetoothAdapter.isEnabled)
diff --git a/bluetooth/bluetooth/src/main/java/androidx/bluetooth/BluetoothLe.kt b/bluetooth/bluetooth/src/main/java/androidx/bluetooth/BluetoothLe.kt
index 5b34edc..24d6dd1 100644
--- a/bluetooth/bluetooth/src/main/java/androidx/bluetooth/BluetoothLe.kt
+++ b/bluetooth/bluetooth/src/main/java/androidx/bluetooth/BluetoothLe.kt
@@ -49,20 +49,10 @@
* Entry point for BLE related operations. This class provides a way to perform Bluetooth LE
* operations such as scanning, advertising, and connection with a respective [BluetoothDevice].
*/
-class BluetoothLe private constructor(private val context: Context) {
+class BluetoothLe constructor(private val context: Context) {
- companion object {
+ private companion object {
private const val TAG = "BluetoothLe"
- @Volatile
- @JvmStatic
- private var instance: BluetoothLe? = null
-
- @Suppress("VisiblySynchronized")
- @JvmStatic
- fun getInstance(context: Context) =
- instance ?: synchronized(this) {
- instance ?: BluetoothLe(context.applicationContext).also { instance = it }
- }
}
@RequiresApi(34)
diff --git a/bluetooth/integration-tests/testapp/src/main/java/androidx/bluetooth/integration/testapp/ui/advertiser/AdvertiserFragment.kt b/bluetooth/integration-tests/testapp/src/main/java/androidx/bluetooth/integration/testapp/ui/advertiser/AdvertiserFragment.kt
index a8b1eb1..3e1623b 100644
--- a/bluetooth/integration-tests/testapp/src/main/java/androidx/bluetooth/integration/testapp/ui/advertiser/AdvertiserFragment.kt
+++ b/bluetooth/integration-tests/testapp/src/main/java/androidx/bluetooth/integration/testapp/ui/advertiser/AdvertiserFragment.kt
@@ -136,7 +136,7 @@
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
- bluetoothLe = BluetoothLe.getInstance(requireContext())
+ bluetoothLe = BluetoothLe(requireContext())
_binding = FragmentAdvertiserBinding.inflate(inflater, container, false)
diff --git a/bluetooth/integration-tests/testapp/src/main/java/androidx/bluetooth/integration/testapp/ui/scanner/ScannerFragment.kt b/bluetooth/integration-tests/testapp/src/main/java/androidx/bluetooth/integration/testapp/ui/scanner/ScannerFragment.kt
index b65bbde..acdb648 100644
--- a/bluetooth/integration-tests/testapp/src/main/java/androidx/bluetooth/integration/testapp/ui/scanner/ScannerFragment.kt
+++ b/bluetooth/integration-tests/testapp/src/main/java/androidx/bluetooth/integration/testapp/ui/scanner/ScannerFragment.kt
@@ -140,7 +140,7 @@
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
- bluetoothLe = BluetoothLe.getInstance(requireContext())
+ bluetoothLe = BluetoothLe(requireContext())
binding.tabLayout.addOnTabSelectedListener(onTabSelectedListener)