Do not store redundant copy of bluetooth adapter

This commit is contained in:
Mygod
2018-12-25 16:26:18 +08:00
parent ec08802fbd
commit 2b982dd349

View File

@@ -19,19 +19,19 @@ class BluetoothTethering(context: Context) : BluetoothProfile.ServiceListener, A
}
}
private val adapter = BluetoothAdapter.getDefaultAdapter()
private var pan: BluetoothProfile? = null
/**
* Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/78d5efd/src/com/android/settings/TetherSettings.java
*/
val active: Boolean? get() {
val pan = pan ?: return null
return adapter?.state == BluetoothAdapter.STATE_ON && isTetheringOn.invoke(pan) as Boolean
return BluetoothAdapter.getDefaultAdapter()?.state == BluetoothAdapter.STATE_ON &&
isTetheringOn.invoke(pan) as Boolean
}
init {
try {
adapter?.getProfileProxy(context, this, PAN)
BluetoothAdapter.getDefaultAdapter()?.getProfileProxy(context, this, PAN)
} catch (e: SecurityException) {
Timber.w(e)
SmartSnackbar.make(e).show()
@@ -45,7 +45,7 @@ class BluetoothTethering(context: Context) : BluetoothProfile.ServiceListener, A
pan = proxy
}
override fun close() {
adapter?.closeProfileProxy(PAN, pan)
BluetoothAdapter.getDefaultAdapter()?.closeProfileProxy(PAN, pan)
pan = null
}
}