1

我开发了一个可以检测设备类型(智能手机普通或游戏手柄)的 APP Android。我在 HTC ONE M7 中做了一个没有连接游戏手柄的测试。但我的代码告诉我这是一个游戏手柄。

这是我的代码:

InputDevice dev = InputDevice.getDevice(deviceId);
int sources = dev.getSources();
// Verify that the device has gamepad buttons, control sticks, or both.
if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) || ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK))
{
     Log.d(TAG, "device detected : Game Pad");
}

而对于 HTC ONE M7 的 deviceID = 2,我发现 ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) 为真。这就是 HTC 被视为 GamePad 的原因。

有谁知道为什么?

4

1 回答 1

0

我找到了解决方案。

我们应该像这样检查 GamePad 的存在:(&& 而不是 ||)

InputDevice dev = InputDevice.getDevice(deviceId);
int sources = dev.getSources();
// Verify that the device has gamepad buttons, control sticks, or both.
if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) && ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK))
{
    Log.d(TAG, "device detected : Game Pad");
}

因为对于真正的 GamePad,它同时包含 InputDevice.SOURCE_GAMEPAD 和 InputDevice.SOURCE_JOYSTICK。

你可以看看这个: How do I determine what is the source of Input Device in android?

谢谢。

于 2014-10-14T09:47:08.497 回答