[adb] python 안드로이드 스마트폰 제어하기
1. 스마트폰과 연결하기 위해 구글 개발자 페이지에서 adb (android device bridge)를 설치합니다.
https://developer.android.com/studio/releases/platform-tools?hl=ko#downloads
SDK 플랫폼 도구 출시 노트 | Android 개발자 | Android Developers
Android SDK 플랫폼 도구는 Android SDK의 구성요소입니다.
developer.android.com
1-1. 윈도우 환경변수에 해당 폴더 추가
2. 자신의 파이썬 작업환경에서 pure-python-adb를 설치합니다.
pip install pure-python-adb
3. 안드로이드 스마트폰에서 개발자모드 활성화 및 USB 디버깅 모드 허용
위 작업이 모두 완료되었다면 cmd창에 adb를 입력해봅니다.
휴대폰을 PC에 연결해주고 USB 디버깅 알림을 허용해줍니다.
그 후 cmd창에서 아래 명령어를 입력합니다.
adb start-server
연결한 스마트폰을 확인하는 방법은 아래와 같습니다.
adb devices
이제 휴대폰을 조작하기 위한 모든 준비가 끝났습니다.
5. 아래와 같이 파이썬코드를 작성해줍니다.
현재 화면의 스크린샷을 찍는 간단한 코드입니다. 바로 실행해보겠습니다.
from ppadb.client import Client as AdbClient
import time
class ADBTest:
def __init__(self) :
print('ADBTest ready')
def connect(self):
client = AdbClient(host="127.0.0.1", port=5037) # Default is "127.0.0.1" and 5037
devices = client.devices()
if len(devices) == 0:
print('No devices')
quit()
device = devices[0]
print(f'device: {device}')
print(f'client: {client}')
return device, client
def device_screen_capture(self, device):
result = device.screencap()
with open('screen.png', 'wb') as fp:
fp.write(result)
def work_start(self):
device, client = self.connect()
self.device_screen_capture(device)
if __name__ == "__main__":
ADBbot = ADBTest()
ADBbot.work_start()
끝