树莓派(二) adb命令控制手机拨打/接听电话

实验场景:

将手机通过数据线与树莓派相连,使用终端命令查看是否已经连接上,若成功则显示:

# 查看adb命令连接的设备
adb devices


# 若成功连接则再次运行显示
List of devices attached
...(这里是设备名)  device

常见命令:

  • 锁定/解锁手机
adb shell input keyevent 26 //锁定手机

adb shell input keyevent 82 //解锁手机(如果设置了密码,会提示输入密码)
  • 重启/关机
adb shell reboot  //重启

adb shell reboot -p  //关机
  • 打开关闭蓝牙
adb shell service call bluetooth_manager 6 //打开蓝牙

adb shell service call bluetooth_manager 9 //关闭蓝牙
  • 打开关闭wifi
adb shell svc wifi enable  //打开wifi

adb shell svc wifi disable  //关闭wifi
  • 连接时保持亮屏
svc power stayon [true|false|usb|ac|wireless]
  • 抓取开机日志
adb wait-for-device && adb shell logcat -v threadtime | tee mybootup.log
  • 查看日志
adb logcat
  • 关闭/重启adb服务进程
adb kill-server

adb start-server
  • 拨打电话
adb shell am start -a android.intent.action.CALL -d tel:10010

查看手机当前通话状态:

adb shell dumpsys telephony.registry
  • mCallState - 呼叫状态
    • 0:表示待机状态
    • 1:表示来电尚未接听状态
    • 2:表示电话占线
  • mServiceState - 服务状态
    • 0:表示正常使用状态
    • 1:表示电话没有连接到任何电信运营网络
    • 2:表示电话只能拨打紧急呼叫号码
    • 3:表示电话已关机

实验代码:

这里主要实现通过adb命令判断手机的通话状态,从而控制手机是拨打还是接听电话

import os

def test_call():
    # 解锁手机
    os.popen('adb shell input keyevent 82')
    sleep(0.5)
    # 向上滑屏
    os.popen('adb shell input swipe 550 2360 577 1198')
    sleep(0.5)
    # 打开拨号界面
    os.popen('adb shell input keyevent 5')
    sleep(0.5)
    
    # 查询通话状态
    answer = os.popen('adb shell dumpsys telephony.registry | grep "mCallState"')
    state = answer.read()

    while True: 
        if "mCallState=0" in state:
            # 拨打电话
            number = input("请输入要拨打的号码:\n")
            call = os.popen('adb shell am start -a android.intent.action.CALL -d tel:{}'.format(number))
        elif "mCallState=1" in state:
            # 自动接听来电
            os.popen('adb shell setprop persist.sys.tel.autoanswer.ms 2000')
        else:
            # 挂断电话
            os.popen('adb shell input keyevent 6')
    
if  __name__  ==  '__main__':
    test_call()

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值