Fix turning off bluetooth tethering
This commit is contained in:
@@ -67,27 +67,11 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
pendingCallback = null
|
pendingCallback = null
|
||||||
app.unregisterReceiver(this)
|
app.unregisterReceiver(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* https://android.googlesource.com/platform/packages/apps/Settings/+/b1af85d/src/com/android/settings/TetherSettings.java#384
|
|
||||||
*/
|
|
||||||
@RequiresApi(24)
|
|
||||||
fun start(callback: TetheringManager.StartTetheringCallback) {
|
|
||||||
if (pendingCallback != null) return
|
|
||||||
try {
|
|
||||||
if (adapter?.state == BluetoothAdapter.STATE_OFF) {
|
|
||||||
registerBluetoothStateListener(this)
|
|
||||||
pendingCallback = callback
|
|
||||||
adapter.enable()
|
|
||||||
} else TetheringManager.startTethering(TetheringManager.TETHERING_BLUETOOTH, true, callback)
|
|
||||||
} catch (e: SecurityException) {
|
|
||||||
SmartSnackbar.make(e.readableMessage).shortToast().show()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private var connected = false
|
private var connected = false
|
||||||
private var pan: BluetoothProfile? = null
|
private var pan: BluetoothProfile? = null
|
||||||
|
private var stoppedByUser = false
|
||||||
var activeFailureCause: Throwable? = null
|
var activeFailureCause: Throwable? = null
|
||||||
/**
|
/**
|
||||||
* Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/78d5efd/src/com/android/settings/TetherSettings.java
|
* Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/78d5efd/src/com/android/settings/TetherSettings.java
|
||||||
@@ -96,7 +80,7 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
val pan = pan ?: return null
|
val pan = pan ?: return null
|
||||||
if (!connected) return null
|
if (!connected) return null
|
||||||
activeFailureCause = null
|
activeFailureCause = null
|
||||||
return adapter?.state == BluetoothAdapter.STATE_ON && try {
|
val on = adapter?.state == BluetoothAdapter.STATE_ON && try {
|
||||||
pan.isTetheringOn
|
pan.isTetheringOn
|
||||||
} catch (e: InvocationTargetException) {
|
} catch (e: InvocationTargetException) {
|
||||||
activeFailureCause = e.cause ?: e
|
activeFailureCause = e.cause ?: e
|
||||||
@@ -104,6 +88,10 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
else Timber.w(e)
|
else Timber.w(e)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
return if (stoppedByUser) {
|
||||||
|
if (!on) stoppedByUser = false
|
||||||
|
false
|
||||||
|
} else on
|
||||||
}
|
}
|
||||||
|
|
||||||
private val receiver = broadcastReceiver { _, _ -> stateListener() }
|
private val receiver = broadcastReceiver { _, _ -> stateListener() }
|
||||||
@@ -125,11 +113,35 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
|
|
||||||
override fun onServiceDisconnected(profile: Int) {
|
override fun onServiceDisconnected(profile: Int) {
|
||||||
connected = false
|
connected = false
|
||||||
|
stoppedByUser = false
|
||||||
}
|
}
|
||||||
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
|
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
|
||||||
connected = true
|
connected = true
|
||||||
stateListener()
|
stateListener()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://android.googlesource.com/platform/packages/apps/Settings/+/b1af85d/src/com/android/settings/TetherSettings.java#384
|
||||||
|
*/
|
||||||
|
@RequiresApi(24)
|
||||||
|
fun start(callback: TetheringManager.StartTetheringCallback) {
|
||||||
|
if (pendingCallback != null) return
|
||||||
|
try {
|
||||||
|
if (adapter?.state == BluetoothAdapter.STATE_OFF) {
|
||||||
|
registerBluetoothStateListener(BluetoothTethering)
|
||||||
|
pendingCallback = callback
|
||||||
|
adapter.enable()
|
||||||
|
} else TetheringManager.startTethering(TetheringManager.TETHERING_BLUETOOTH, true, callback)
|
||||||
|
} catch (e: SecurityException) {
|
||||||
|
SmartSnackbar.make(e.readableMessage).shortToast().show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@RequiresApi(24)
|
||||||
|
fun stop(callback: (Exception) -> Unit) {
|
||||||
|
TetheringManager.stopTethering(TetheringManager.TETHERING_BLUETOOTH, callback)
|
||||||
|
stoppedByUser = true
|
||||||
|
}
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
app.unregisterReceiver(receiver)
|
app.unregisterReceiver(receiver)
|
||||||
pan?.closePan()
|
pan?.closePan()
|
||||||
|
|||||||
@@ -319,10 +319,9 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
|
|||||||
if (tethering.active == null) tethering.activeFailureCause?.readableMessage else null,
|
if (tethering.active == null) tethering.activeFailureCause?.readableMessage else null,
|
||||||
baseError).joinToString("\n")
|
baseError).joinToString("\n")
|
||||||
|
|
||||||
override fun start() = BluetoothTethering.start(this)
|
override fun start() = tethering.start(this)
|
||||||
override fun stop() {
|
override fun stop() {
|
||||||
TetheringManager.stopTethering(TetheringManager.TETHERING_BLUETOOTH, this::onException)
|
tethering.stop(this::onException)
|
||||||
Thread.sleep(1) // give others a room to breathe
|
|
||||||
onTetheringStarted() // force flush state
|
onTetheringStarted() // force flush state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,10 +151,9 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin
|
|||||||
override val labelString get() = R.string.tethering_manage_bluetooth
|
override val labelString get() = R.string.tethering_manage_bluetooth
|
||||||
override val tetherType get() = TetherType.BLUETOOTH
|
override val tetherType get() = TetherType.BLUETOOTH
|
||||||
|
|
||||||
override fun start() = BluetoothTethering.start(this)
|
override fun start() = tethering!!.start(this)
|
||||||
override fun stop() {
|
override fun stop() {
|
||||||
TetheringManager.stopTethering(TetheringManager.TETHERING_BLUETOOTH, this::onException)
|
tethering!!.stop(this::onException)
|
||||||
Thread.sleep(1) // give others a room to breathe
|
|
||||||
onTetheringStarted() // force flush state
|
onTetheringStarted() // force flush state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user