Run ADB devices on the command line to obtain the connected device. The result shows the ID of each device (serial number ). In other commands of ADB, you can use ADB-s to specify a device to execute commands, but the serial number of each device is different. How does ADB obtain this? After viewing the source code of ADB, we found that the code for getting the serial number is as follows:
// D:/project/Android-1.5/development/host/Windows/USB/API/adb_interface.cpp <br/> bool adbinterfaceobject: getserialnumber (void * buffer, <br/> unsigned long * buffer_char_size, <br/> bool ANSI) {<br/> If (! Isopened () {<br/> setlasterror (error_invalid_handle); <br/> return false; <br/>}< br/> // open USB device for this intefface <br/> handle usb_device_handle = createfile (interface_name (). c_str (), <br/> generic_read, <br/> file_1__read | file_1__write, <br/> null, <br/> open_existing, <br/> 0, <br/> null); <br/> If (invalid_handle_value = usb_device_handle) <br/> return NULL; <br/> wchar se Rial_number [512]; <br/> // send IOCTL <br/> DWORD ret_bytes = 0; <br/> bool ret = deviceiocontrol (usb_device_handle, <br/> adb_ioctl_get_serial_number, <br/> null, 0, <br/> serial_number, sizeof (serial_number), <br/> & ret_bytes, <br/> null ); <br/> // preserve error accross closehandle <br/> ulong error = RET? No_error: getlasterror (); <br/>: closehandle (usb_device_handle); <br/> If (no_error! = Error) {<br/> setlasterror (error); <br/> return false; <br/>}< br/> unsigned long str_len = <br/> static_cast (wcslen (serial_number) + 1); <br/> If (null = buffer) | (* buffer_char_size <str_len) {<br/> * buffer_char_size = str_len; <br/> setlasterror (error_insufficient_buffer); <br/> return false; <br/>}< br/> If (! ANSI) {<br/> // If user asked for wide char name just return it <br/> wcscpy (reinterpret_cast (buffer), serial_number); <br/> return true; <br/>}< br/> // we need to convert name from wide Char to ANSI string <br/> int res = widechartomultibyte (cp_acp, <br/> 0, <br/> serial_number, <br/> static_cast (str_len), <br/> reinterpret_cast (buffer), <br/> static_cast (* buffer_char_size), <br/> null, <br/> N Ull); <br/> return (res! = 0); <br/>}
From the code above, we can see that ADB is obtained by sending adb_ioctl_get_serial_number through deviceiocontrol, and the above createfile uses interface_name as the full path of the ADB device, in the format "////? // USB # vid_xxxx & pid_xxxx & mi_xx #123456789 abcdef # {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}, the code for creating an ADB device is as follows:
// D:/project/Android-1.5/development/host/Windows/USB/API/adb_api.cpp <br/> adbapihandle adbcreateinterface (guid class_id, <br/> unsigned short vendor_id, <br/> unsigned short product_id, <br/> unsigned char interface_id) {<br/> // enumerate all active interfaces for the given class <br/> adbenuminterfacearray interfaces; <br/> If (! Enumeratedeviceinterfaces (class_id, <br/> digcf_deviceinterface | digcf_present, <br/> true, <br/> true, <br/> & interfaces) {<br/> return NULL; <br/>}< br/> If (interfaces. empty () {<br/> setlasterror (error_device_not_available); <br/> return NULL; <br/>}< br/> // now iterate over active interfaces looking for the name match. <br/> // The name is formatted as such: <br/> //"////? // USB # vid_xxxx & pid_xxxx & mi_xx #123456789 abcdef # {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} "<br/> // Where <br/> // vid_xxxx is for the vendor ID (XXXX are hex for the given vendor ID ), <br/> // pid_xxxx is for the product ID (XXXX are Hex for the given product ID) <br/> // mi_xx is for the interface ID (XX are Hex for the given interface ID) <br/> // enumeratedeviceinterfaces will guarantee that returne D interface names <br/> // will have our class ID at the end of the name (those last xxxes in the <br/> // format ). so, we only need to match the beginning of the name <br/> wchar_t match_name [64]; <br/> If (0xff = interface_id) {<br/> // No interface ID for the name. <br/> swprintf (match_name, l "////? // USB # VID _ % 04x & pid _ % 04x # ", <br/> vendor_id, product_id ); <br/>} else {<br/> // with interface ID for the name. <br/> swprintf (match_name, l "////? // USB # VID _ % 04x & pid _ % 04x & mi _ % 02x # ", <br/> vendor_id, product_id, interface_id ); <br/>}< br/> size_t match_len = wcslen (match_name); <br/> for (adbenuminterfacearray: iterator it = interfaces. begin (); <br/> it! = Interfaces. end (); It ++) {<br/> const adbinstanceenumentry & next_interface = * it; <br/> If (0 = wcsnicmp (match_name, <br/> next_interface.device_name (). c_str (), <br/> match_len) {<br/> // found requested interface among active interfaces. <br/> return adbcreateinterfacebyname (next_interface.device_name (). c_str (); <br/>}< br/> setlasterror (error_device_not_available); <br/> return NULL; <br/>}
ADB is a USB device that does not have all USB devices (classid: {0xf72fe0d4, 0 xcbcb, 0x407d, {0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b}), so it takes at least one second for ADB devices to discover new devices after USB is inserted.
// D:/project/Android-1.5/system/CORE/ADB/usb_windows.c <br/> void * device_poll_thread (void * unused) {<br/> D ("created device thread/N"); <br/> while (1) {<br/> find_devices (); <br/> adb_sleep_ms (1000); <br/>}< br/> return NULL; <br/>}
If you use Windows to listen to a USB device to listen to the ADB device, you can obtain the interface_name used by the getserialnumber function in the dbcc_name field of the pdev_broadcast_deviceinterface structure.