树莓派(二) 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()

你可能感兴趣的:(树莓派使用中的问题,adb,python)