Skip to content

Commit

Permalink
Fix disconnectAll
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod committed Jun 13, 2023
1 parent d4a7233 commit d84c30e
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions app/src/main/java/be/mygod/pogoplusplus/SfidaManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object SfidaManager : BluetoothGattCallback() {
* This private API is also called by the game so we are fine.
*/
private val removeBond by lazy { BluetoothDevice::class.java.getDeclaredMethod("removeBond") }
private fun removeBond(device: BluetoothDevice) = removeBond.invoke(device) as Boolean

/**
* Landroid/bluetooth/BluetoothGatt;->mClientIf:I,unsupported
Expand Down Expand Up @@ -78,17 +79,15 @@ object SfidaManager : BluetoothGattCallback() {
}
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
fun disconnect(device: BluetoothDevice) {
if (bluetooth.adapter.bondedDevices?.contains(device) != false && removeBond(device) as Boolean ||
if (bluetooth.adapter.bondedDevices?.contains(device) != false && removeBond(device) ||
bluetooth.getConnectionState(device, BluetoothProfile.GATT) != BluetoothProfile.STATE_CONNECTED) return
disconnectGatt(device)
}
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
fun disconnectAll() {
val bonded by lazy { bluetooth.adapter.bondedDevices?.toSet() }
bluetooth.adapter.bondedDevices?.forEach { device -> getDeviceName(device) == null || removeBond(device) }
for (device in bluetooth.getConnectedDevices(BluetoothProfile.GATT)) {
if (getDeviceName(device) == null ||
bonded?.contains(device) != false && removeBond(device) as Boolean) continue
disconnectGatt(device)
if (getDeviceName(device) != null) disconnectGatt(device)
}
}
}

0 comments on commit d84c30e

Please sign in to comment.