Android Read USB Gamepad - android

I'm trying to read buttons pressed on a twin USB gamepad in android.
I tried all the methods available in USB package of android including bulkTransfer, controlTransfer, requestWait. but all of them always returns the same thing while pressing the buttons. I even can't get the real name of the device.
I installed USB\BT JoyCenter and it detects the key pressed and also show the name of the gamepad connected. I really can't figure out how to read the gamepad and android documentation doesn't fully cover how to do this.

I found out where the problem is. Actually I print out the byte array directly. I think java print byte memory location instead of it's content. I used a bytes to hex converter and it works fine now.

The first thing you might want to do is plug the device into a linux box and type lsusb. You need to find out what USB Classes are supported by the USB device. Post your answer here for others to see.
Then you need to determine if Android actually supports this class directly. If it does then things should be relatively easy (so read the manual). If not, you may need to use libusb or similar to do the actual low-level interactions. This would not be an easy path, but if the USB class is supported by libusb you can get there.
Once you know the class you are working with, you may find from the standard docos that you need to turn something "on" before it will start sending the data that you are interested in.

Related

How do I print programatically on android without the prompt where you have to choose a printer?

I've been trying to find a solution to this issue all day but I haven't come across anything and android has no documentation related to this. Normally when you print something programmatically following the instructions within the documentation, a screen will pop up on the device where you have to select a printer you want to use and which pages you want to print. Seems like this is a built-in feature of the print manager class.
I'm trying to find a solution that lets me directly print on the default connected printer without this screen popping up.
So I figured out a way around this issue. The printer I was using is a standard ESC/POS printer and it supports Bluetooth, so I looked up documentation on this and I just had to directly write the commands and text it needs as byte arrays into the Bluetooth output stream and that way it can print without any user interaction on the device. I assume this is also possible using cable or network connection too as long as you write into the necessary output streams.

Android how to get original data from bluetooth hid device?

I want to get the data from hid BlueTooth device (as a mouse), app get this data directly. I have search many document, it seems I can't get the original data except the phone is rooted, I want to know if there is a good solution that I can use. Thanks
Most likely there is no good solution for this case. Mouse and keyboard are used as input devices by Android itself.

Simulate USB/Bluetooth HID device locally in order to inject events?

Is it possible to simulate a USB or Bluetooth device connected to Android?
I would like to make an app which is able to simulate an HID device locally. What I mean by this is: the app should make Android believe that an USB/Bluetooth HID device is connected. This would allow my app to inject touch events globally, I hope. Is there anyway to simulate a (virtual) device? Note that I don't really care about the device, I just want to use the built-in support for HID input.
I know a lot of people already asked about touch event injections but this approach seems to be a little bit different. I do not want to use this for testing purpose, so InstrumentationTestCase and the like won't help.
Rooting the device might be an option, although I can also imagine to ask my endusers to install a specifically signed app manually (according to https://stackoverflow.com/a/16737083/2923406 and https://stackoverflow.com/a/22902808/2923406 this works, but it would be device specific).
I am aware of:
Simulate a mouse input on android - This guy seems to do it in his own app, although his questions wasn't answered ;)
http://www.pocketmagic.net/2013/01/programmatically-injecting-events-on-android-part-2/#.U58SqfldVHV - Needs Root. Did anyone succeeded in using this? Also, the solution seems very device-specific again.
Bluetooth + simulating mouse - That's not what I want. The phone itself should not be an HID device, but use the (virtually created) one itself. Same for this:
https://stackoverflow.com/a/8174973/2923406
Note that I do not want to turn my phone into an HID device of any kind.
Simulating is a broad term. When I created my Bluetooth app I "simulated" a Bluetooth connection in two ways.
Way 1: Use a serial port UART converter and hook it up to a Bluetooth module transmitter.
Then you can use a terminal program like CoolTerm, to send your data.
Like so. In this case I coded in a string to send on successful connection with the device however you can make a infinite loop for testing purposes effectively not requiring your phone to be turned into a HID device.
Way 2 (not easy): Use your computers bluetooth in a server/client relashionship model.
This is harder to do. What you can do is convert your pc/mac into a server and the Android phone into a client or vice-versa. For this you will need to write external code which will need to be compiled separately on a jvm(java virtual machine). The procedure to do this can be found here. If you are using a Linux machine you have to separately download the Bluez module. I have not tried this on any other operating sytem other than Ubuntu, and it was a pain to get functional.
Hopefully that helped.
Yes, it's quite easy using the AOA2 protocol check this & this links for details ( you'll need to switch your device to the Accessory mode )
This may be possible (or at least be easier) using the Robolectric library, which simulates a full Android device locally. Although it is intended primarily for testing, the fact that it simulates a whole device locally - including Bluetooth and USB - means adding to it may be an easier approach.
In other words, you may be able to modify the classes it uses to simulate these abilities locally (i.e. in the IDE itself without an emulator or device) in order to simulate them on the device itself. After all, it does provide full simulations of these functions. You could simply change these Bluetooth and USB simulating classes to load onto the device itself rather than onto the local Robolectric test "device."
This is just an idea though - I can't confirm this will work - it just might be a good place for you to start.

