【发布时间】: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);
}
}
有没有办法知道是什么原因造成的并避免它?
【问题讨论】: