In this post I will show how to use device classes from InputStick library. In this case you don't need to know any technical details about USB. I think that functionality offered by standard devices from library should be sufficient for 99% of applications. InputStick library still requires a lot of work, at this moment code is a bit of a mess so I guess there is no sense to go into too many details now. Implementation of some things may be changed any time soon. At this moment there are following device classes implemented:
- USB Mouse
- USB Keyboard
- USB Gamepad
I will use USBKeyboard class as an example.
Any class that emulates USB device using InputStick library must inherit from USBDevice class, which takes care of managing communication with InputStick, queueing messages etc. There is also USBDeviceListener interface, which allows to receive incoming USB data and callbacks informing about change of connection state, using following methods:
- public void onUSBData(byte[] data);
- public void onUSBEvent(int code);
first method is called when data is received from USB host. In case of USB keyboard, this method will be called whenever state of NumLock, CapsLock, ScrollLock LEDs changes. Second one is called whenever connection state changes. Possible states are:
- STATE_NONE - initial state (disconnected).
- STATE_CONNECTING - establishing Bluetooth connection.
- STATE_CONNECTED - Bluetooth connection established, configuration data can be now uploaded (library takes care of it).
- STATE_READY - USB host set device state to "configured", now it can be used.
Now a quick overview of USBKeyboard class. Methods inherited from USBDevice:
- public void setContext(Context context) - used to pass application context.
- public void setMac(String mac) - sets Bluetooth MAC address of InputStick device (usually loaded from applications preferences).
- public void connect() - initiates connection to InputStick.
- public void disconnect() - disconnects from InputStick.
- public int getState() - returns current state of connection.
methods specific to USBKeyboard:
- public void type(byte modifier, byte key) - presses and releases key and modifier (Alt/Crtl/Shift/Win key).
- public void type(String toType) - types content of passed String.
- public boolean isCapsLockOn() - returns current state of CapsLock.
- public boolean isNumLockOn() - returns current state of NumLock.
- public static byte isKeyCode(char c) - translates char to HID key code (codes used by USB keyboard).
Here's an example showing how all this works in Android Activity class:
When Activity is created, init() must be called to pass all necessary parameters. When user clicks "Connect" button, library will try to establish Bluetooth connection (remember that application requires permission to use Bluetooth!) and configure InputStick device. Every time connection state changes it will be reported using onUSBEvent, which can be used to enable/disable parts of user interface (for example: "Type" button). When Activity needs to type some text, "type" method should be used (it will type text and press enter key). Finally, when user clicks "Disconnect" button, application will terminate current connection.