Skip to content

fix(bluetooth): fix audio-headphones device type support#3006

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
add-uos:master
Feb 5, 2026
Merged

fix(bluetooth): fix audio-headphones device type support#3006
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
add-uos:master

Conversation

@add-uos
Copy link
Contributor

@add-uos add-uos commented Feb 5, 2026

Correct typo "autio-headphones" to "audio-headphones" in device type check and add corresponding icon mapping to ensure proper display and handling of audio headphone devices.

log: fix audio-headphones device type support
bug: PMS-285835

Summary by Sourcery

Fix handling of Bluetooth devices reported as audio headphones so they are recognized correctly and display the appropriate icon.

Bug Fixes:

  • Correct the audio headphones device type comparison so devices reported as "audio-headphones" are properly handled.

Enhancements:

  • Map the "audio-headphones" Bluetooth device type to a dedicated headset icon for consistent UI representation.

Correct typo "autio-headphones" to "audio-headphones" in device type
check and add corresponding icon mapping to ensure proper display
and handling of audio headphone devices.

log:  fix audio-headphones device type support
bug: PMS-285835
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 5, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Fixes handling of the 'audio-headphones' Bluetooth device type by correcting a typo in the device type check and adding an icon mapping for the device type.

Class diagram for BluetoothDevice icon mapping update

classDiagram
    class BluetoothDevice {
        +QString id
        +QString name
        +QString deviceType()
        +State state
    }

    class BluetoothWorker {
        +void connectDevice(QString deviceId, QString adapterId)
    }

    class DeviceTypeIconMap {
        +QMap~QString,QString~ deviceType2Icon
    }

    BluetoothWorker --> BluetoothDevice : uses
    DeviceTypeIconMap .. BluetoothDevice : maps deviceType

    class DeviceTypeIconEntries {
        +input-keyboard : bluetooth_keyboard
        +input-mouse : bluetooth_mouse
        +input-tablet : bluetooth_touchpad
        +audio-card : bluetooth_pheadset
        +audio-headset : bluetooth_clang
        +audio-headphones : bluetooth_headset
        +network-wireless : bluetooth_lan
        +camera-video : bluetooth_vidicon
        +printer : bluetooth_print
    }

    DeviceTypeIconMap o-- DeviceTypeIconEntries : contains entries
Loading

Flow diagram for BluetoothWorker connectDevice type check update

flowchart TD
    A[connectDevice called with deviceId and adapterId] --> B[Retrieve adapter via adapterById]
    B --> C[Find device via adapter.deviceById]
    C --> D{Device exists?}
    D -- No --> E[Proceed with connection logic]
    D -- Yes --> F{device.deviceType is audio-headset or audio-headphones?}
    F -- No --> E[Proceed with connection logic]
    F -- Yes --> G{device.state is BluetoothDevice::StateAvailable?}
    G -- Yes --> H[Return early and do not reconnect]
    G -- No --> E[Proceed with connection logic]
Loading

File-Level Changes

Change Details Files
Ensure Bluetooth connection logic correctly recognizes 'audio-headphones' device type and does not attempt to reconnect already-available devices.
  • Correct the device type string comparison from the misspelled 'autio-headphones' to 'audio-headphones' in the Bluetooth device connection check
  • Preserve the existing condition that skips connecting when the device is an audio headset/headphones and already in the available state
src/plugin-bluetooth/operation/bluetoothworker.cpp
Add icon mapping support for 'audio-headphones' device type so it displays with the appropriate UI icon.
  • Extend the deviceType2Icon mapping to include 'audio-headphones' keyed to the 'bluetooth_headset' icon
src/plugin-bluetooth/operation/bluetoothdevice.h

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Consider centralizing Bluetooth device type strings (e.g., audio-headset, audio-headphones) into shared constants or an enum to avoid future typos and keep comparisons and icon mappings in sync.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider centralizing Bluetooth device type strings (e.g., `audio-headset`, `audio-headphones`) into shared constants or an enum to avoid future typos and keep comparisons and icon mappings in sync.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码修改主要是关于蓝牙设备类型字符串的修复。以下是对代码的详细审查和改进建议:

1. 语法逻辑审查

修改内容分析

  1. bluetoothdevice.h: 在 deviceType2Icon 映射表中新增了一条映射 {"audio-headphones","bluetooth_headset"}。这允许系统识别类型为 "audio-headphones" 的设备并显示对应的图标。
  2. bluetoothworker.cpp: 修复了连接设备时的条件判断逻辑,将拼写错误的 "autio-headphones" 修正为 "audio-headphones"

