I have learned a little bit of Python recently, and I happen to have a pre-research work on mobile phone synchronization tools to complete.
To achieve communication between PC and mobile phone, you must first find their communication protocol. Fortunately, Android has a complete protocol: ADB
ADB’s code is open source and supports the Windows platform. There are ready-made DLLs that can be called: AdbWinApi.dll, AdbWinUsbApi.dll
Okay, it can be done with VC, but I wanted to try it with Python, so I started the arduous process of checking information + experimenting.
I won’t go into details about the experimental process. Since the above two DLLs are implemented in C, and the provided header files are also in C language, we have the following python test program (Python2.7):
import ctypes #自定义的GUID结构,有兴趣的可以自己研究用uuid模块 class GUID(ctypes.Structure): _fields_ = [("Data1", ctypes.c_ulong), ("Data2", ctypes.c_ushort), ("Data3", ctypes.c_ushort), ("Data4", ctypes.c_ubyte*8)] #自己定义的一个结构体,便于使用DLL接口 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 string dll = ctypes.cdll.LoadLibrary('AdbWinApi.dll') usb_class_id = GUID(0xF72FE0D4, 0xCBCB, 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 import ctypes #自定义的GUID结构,有兴趣的可以自己研究用uuid模块 class GUID(ctypes.Structure): _fields_ = [("Data1", ctypes.c_ulong), ("Data2", ctypes.c_ushort), ("Data3", ctypes.c_ushort), ("Data4", ctypes.c_ubyte*8)] #自己定义的一个结构体,便于使用DLL接口 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 string dll = ctypes.cdll.LoadLibrary('AdbWinApi.dll') usb_class_id = GUID(0xF72FE0D4, 0xCBCB, 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 use the two DLLs AdbWinApi.dll and AdbWinUsbApi.dll to find the Android device connected to your PC.
Only 3 DLL interfaces have been called, but the purpose has been achieved. The following conclusion can be drawn:
It is feasible to use Python to call DLL to implement the ADB tool, but of course there will be a lot of trouble.
Written at the end, it is quite troublesome for Python to call a DLL written in C, especially parameter passing, especially pointer processing, which requires the ctypes module. . .