【问题标题】:InputManager automatically removes Wireless Controller from listInputManager 自动从列表中删除无线控制器
【发布时间】:2020-01-24 10:59:53
【问题描述】:

感谢 Android 设备上的 InputManager,我正在尝试处理 ps4 控制器。

控制器通过蓝牙连接到内置安卓界面的智能手机。在连接时,调用 InputDeviceListener 并且日志显示onInputDeviceAdded: Wireless Controller 对于以下代码来说这是正常的。但是,在 5 秒后,onInputDeviceRemoved 会因任何原因被调用!这意味着控制器已从设备列表中删除,并阻止智能手机接收任何KeyEvent

控制器似乎只是从 InputManager 列表中删除,因为它仍然与智能手机配对。

这是我的代码:

public class activity_test extends AppCompatActivity {

    private static final String TAG = activity_test.class.getSimpleName();

    private InputManager mInputManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        mInputManager = (InputManager)getSystemService(Context.INPUT_SERVICE);

        int[] ids = mInputManager.getInputDeviceIds();

        mInputManager.registerInputDeviceListener(new InputManager.InputDeviceListener() {
            @Override
            public void onInputDeviceAdded(int i) {
                Log.d(TAG, "onInputDeviceAdded: "+mInputManager.getInputDevice(i));
            }

            @Override
            public void onInputDeviceRemoved(int i) {
                Log.d(TAG, "onInputDeviceRemoved: "+mInputManager.getInputDevice(i));
            }

            @Override
            public void onInputDeviceChanged(int i) {
                Log.d(TAG, "onInputDeviceChanged: "+mInputManager.getInputDevice(i));
            }
        }, null);
    }
}

有没有办法知道是什么原因造成的并避免它?

【问题讨论】:

    标签: android joystick


    【解决方案1】:

    实际上代码有效。我用蓝牙键盘试了一下,效果很好。 看起来问题来自不支持通过蓝牙的 ps4 控制器的智能手机/Android 版本(我目前使用的是华为 p8 lite 2017,Android 8.0.0)。 所以我找到的解决方案是用 OTG 电缆连接两者。

    【讨论】:

      猜你喜欢
      • 2018-10-18
      • 1970-01-01
      • 2011-03-22
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      相关资源
      最近更新 更多