Android Gets the name of the USB device

Source: Internet
Author: User

1. Inside the note. Is the code that Samsung devices may not support and need to be replaced.

2.mUsbManager. Getsystemservice (Context.usb_service) was obtained.

3. Picked from the StackOverflow. The source address could not be found.

protected static final int std_usb_request_get_descriptor = 0x06;
Http://libusb.sourceforge.net/api-1.0/group__desc.html
protected static final int libusb_dt_string = 0x03;

Public String Getusbname () {
String strusbname = null;
hashmap<string, usbdevice> devicelist = Musbmanager.getdevicelist ();
if (devicelist.size () = = 0) {
return strusbname;
}

iterator<usbdevice> deviceiterator = Devicelist.values (). Iterator ();

if (Deviceiterator.hasnext ()) {
Usbdevice device = (usbdevice) deviceiterator.next ();
Strusbname = Device.getdevicename ();

LOG.D ("", "Name:" + device.getdevicename () + "\ n"
+ "VID:" + Device.getvendorid ()
+ "PID:" + Device.getproductid ());

Usbinterface intf = device.getinterface (0);
int EPC = 0;
EPC = Intf.getendpointcount ();
LOG.D ("", "Endpoints:" + EPC + "\ n");

LOG.D ("", "Permission:" + boolean.tostring (musbmanager.haspermission (device)) + "\ n");

Usbdeviceconnection connection = Musbmanager.opendevice (device);
if (null==connection) {
LOG.D ("", "(Unable to establish connection) \ n");
} else {

Claims exclusive access to a usbinterface.
This must is done before sending or receiving data on
Any usbendpoints belonging to the interface.
Connection.claiminterface (intf, true);

Getrawdescriptors can used to access descriptors
Not supported directly via the higher level APIs,
Like getting the manufacturer and product names.
Because it returns bytes, you can get a variety of
Different data types.
byte[] Rawdescs = Connection.getrawdescriptors ();
String manufacturer = "", Product = "";

Try
{
byte[] buffer = new byte[255];
int idxman = rawdescs[14];
int IDXPRD = rawdescs[15];

int rdo = Connection.controltransfer (usbconstants.usb_dir_in
| Usbconstants.usb_type_standard, Std_usb_request_get_descriptor,
(libusb_dt_string << 8) | Idxman, 0, buffer, 0xFF, 0);
Manufacturer = new String (buffer, 2, rdo-2, "Utf-16le");

RDO = Connection.controltransfer (usbconstants.usb_dir_in
| Usbconstants.usb_type_standard, Std_usb_request_get_descriptor,
(libusb_dt_string << 8) | IDXPRD, 0, buffer, 0xFF, 0);
Product = new String (buffer, 2, rdo-2, "Utf-16le");

/* int rdo = Connection.controltransfer (usbconstants.usb_dir_in
| Usbconstants.usb_type_standard, Std_usb_request_get_descriptor,
(libusb_dt_string << 8) | Idxman, 0x0409, buffer, 0xFF, 0); */

} catch (Unsupportedencodingexception e)
{
E.printstacktrace ();
}

LOG.D ("", "Manufacturer:" + manufacturer + "\ n");
LOG.D ("", "Product:" + product + "\ n");
LOG.D ("", "serial#:" + connection.getserial () + "\ n");
}

}
return strusbname;
}

Android Gets the name of the USB device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.