Recently learned a little python, and then just have a mobile phone synchronization tool for the pre-research work to be completed.
To realize the communication between PC and mobile phone, first to find their communication protocol, fortunately, Android has a perfect protocol: ADB
The ADB code is open source and supports the Windows platform, and there are ready-made DLLs that can be called: Adbwinapi.dll,adbwinusbapi.dll
OK, VC can be done, but I want to use Python to try, so began the bitter force of the search data + experimental process.
The experimental process is not much to say, because the above two DLLs are implemented in C, the provided header file is also C language, so the following Python test program (Python2.7):
Import cTYPES #自定义的GUID结构, interested can study with 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)] #自己定义的一个结构体, facilitates the use of DLL interface class Adbinterfaceinfo (cTYPES. Structure): _fields_ = [("class_id", GUID), ("Flags", Ctypes.c_ulong), ("Device_name", CT ypes.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 string dll = Ctypes.cdll.LoadLibrary (' AdbWinApi.dll ') usb_class_id = GUID (0xf72fe0d4, 0xCBCB, 0x407d, (0x88, 0x14, 0 x9e, 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 =" + Strgui D (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 pser ial = ctypes.c_char_p () Pserial.value = serial Serial_len = Ctypes.c_ulong (len (serial)) r ET = 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 Fa Il ' else:print ' finished ' Breakimport ctypes# a custom GUID structure that is interesting to study with 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)] #自己定义的一个结构体, facilitates the use of DLL interface class Adbinterfaceinfo (cTYPES. Structure): _fields_ = [("class_id", GUID), ("Flags", Ctypes.c_ulong), ("Device_name", CT ypes.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, 0xCBCB, 0x407d, (0x 0x9e, 0x14, 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 (b uff.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 pser ial = 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 Fa Il ' else:print ' finished ' break
In this simple Python code, you can find the Android devices that are connected to your PC via the AdbWinApi.dll and AdbWinUsbApi.dll two DLLs.
Only 3 DLL interfaces have been called, but the purpose has been reached and the following conclusions can be drawn:
It is possible to implement the ADB tool in a way that Python calls DLLs, and of course there is no less hassle.
Write in the end, Python call C write DLL is still more troublesome, especially the parameter transfer, especially the pointer processing, this aspect also relies on the cTYPES module ...