Converting iOS External Accessory Framework code to Android

We were given a bluetooth device that we were asked to connect both iOS and Android devices to a particular custom peripheral.
The demo app seems to bring up a list of devices, asking which to use, then brings up a list of protocols, which includes one defined in the info.plist under "Supported external accessory protocols" as com.(company).bt.
I looked all over and found nothing about this process being standard with Bluetooth, and the entire workflow appears to be different on Android. I've tried using the bluetooth sample app to connect to this device but the red connected LED never lights up, and depending on the UUID given, I either receive 1 byte or 7. The sample code I was given doesn't even touch the InputStream unless the stream has reached 16 bytes (Which of course, never happens)
Is there some sort of guide on how to connect to this device? Based on what I read from the code, a message isn't even sent until the user presses a button, but the LED is lit long before this is even an option in iOS.
Edit: I finally found something from clicking around - it seems that the plist entry has to do with mfi (Which I guess the documentation is only available to those in the program?) With that said, does that mean this device will only work with iOS devices?
As you mentioned connection in Android and iOS is done differently.
The protocol defined in info.plist should be the protocol you are meant to use with the particular Bluetooth chip you have.
There is a guide explaining how to connect in iOS, you should probably also check which Bluetooth version it is. If it is lower than Bluetooth 4.0 (BLE- Bluetooth Low Energy) or not. I assume it isn't BLE since you mentioned it is MFI...
1)If it is BLE use Core Bluetooth
2)Otherwise have a look at the following link: Introduction to Stream Programming Guide for Cocoa.
It is a bit hard to understand what you mean about the red LED because we don't know what device you are using. Considering you are receiving some bytes from the device it means that the communication was established. You can start debugging and perhaps understand better the code by changing it to "touch" the InputStream even if 1 byte is received...
Hope this helps.
Cheers!
EDIT: Just read the title again, I am not sure what you mean by saying you want to convert the code to Android? You only described your attempt to connect in iOS.
A quick Google search would give you the following links for Android guide:
1) Bluetooth (not BLE)
2) BLE

Android and Guitar Hero controller

I know that the latest versions of Android (Honeycomb and ICS) have support for joysticks and gamepads.
Guitar Hero (and Garage Band) controllers are essentially USB HID devices, right?
So my question:
Is that possible to receive data (button clicks) from the Guitar Hero (or Rock Band) controllers on Android device?
Would the Android understand it as a gamepad input?
P.S. all I need is to detect in my game the input from those five buttons on the plastic guitar fret.
A good starting point would be to review the linux source code for Frets On Fire, which supports some of the Guitar Hero controllers.
Frets on Fire: SourceForge
SVN: https://fretsonfire.svn.sourceforge.net/svnroot/fretsonfire
It looks like it would be difficult to universally support all controllers, from different platforms. Each console has it's own protocol, but it does look like JoyStick to keyboard emulation is possible on the PC with the PS3 controller. There is a config file for the PS3 controller on the second link that may be helpful, it's for JoyToKey (which isn't open source), but some of the values in the config may help you.
Hey this is a really cool idea. start here:
http://developer.android.com/guide/topics/usb/host.html
Then, check out this sample:
http://developer.android.com/resources/samples/USB/MissileLauncher/index.html
in that sample, there is a method named setDevice(UsbDevice device)
If I were implementing this, I would start with a duplicate of the MissileLauncher project, and modify this setDevice method. I would log everything I could possible find about the UsbDevice device and try experimenting with these conditionals in the setDevice method:
if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
...
if (intf.getEndpointCount() != 1) {
While the MissileLauncher uses this type and this endpointCount, it is very likely the garageband controller will have different values
ALSO
check out the run method to see an example of back and forth communication
DISCLAIMER: I have no idea if this will work. I've also seen blogs stating this cannot be done.
My guess is it should detect data. This is even possible on existing Android devices - but it is not Android Market friendly.
To accomplish this on non-4.0 devices you must provide an alternative power source to the usb port. Usually this can be done with a cheap usb hub that leaks power. The device also must be rooted. Once this is complete you need to create an interface to the device shell to launch a native language like C outside of your Dalvik VM. Use your C to bind to the appropriate socket and you should be able to bus data back and forth. You may also need to compile a kernel module to give driver support.
Like I said this is a complete hack for device below 4.0. But it is possible.

Categories

Resources