近期学了一点Python,然后正好有一个手机同步工具方面的预研工作要完成。

要实现PC与手机的通信,首先要找到他们的通信协议,还好的是Android有完善的协议:ADB

ADB的代码是开源的,而且支持Windows平台,有现成的DLL可以调用:AdbWinApi.dll,AdbWinUsbApi.dll

好了,可以用VC搞定,但我想用Python试一下,于是开始了苦逼的查资料+实验的过程。

实验过程就不多说了,由于上面的两个DLL都是用C实现的,提供的头文件也是C语言的,所以有了下面这个python测试程序(Python2.7):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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

上面这个简单的Python代码,可以通过AdbWinApi.dll和AdbWinUsbApi.dll这两个DLL来找到你PC上正连接着的Android设备。

只调用了3个DLL接口,但目的已经达到,可以得出下面的结论:

使用Python调用DLL的方式来实现ADB工具是可行的,当然麻烦是不会少的了。

写在最后,Python调用C写的DLL还是比较麻烦的,特别是参数传递,尤其是指针的处理,这方面还得依靠ctypes模块。。。

www.qytang.com/
http://www.qytang.com/cn/list/29/
http://www.qytang.com/cn/list/28/610.htm
http://www.qytang.com/cn/list/28/595.htm
http://www.qytang.com/cn/list/28/583.htm
http://www.qytang.com/cn/list/28/582.htm
http://www.qytang.com/cn/list/28/576.htm
http://www.qytang.com/cn/list/28/523.htm
http://www.qytang.com/cn/list/28/499.htm
http://www.qytang.com/cn/list/28/488.htm
http://www.qytang.com/cn/list/28/466.htm
http://www.qytang.com/cn/list/28/463.htm
http://www.qytang.com/cn/list/28/458.htm
http://www.qytang.com/cn/list/28/455.htm
http://www.qytang.com/cn/list/28/447.htm

Python手机开发调用DLL实现部分ADB功能-乾颐堂的更多相关文章

  1. python socket编程入门(编写server实例)-乾颐堂

    python 编写server的步骤: 1. 第一步是创建socket对象.调用socket构造函数.如: socket = socket.socket( family, type ) family参 ...

  2. python中执行命令的3种方法小结-乾颐堂

    目前我使用到的python中执行cmd的方式有三种: 1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在linux上执行的信息. import os o ...

  3. python基础之删除文件及删除目录的方法-乾颐堂

    下面来看一下python里面是如何删除一个文件及文件夹的~~ 首先引入OS模块 import os 删除文件: os.remove() 删除空目录: os.rmdir() 递归删除空目录: os.re ...

  4. python算法 - 快速寻找满足条件的两个数-乾颐堂

    题目前提是一定存在这样两个数 解法一就不写了...一般想不到吧 一开始想到的是解法二最后的用hash表 (其实是想到创建一个跟target一样大的数组啦..存在就写入index,但是要全部找出,那得二 ...

  5. Python服务器开发 -- 网络基础-乾颐堂

    网络由下往上分为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. HTTP是高层协议,而TCP/IP是个协议集,包过许多的子协议.包括:传输层的 FTP,UDP,TCP协议等,网络层的ip ...

  6. 开发中常遇到的Python陷阱和注意点-乾颐堂

    最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...

  7. 常用的 Python 调试工具,Python开发必读-乾颐堂

    以下是我做调试或分析时用过的工具的一个概览.如果你知道有更好的工具,请在评论中留言,可以不用很完整的介绍. 日志 没错,就是日志.再多强调在你的应用里保留足量的日志的重要性也不为过.你应当对重要的内容 ...

  8. python 开发简单的聊天工具-乾颐堂

    python 太强大了,以至于它什么都可以做,哈哈,开个玩笑.但是今天要讲的真的是一个非常神奇的应用. 使用python写一个聊天工具 其实大家平时用的QQ类似的聊天工具,也是使用socket进行聊天 ...

  9. Python守护进程(多线程开发)-乾颐堂

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

随机推荐

  1. Java类型强制转换

    int intA = 10; String StrB = "12"; int c = Integer.parseInt(StrB); // 把String转换成int String ...

  2. erlang的一些小技巧(不定期更新)

    在任意节点热更新代码 rpc:call(Node,c,l,[Mod]) c和l的指的是code,library Erlang Shell隐藏的小技巧 f(). %%把所有绑定变量释放掉 f(Val). ...

  3. Tomcat原理的一点看法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 首先,运行tomcat之前,先写一个html页面, 再写一个serlvet程序 配置文件如下: 那么程序是怎样运行的呢? 浏览器发起请求到 --& ...

  4. [转载]树莓派新版系统上使用mjpg-streamer获取USB摄像头和树莓派专用摄像头RaspiCamera图像

    树莓派新版系统上使用mjpg-streamer获取USB摄像头和树莓派专用摄像头RaspiCamera图像 网上有很多关于mjpg-stream移植到树莓派的文章,大部分还是使用的sourceforg ...

  5. Jquery学习小计

    实时监听输入框值变化 首先创建Jquery.fn扩展 jQuery.fn.extend({ inputChange: function(callback){ if($.support.leadingW ...

  6. Go - reflection

    Go 语言也有反射的机制,通过这种机制可以大大提高程序的灵活性. reflect包中的 TypeOf 与 ValueOf 方法 首先,当我们想要使用反射技术的时候,我们需要先引用 reflect 包. ...

  7. 使用jQuery Pagination Plugin实现分页效果

    最近使用分页这个基础效果较为频繁,而项目前端页面使用的是纯静态的HTML,自己之前写的JSP中的分页就用不成了:项目中也引入了Bootstrap,本来想使用Bootstrap中的分页样式,但发现其样式 ...

  8. poj 3790 Recursively Palindromic Partitions

    /*摘抄自博客:Recursively Palindromic Partitions Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...

  9. scrapy框架的持久化存储

    一 . 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. 执行输出指定格式进行存 ...

  10. ARP数据包伪造

      一台网络中的计算机,其传递到网络中的数据包的内容是完全由其软硬件逻辑决定的,软件可以操控硬件,硬件亦是一种特殊的软件,所以,接收者只根据数据包的内容,绝不可能判定此数据包的真正来源,一切都是可以伪 ...