I recently learned a little python, and there was just a pre-research job on the mobile synchronization tool to complete.
To realize the communication between PCs and mobile phones, first find their communication protocol. Fortunately, Android has a complete protocol: ADB.
ADB'sCodeIt is open-source and supports Windows platforms. Ready-made dll can be called:Adbwinapi. dll,Adbwinusbapi. dll
Well, you can do it with VC, but I want to try it with python, so I started the hard-pressed process of data query + experiment.
The experiment process is not much said. Because the above two DLL files are implemented in C, the header files provided are also in C language, so with the following Python TestProgram(Python2.7 ):
Import ctypes # custom guid structure. If you are interested, you can use the uuid module class GUID (ctypes. structure): _ fields _ = [("data1", ctypes. c_ulong), ("data2", ctypes. c_ushort), ("data3", ctypes. c_ushort), ("data4", ctypes. c_ubyte * 8)] # A struct defined by myself, which facilitates the use of the DLL Interface Class adbinterfaceinfo (ctypes. structure): _ fields _ = [("class_id", guid), ("Flags", ctypes. c_ulong), ("device_name", ctypes. c_wchar * 800)] def strguid (guid): String = ''string = string + '% x' % buff. class_id.data1 + '-% x' % buff. class_id.data2 + '-% x' % buff. class_id.data3 string = string + '-% x' % buff. class_id.data4 [0] string = string + '% x' % buff. class_id.data4 [1] string = string + '% x' % buff. class_id.data4 [2] string = string + '% x' % buff. class_id.data4 [3] string = string + '% x' % buff. class_id.data4 [4] string = string + '% x' % buff. class_id.data4 [5] string = string + '% x' % buff. class_id.data4 [6] string = string + '% x' % buff. class_id.data4 [7] Return stringdll = ctypes. cdll. loadlibrary ('adbwinapi. dll ') usb_class_id = GUID (0xf72fe0d4, 0 xcbcb, 0x407d, (0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b )) enum_handle = DLL. adbenuminterfaces (usb_class_id, ctypes. c_bool ('true'), ctypes. c_bool ('true'), ctypes. c_bool ('true') while (1): buff = adbinterfaceinfo () size = ctypes. c_ulong (ctypes. sizeof (buff) status = DLL. adbnextinterface (enum_handle, ctypes. byref (buff), ctypes. byref (size) If status = 1: # print "guid =" + strguid (buff. class_id) # print "status =" + STR (Status) # print "name =" + STR (buff. device_name) hadbapi = DLL. adbcreateinterfacebyname (buff. device_name); If hadbapi = 0: Print 'adbcreateinterfacebyname fail 'else: Serial = ''* 128 pserial = ctypes. c_char_p () pserial. value = serial serial_len = ctypes. c_ulong (LEN (Serial) ret = DLL. adbgetserialnumber (hadbapi, pserial, ctypes. byref (serial_len), ctypes. c_bool ('false'); If ret = 1: Print 'device name: '+' % s' % serial else: Print 'get device name fail 'else: print 'finished' break
The above simple Python code can be passed throughAdbwinapi. dllAndAdbwinusbapi. dllThese two DLL to find the Android device that is connected to your PC.
Only three DLL interfaces are called, but the purpose has been achieved. The following conclusions can be drawn:
It is feasible to use python to call the DLL method to implement the ADB tool. Of course, there is no less trouble.
At the end of this article, it is quite troublesome for python to call the DLL written in C, especially for parameter passing, especially for pointer processing. In this regard, the ctypes module has to be used...