Skip to content

Commit 71c7da8

Browse files
committed
fix(bluetooth): 解决多个蓝牙设备回连时,反复断开连接的情况
优化了回连的逻辑,补充了一些蓝牙设备Icon类型, 去掉了a2dp能力的检查 保留了手动连接时, 只有音频设备有冲突的情况. Log: 优化蓝牙设备回连逻辑 PMS: BUG-288237 Influence: Bluetooth
1 parent 2cbec08 commit 71c7da8

3 files changed

Lines changed: 56 additions & 62 deletions

File tree

system/bluetooth1/a2dp_workaround.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

system/bluetooth1/bluetooth.go

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -18,6 +18,7 @@ import (
1818
ofdbus "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.dbus"
1919
login1 "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.login1"
2020
"github.com/linuxdeepin/go-lib/dbusutil"
21+
"github.com/linuxdeepin/go-lib/strv"
2122
)
2223

2324
const (
@@ -38,18 +39,23 @@ const (
3839
)
3940

4041
const (
41-
devIconComputer = "computer"
42-
devIconPhone = "phone"
43-
devIconModem = "modem"
44-
devIconNetworkWireless = "network-wireless"
45-
devIconAudioCard = "audio-card"
46-
devIconCameraVideo = "camera-video"
47-
devIconCameraPhoto = "camera-photo"
48-
devIconPrinter = "printer"
49-
devIconInputGaming = "input-gaming"
50-
devIconInputKeyboard = "input-keyboard"
51-
devIconInputTablet = "input-tablet"
52-
devIconInputMouse = "input-mouse"
42+
devIconComputer = "computer"
43+
devIconPhone = "phone"
44+
devIconModem = "modem"
45+
devIconNetworkWireless = "network-wireless"
46+
devIconAudioCard = "audio-card"
47+
devIconCameraVideo = "camera-video"
48+
devIconCameraPhoto = "camera-photo"
49+
devIconPrinter = "printer"
50+
devIconInputGaming = "input-gaming"
51+
devIconInputKeyboard = "input-keyboard"
52+
devIconInputTablet = "input-tablet"
53+
devIconInputMouse = "input-mouse"
54+
devIconAudioHeadset = "audio-headset"
55+
devIconAudioHeadPhones = "audio-headphones"
56+
devIconVideoDisplay = "video-display"
57+
devIconMultimediaPlayer = "multimedia-player"
58+
devIconScanner = "scanner"
5359
)
5460

5561
const (
@@ -1013,15 +1019,11 @@ func (b *SysBluetooth) autoConnectPairedDevice(devPath dbus.ObjectPath, adapterP
10131019
return nil
10141020
}
10151021

1016-
switch device.Icon {
1017-
// 只自动连接一个这些图标的设备
1018-
case devIconAudioCard, devIconInputKeyboard, devIconInputMouse, devIconInputTablet:
1019-
connectedDevice := b.findFirstConnectedDeviceByIcon(device.Icon)
1020-
if connectedDevice != nil {
1021-
logger.Debugf("there is already a connected %v, icon: %v, do not auto connect it: %v",
1022-
connectedDevice, device.Icon, device)
1023-
return nil
1024-
}
1022+
connectedDevice := b.getConflictingConnectedDevice(device.Icon)
1023+
if connectedDevice != nil {
1024+
logger.Debugf("there is already a connected %v, icon: %v, do not auto connect it: %v",
1025+
connectedDevice, device.Icon, device)
1026+
return nil
10251027
}
10261028

10271029
logger.Debug("auto connect paired", device)
@@ -1126,20 +1128,39 @@ func (b *SysBluetooth) getConnectedDeviceByAddress(address string) *device {
11261128
return nil
11271129
}
11281130

1129-
func (b *SysBluetooth) findFirstConnectedDeviceByIcon(icon string) *device {
1131+
func (b *SysBluetooth) findFirstConnectedDeviceByIcons(icon strv.Strv) *device {
11301132
b.connectedMu.Lock()
11311133
defer b.connectedMu.Unlock()
11321134

11331135
for _, devices := range b.connectedDevices {
11341136
for _, dev := range devices {
1135-
if dev.Icon == icon {
1137+
if icon.Contains(dev.Icon) {
11361138
return dev
11371139
}
11381140
}
11391141
}
11401142
return nil
11411143
}
11421144

1145+
// getConflictingConnectedDevice 检查是否已有冲突的已连接设备
1146+
// 对于输入设备(键盘、鼠标、平板),只允许连接一个同类型设备
1147+
// 对于音频设备,只允许连接一个音频设备
1148+
// getConflictingConnectedDevice 检查是否已有冲突的已连接设备
1149+
// 对于输入设备(键盘、鼠标、平板),只允许连接一个同类型设备
1150+
// 对于音频设备,只允许连接一个音频设备
1151+
// 返回已连接的冲突设备,如果没有冲突则返回nil
1152+
func (b *SysBluetooth) getConflictingConnectedDevice(deviceIcon string) *device {
1153+
switch deviceIcon {
1154+
// 只自动连接一个这些图标的设备
1155+
case devIconInputKeyboard, devIconInputMouse, devIconInputTablet:
1156+
return b.findFirstConnectedDeviceByIcons(strv.Strv{deviceIcon})
1157+
// 这些可能都是音频设备,只连接一个
1158+
case devIconAudioCard, devIconAudioHeadset, devIconAudioHeadPhones:
1159+
return b.findFirstConnectedDeviceByIcons(strv.Strv{devIconAudioCard, devIconAudioHeadset, devIconAudioHeadPhones})
1160+
}
1161+
return nil
1162+
}
1163+
11431164
func (b *SysBluetooth) setAutoConnectFinishedStatus(adapterPath dbus.ObjectPath, status bool) {
11441165
adapter, err := b.getAdapter(adapterPath)
11451166
if err != nil {

system/bluetooth1/device.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,11 @@ func (d *device) connectProperties() {
304304
}
305305
logger.Debugf("%s Connected: %v", d, connected)
306306
d.connected = connected
307-
308-
//音频设备主动发起连接时也断开之前的音频连接
309-
if d.connected && d.Paired {
310-
go d.audioA2DPWorkaround()
307+
if strings.Contains(d.Icon, "audio") {
308+
dev := _bt.getConflictingConnectedDevice(d.Icon)
309+
if dev != nil && dev.connected && d.connected && d.Paired {
310+
dev.Disconnect()
311+
}
311312
}
312313

313314
// check if device need to be removed, if is, remove device
@@ -600,8 +601,12 @@ func (d *device) doConnect(hasNotify bool) error {
600601
}
601602
return err
602603
}
603-
604-
d.audioA2DPWorkaround()
604+
if strings.Contains(d.Icon, "audio") {
605+
dev := _bt.getConflictingConnectedDevice(d.Icon)
606+
if dev != nil && dev.connected {
607+
dev.Disconnect()
608+
}
609+
}
605610

606611
err = d.doRealConnect()
607612
if err != nil {
@@ -742,17 +747,6 @@ func (d *device) getAndResetNeedRemove() bool {
742747
return needRemove
743748
}
744749

745-
func (d *device) audioA2DPWorkaround() {
746-
// TODO: remove work code if bluez a2dp is ok
747-
// bluez do not support muti a2dp devices
748-
// disconnect a2dp device before connect
749-
for _, uuid := range d.UUIDs {
750-
if uuid == A2DP_SINK_UUID {
751-
_bt.disconnectA2DPDeviceExcept(d)
752-
}
753-
}
754-
}
755-
756750
func (d *device) Connect() error {
757751
logger.Debug(d, "call Connect()")
758752
err := d.doConnect(true)

0 commit comments

Comments
 (0)