Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually check the paired state instead of assuming it for bluetooth connection sensor #2738

Merged
merged 5 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class BluetoothSensorManager : SensorManager {

if (checkPermission(context, bluetoothConnection.id)) {

val bluetoothDevices = BluetoothUtils.getBluetoothDevices(context, true)
val bluetoothDevices = BluetoothUtils.getBluetoothDevices(context)
pairedDevices = bluetoothDevices.filter { b -> b.paired }.map { checkNameAddress(it) }
connectedPairedDevices = bluetoothDevices.filter { b -> b.paired && b.connected }.map { checkNameAddress(it) }
connectedNotPairedDevices = bluetoothDevices.filter { b -> !b.paired && b.connected }.map { checkNameAddress(it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.homeassistant.companion.android.common.bluetooth

import android.annotation.SuppressLint
import android.bluetooth.BluetoothManager
import android.bluetooth.BluetoothProfile
import android.content.Context
import androidx.core.content.getSystemService
import java.lang.reflect.Method

object BluetoothUtils {
fun getBluetoothDevices(context: Context, allowDuplicates: Boolean = false): List<BluetoothDevice> {
@SuppressLint("MissingPermission")
fun getBluetoothDevices(context: Context): List<BluetoothDevice> {
val devices: MutableList<BluetoothDevice> = ArrayList()

val bluetoothManager =
Expand All @@ -26,20 +28,20 @@ object BluetoothUtils {
BluetoothDevice(
btDev.address,
name,
true,
btDev.bondState == android.bluetooth.BluetoothDevice.BOND_BONDED,
isConnected(btDev)
)
)
}
val btConnectedDevices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)
for (btDev in btConnectedDevices) {
if (!allowDuplicates && devices.any { it.address == btDev.address }) continue
if (devices.any { it.address == btDev.address }) continue
val name = btDev.name ?: btDev.address
devices.add(
BluetoothDevice(
btDev.address,
name,
false,
btDev.bondState == android.bluetooth.BluetoothDevice.BOND_BONDED,
isConnected(btDev)
)
)
Expand Down