Skip to content

Commit

Permalink
Use android connected device list instead of own list
Browse files Browse the repository at this point in the history
  • Loading branch information
NiroDeveloper committed May 30, 2024
1 parent cc998cb commit 268a354
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.niro.cameraremote.bluetooth

import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothHidDevice
import android.bluetooth.BluetoothProfile
import android.util.Log
Expand All @@ -9,7 +10,8 @@ import dev.niro.cameraremote.bluetooth.helper.getNameWithState
import dev.niro.cameraremote.interfaces.IAppStateCallback
import dev.niro.cameraremote.interfaces.IConnectionStateCallback

class BluetoothServiceCallback(private val connectionStateListener: IConnectionStateCallback) : BluetoothProfile.ServiceListener {
class BluetoothServiceCallback(private val connectionStateListener: IConnectionStateCallback) :
BluetoothProfile.ServiceListener {

var hidDevice: BluetoothHidDevice? = null
private set
Expand Down Expand Up @@ -48,8 +50,6 @@ class BluetoothServiceCallback(private val connectionStateListener: IConnectionS
connectionStateListener.onConnectionStateChanged(false)
}

fun isDeviceConnected() = hidCallback?.isDeviceConnected() ?: false

private fun registerApp(registerHidDevice: BluetoothHidDevice): HidDeviceCallback {
val appStateListener = object : IAppStateCallback {
override fun onAppStateChanged(registered: Boolean) {
Expand Down Expand Up @@ -118,4 +118,16 @@ class BluetoothServiceCallback(private val connectionStateListener: IConnectionS
}
}

fun isDeviceConnected() = getConnectedDevices().isNotEmpty()

fun getConnectedDevices(): List<BluetoothDevice> {
try {
return hidDevice?.connectedDevices ?: listOf()
} catch (ex: SecurityException) {
Log.wtf(null, "Failed auto connect: $ex")
}

return listOf()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class HidDeviceCallback(
var appRegistered = false
private set

private val connectedDevices = mutableListOf<BluetoothDevice>()

fun isDeviceConnected() = connectedDevices.isNotEmpty()

override fun onAppStatusChanged(pluggedDevice: BluetoothDevice?, registered: Boolean) {
super.onAppStatusChanged(pluggedDevice, registered)

Expand All @@ -46,23 +42,6 @@ class HidDeviceCallback(
}

val connected = state == BluetoothProfile.STATE_CONNECTED
val isDeviceInConnectedList = connectedDevices.contains(device)

// Do nothing if the state does not change
// - Connected=true && already in device list
// - Connected=false && not in device list
if (connected == isDeviceInConnectedList) {
val variableInfo = "connected=$connected, isDeviceInConnectedList=$isDeviceInConnectedList"
Log.d(null, "Connection state of $device changed, but it is not relevant: $variableInfo")

return
}

if (connected) {
connectedDevices.add(device)
} else {
connectedDevices.remove(device)
}

connectionStateListener.onConnectionStateChanged(connected)
}
Expand Down

0 comments on commit 268a354

Please sign in to comment.