Accurately correct Android clock with adb and python

Preface

If you want to correct the Android clock from the adb command, you can correct it by rooting and typing the following command from the shell.

$ adb shell "su -c 'date `date +%s`'"

However, with this method, you can specify only in seconds, so in the worst case, an error of 1 second will occur. There is no problem in normal use, but this method cannot be used, for example, when the deviation in seconds becomes fatal when analyzing the log output. There is also a method of using the NTP application, but since I found only the application that requires screen operation, I created a script using Python to automate it and corrected it.

strategy

Very simply, the adb command is executed at XX hours XX minutes XX seconds .000000.

Implementation by Python

It seems that it can be done with a shell script, but it was troublesome (I don't know), so I implemented it in Python. Since it is written in 2 series, please change it to 3 series as appropriate.

Source

import sys
import subprocess
import time
from contextlib import closing

m = 8

def loop():
    while True:
        t = time.time()
        it = int(t*10**m)
        if it == int(t)*10**m:
            subprocess.call("adb shell \"su -c 'date %d'\"" % int(t), shell=True)
            break

if __name__=='__main__':
    loop()
    print "Done"

Commentary

Call the loop () method and enter an infinite loop until you can set the time. The time is acquired by time.time (), but since the obtained value is float, the value after the decimal point is also included. Compare the value rounded after rounding up to the number of digits specified by m: int (t × $ 10 ^ m $) and the value rounded up and rounded up: int (t) × $ 10 ^ m $, and if they are the same, execute the command. If not, repeat the time acquisition again. In my environment, I was able to set the time without any problem even if I specified up to 8 digits, but it may be better to change this area according to the environment.

I thought it was written, but the method that specifies the precision by giving m as an argument is more beautiful. After that, it may be kinder to be able to time out. I will implement it if I have time.

Impact of command execution delay

Actually, there is a delay from the execution of the adb command until it is reflected, so an error of about 10 msec will occur with a margin rather than an 8-digit precision. Therefore, measure how much the delay is (I will post an article about the method later), shift it in advance, and execute the command. In particular

padding = 0.07
...
    t = time.time() + padding

The time is staggered in the form of. In my environment, the deviation was about 0.07 = 70msec, and it is not very stable, so this method has to allow a deviation of about 100msec.

Recommended Posts

Accurately correct Android clock with adb and python
Interact with Python on Android from PC via adb
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
python with pyenv and venv
Works with Python and R
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Scraping with Python, Selenium and Chromedriver
Scraping with Python and Beautiful Soup
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Reading and writing NetCDF with Python
I played with PyQt5 and Python3
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python
Communicate between Elixir and Python with gRPC
Data pipeline construction with Python and Luigi
Calculate and display standard weight with python
Monitor Mojo outages with Python and Skype
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Passwordless authentication with RDS and IAM (Python)
Python installation and package management with pip
Using Python and MeCab with Azure Databricks
POST variously with Python and receive with Flask
Capturing images with Pupil, python and OpenCV
Fractal to make and play with Python
A memo with Python2.7 and Python3 on CentOS
Use PIL and Pillow with Cygwin Python
Create and decrypt Caesar cipher with python
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Reading and writing JSON files with Python
Dealing with "years and months" in Python
I installed and used Numba with Python3.5
Tweet analysis with Python, Mecab and CaboCha
Linking python and JavaScript with jupyter notebook
Traffic monitoring with Kibana, ElasticSearch and Python
FM modulation and demodulation with Python Part 2
Encrypt with Ruby (Rails) and decrypt with Python
Easily download mp3 / mp4 with python and youtube-dl!
Operate home appliances with Python and IRKit
Clean python environment with pythonz and virtualenv
Practice web scraping with Python and Selenium
Easy web scraping with Python and Ruby
Importing and exporting GeoTiff images with Python
I'm using tox and Python 3.3 with Travis-CI
Happy GUI construction with electron and python
Use Python and MeCab with Azure Functions