You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

蓝牙开关后pygame与DS4Windows无法识别控制器求助

问题描述

原本PS4手柄通过蓝牙连接正常,可被pygame和DS4Windows识别,但关闭并重新开启蓝牙后,手柄彻底无法被这两个工具识别。使用的交互代码如下:

import pygame
import vgamepad as vg
from time import sleep

pygame.init()
pygame.joystick.init()
gamepad = vg.VDS4Gamepad()
joystick = None

while True:
    while pygame.joystick.get_count() == 0:
        sleep(1)

    # Check if the joystick is disconnected
    if joystick is not None and not joystick.get_init():
        joystick = None

    if joystick is None:
        joystick = pygame.joystick.Joystick(0)
        joystick.init()

    pygame.event.get()

    # Check if the joystick is still valid before accessing its properties
    if joystick is not None and joystick.get_init():
        buttons = [joystick.get_button(i) for i in range(joystick.get_numbuttons())]
        axes = [joystick.get_axis(i) for i in range(joystick.get_numaxes())]
        
        print("Buttons:", buttons)
        print("Axes:", axes)

        for k, j in enumerate(buttons):
            if j:
                gamepad.press_button(vg.XUSB_BUTTON(k))
            else:
                gamepad.release_button(vg.XUSB_BUTTON(k))

        gamepad.left_joystick(x_value=int(joystick.get_axis(0) * 32767), y_value=int(joystick.get_axis(1) * 32767))
        gamepad.right_joystick(x_value=int(joystick.get_axis(2) * 32767), y_value=int(joystick.get_axis(3) * 32767))
        gamepad.left_trigger(int(joystick.get_axis(4) * 32767))
        gamepad.right_trigger(int(joystick.get_axis(5) * 32767))

        gamepad.update()

    sleep(0.005)
排查与解决步骤

系统层面排查

  • 检查系统蓝牙设备列表:打开设备管理器,查看蓝牙分类下是否有PS4手柄,若存在黄色感叹号,右键选择「卸载设备」后重新配对;若未找到,删除已配对的手柄记录,重新通过蓝牙配对
  • 重启蓝牙服务:在Windows服务中找到「Bluetooth Support Service」,右键选择「重启」;或直接重启电脑,修复蓝牙栈异常
  • 检查手柄电量:低电量会导致连接不稳定,先给手柄充电30分钟以上再尝试连接

DS4Windows专属修复

  • 以管理员权限运行DS4Windows:右键程序图标选择「以管理员身份运行」,避免权限不足无法读取设备
  • 重置DS4Windows配置:找到配置文件夹(默认路径C:\Users\[你的用户名]\AppData\Roaming\DS4Windows),备份后删除文件夹内所有文件,重新启动软件
  • 确认连接模式:在DS4Windows设置中选择「Bluetooth」模式,关闭其他独占模式选项

Pygame代码优化

原代码的手柄重连检测逻辑存在漏洞,修改后的代码可更可靠地处理断开重连场景:

import pygame
import vgamepad as vg
from time import sleep

pygame.init()
pygame.joystick.init()
gamepad = vg.VDS4Gamepad()
joystick = None

while True:
    current_count = pygame.joystick.get_count()
    # 处理手柄未连接状态
    if current_count == 0:
        if joystick is not None:
            joystick.quit()
            joystick = None
        print("等待手柄连接...")
        sleep(1)
        pygame.event.get()
        continue
    
    # 初始化或重新初始化手柄
    if joystick is None or not joystick.get_init():
        try:
            joystick = pygame.joystick.Joystick(0)
            joystick.init()
            print(f"已连接手柄: {joystick.get_name()}")
        except pygame.error:
            joystick = None
            print("初始化手柄失败,重试中...")
            sleep(0.5)
            continue
    
    # 处理pygame设备事件,避免队列阻塞
    for event in pygame.event.get():
        if event.type == pygame.JOYDEVICEADDED:
            print("检测到新手柄连接")
        elif event.type == pygame.JOYDEVICEREMOVED:
            print("手柄已断开连接")
            joystick.quit()
            joystick = None
    
    # 读取手柄数据并映射到虚拟手柄
    if joystick and joystick.get_init():
        try:
            buttons = [joystick.get_button(i) for i in range(joystick.get_numbuttons())]
            axes = [joystick.get_axis(i) for i in range(joystick.get_numaxes())]
            
            print("Buttons:", buttons)
            print("Axes:", axes)

            for k, j in enumerate(buttons):
                if j:
                    gamepad.press_button(vg.XUSB_BUTTON(k))
                else:
                    gamepad.release_button(vg.XUSB_BUTTON(k))

            gamepad.left_joystick(x_value=int(joystick.get_axis(0) * 32767), y_value=int(joystick.get_axis(1) * 32767))
            gamepad.right_joystick(x_value=int(joystick.get_axis(2) * 32767), y_value=int(joystick.get_axis(3) * 32767))
            gamepad.left_trigger(int(joystick.get_axis(4) * 32767))
            gamepad.right_trigger(int(joystick.get_axis(5) * 32767))

            gamepad.update()
        except pygame.error:
            print("读取手柄数据失败,可能已断开")
            joystick.quit()
            joystick = None
    
    sleep(0.005)

额外注意事项

  • 避免同时运行多个手柄管理工具:比如DS4Windows和Steam手柄配置工具,二者会抢占设备控制权
  • 更新蓝牙驱动:外接蓝牙适配器需更新至最新驱动;内置蓝牙则更新主板芯片组驱动

内容的提问来源于stack exchange,提问作者user23130088

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 03:42:20