结论

  • 逻辑正确。之前的代码 "autio-headphones" 是一个拼写错误,导致该类型的设备无法被正确识别。现在的修改修复了这个 Bug,使其与头文件中定义的类型字符串保持一致。

2. 代码质量审查

存在的问题

  1. 魔法字符串
    代码中大量使用硬编码的字符串(如 "audio-headset", "audio-headphones")来判断设备类型。这种方式容易导致拼写错误(正如本次 diff 所修复的问题),且难以维护。如果将来需要修改类型名称,需要在多处进行查找替换。

  2. 拼写一致性
    bluetoothdevice.h 中,映射表里存在 "audio-card""audio-headset",以及新增的 "audio-headphones"。在 bluetoothworker.cpp 中,逻辑判断只包含了 audio-headsetaudio-headphones。需要确认 audio-card 类型的设备是否也需要特殊的连接逻辑,或者这里是否遗漏了其他类型。

3. 代码性能审查

  • 性能影响:本次修改主要涉及字符串比较,对性能的影响微乎其微。QMap 的查找效率为 $O(\log n)$,对于设备类型这种数量级很小的数据来说非常快。
  • 多次调用:在 bluetoothworker.cpp 中,device->deviceType() 被调用了两次。虽然编译器可能会进行优化,但为了代码清晰和避免潜在的多次虚函数调用开销,建议缓存结果。

4. 代码安全审查

  • 类型安全:使用字符串进行比较本身不是类型安全的。如果底层的蓝牙库返回的类型字符串发生微小变化(例如大小写变化),代码就会失效。
  • 健壮性:目前的逻辑是:如果是特定类型且可用,则直接返回(不执行后续连接)。这看起来像是一个防抖动或防止重复连接的逻辑。确保这个逻辑符合业务需求(即:为什么这两种特定设备在可用状态下不能被连接?)。

改进建议

为了提高代码质量、可维护性和健壮性,建议进行以下重构:

建议 1:使用枚举代替字符串(推荐)

定义一个枚举类型来表示蓝牙设备类型,避免直接使用字符串进行比较。

// bluetoothdevice.h
enum class BluetoothDeviceType {
    Unknown,
    InputTablet,
    AudioCard,
    AudioHeadset,
    AudioHeadphones, // 新增
    NetworkWireless,
    CameraVideo,
    Printer
};

// 在类中添加一个转换函数
BluetoothDeviceType deviceType() const; 

优点

  • 编译时检查,杜绝拼写错误。
  • IDE 支持自动补全和重构。
  • 代码可读性更强。

建议 2:使用常量定义字符串(次选)

如果无法修改底层接口返回的数据类型,必须使用字符串,至少应该将它们定义为常量。

// bluetoothdevice.h
namespace BluetoothDeviceTypeStrings {
    constexpr const char* InputTablet = "input-tablet";
    constexpr const char* AudioHeadset = "audio-headset";
    constexpr const char* AudioHeadphones = "audio-headphones"; // 新增
    // ...
}

使用方式

// bluetoothworker.cpp
const QString currentType = device->deviceType();
if (device && 
   (currentType == BluetoothDeviceTypeStrings::AudioHeadset || 
    currentType == BluetoothDeviceTypeStrings::AudioHeadphones) && 
    device->state() == BluetoothDevice::StateAvailable) {
    return;
}

建议 3:优化 bluetoothworker.cpp 中的逻辑

无论是否采用上述建议,都应缓存 deviceType() 的结果,并简化判断逻辑。

// 优化后的代码片段
const BluetoothDevice *device = adapter->deviceById(deviceId);
if (!device) return;

// 缓存类型,避免多次调用
const QString devType = device->deviceType();

// 检查是否为音频设备且状态可用
const bool isAudioDevice = (devType == "audio-headset" || devType == "audio-headphones");
if (isAudioDevice && device->state() == BluetoothDevice::StateAvailable) {
    return;
}

总结

本次 diff 成功修复了一个拼写错误,是一个正确的 Bug 修复。
但从长远来看,强烈建议重构这部分代码,引入枚举或常量来管理设备类型字符串,以防止未来再次出现类似的拼写错误,并提高代码的可读性和维护性。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, mhduiy

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@add-uos
Copy link
Contributor Author

add-uos commented Feb 5, 2026

/forcemerge

@deepin-bot
Copy link

deepin-bot bot commented Feb 5, 2026

This pr force merged! (status: blocked)

@deepin-bot deepin-bot bot merged commit 25edd42 into linuxdeepin:master Feb 5, 2026
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants