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) が true であることがわかりました。そのため、HTC はゲームパッドと見なされます。

誰もがなぜ知っていますか?

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");
}

実際のゲームパッドの場合、InputDevice.SOURCE_GAMEPAD と InputDevice.SOURCE_JOYSTICK が同時に含まれているためです。

あなたはこれを見ることができます: Android の入力デバイスのソースを確認するにはどうすればよいですか?

ありがとう。

于 2014-10-14T09:47:08.497 に答える