Hello everyone, from 3.1 to start, android devices have been available as HOST terminal, and the official visit of a USB-API.
I need to use in the official Android 4.0 USB API to read and write external USB devices.
Test environment:
Phone: Samsung 9250
System: android 4.0
External device: U disk, mouse, keyboard
Refer to the official code, I wrote a small application to read USB devices.
After verification, only U disk can be read correctly the application layer, while the HID device can not be accessed.
Currently, my personal USB devices KERNEL inference is being MOUNT to the specified file.
UsbManager is to enumerate the files.
MOUNT HID device will not be a designated file, the application layer can not be accessed.
I do not know whether you have done a similar development, could give me some tips.
Attached test code:
private void UsbInit ()
{
Intent intent = getIntent ();
mUsbManager = (UsbManager) getSystemService (Context.USB_SERVICE);
if (mUsbManager == null)
{
Toast.makeText (this,
"USB manager is not OK",
. Toast.LENGTH_LONG) show ();
return;
}
/ / MUsbDevice = (UsbDevice) intent.getParcelableExtra (UsbManager.EXTRA_DEVICE);
/ / Check for existing devices
for (UsbDevice device: mUsbManager.getDeviceList () values ().) {
if (device! = null)
{
mUsbDevice = device;
break;
}
}
if (mUsbDevice == null)
{
Toast.makeText (this,
"USB is not OK",
. Toast.LENGTH_LONG) show ();
return;
} Else {
String tmp = "vid is";
tmp = tmp + Integer.toHexString (mUsbDevice.getVendorId ()) + ".";
tmp = tmp + "pid is";
tmp = tmp + Integer.toHexString (mUsbDevice.getProductId ()) + ".";
Toast.makeText (this, tmp, Toast.LENGTH_LONG) show ();.
}
if (mUsbDevice.getInterfaceCount ()! = 1) {
Log.e (TAG, "could not find interface");
return;
}
UsbInterface intf = mUsbDevice.getInterface (0);
/ / Device should have one endpoint
if (intf.getEndpointCount () <1) {
Log.e (TAG, "could not find endpoint");
return;
}
/ / Endpoint should be of type interrupt
UsbEndpoint ep = intf.getEndpoint (2);
if (ep.getType ()! = UsbConstants.USB_ENDPOINT_XFER_INT) {
Log.e (TAG, "endpoint is not interrupt type");
return;
}
mEndpointIntr = ep;
if (mUsbDevice! = null) {
UsbDeviceConnection connection = mUsbManager.openDevice (mUsbDevice);
if (connection! = null && connection.claimInterface (intf, true)) {
Log.d (TAG, "open SUCCESS");
mConnection = connection;
Thread thread = new Thread (this);
thread.start ();
} Else {
Log.d (TAG, "open FAIL");
mConnection = null;
}
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
You have encountered similar problems?
Reply:
I think your USB interface directly with the android does not work, you can try using the NDK, direct call linux HID API to open a file node, the node should be in the file under / dev / usb / directory;
Reply:
Hello, I am using ndk on the way to 2.2, but with the way the library libusb 4.0 file permissions to read data or prompt enough
libusb_open return value -3, checked to LIBUSB_ERROR_ACCESS if the user has insufficient permissions
Problem or privileges
Reply:
Reading USB node to solve the problem of empty:
1 New: android.hardware.usb.host.xml
Content:
File to the terminal device: / system / etc / directory permissions under
After sending the need to restart the device to be effective.
(2) modify the main configuration file AndroidManifest.xml
Andrews
Add in the corresponding location:
<- Android:! Required = "true" ->
android: resource = "@ xml / device_filter">
3 Add device_filter.xml file
under res \ xmlFile contents:
Where: 3 and 2 to the corresponding value, if you know, you can read from the node list, fill out correctly, android layer can receive messages corresponding insertion or removal.
4 Good luck
Reply:
This is not to say that he simply can not read USB problem, but said the mouse, keyboard and other systems can not be identified as USB, so getDeviceList is empty
I have encountered this problem, I ask LZ now solved? NDK to call the library with linux can not?
Eager to answer
Reply:
I ask you now know how to do in 4.0 yet?
Reply:
Seeking attention, seeking attention ...
Come quickly to solve the problem ah.
Reply:
Device node may still question authority. I was looking for a device node other directory can be. You can find what / dev, / sys, / proc usb device under the corresponding device nodes. One a try if the permissions to all users can read and write successfully readable and writable. Sometimes it may also in subdirectories.
Reply:
Landlord to solve the problem it? HID device will not be MOUNT into the specified file, the application layer can not be accessed. ? ? ? ? ?
I hid the device under test in dev / bus / usb will mount a file
Reply:
To solve the same landlord with no seeking ah ah @! ! @
Reply:
I am here is the problem, the landlord, ah, ah seek solution
Reply:
chmod-R 777 / dev / bus / usb /
No comments:
Post a Comment