Honey, There Is a Python in My Android Phone!

摘要

Simple scripting, such as Python, Bash, PHP, Perl are all embedded in modern OS. Since Android is also a linux-based OS, it is also possible to write a script. Unfortunately before SL4A arrives, it is impossible to script on Android phone. SL4A provides rich APIs that allow user to access Android intents such as GPS location, Accelerometer and etc. through scripting. Hence with those APIs, the user can build simple scripts to interact with those APIs.

This talks demonstrates how android intents can be accessed using python client, hence understanding the interaction between Android and Python API inside SL4A via RPC by dissecting the source code of both Android and Python API inside SL4A. The speaker will then demonstrate how different android intents such as GPS coordinate and gyroscope through a presentation of websocket coded in Python and webapp demo using babylon.js, together with a flask application that allows user to send SMS messages through Rest APIs, using android API.

說明

This is a small code snippet that I have written in Python for the POC of getting the GPS coordinate from android phone: ```python import androidhelper import json import time from bottle import Bottle, run, response, request app = Bottle() droid = androidhelper.Android() @app.route("/location") def get_location(): droid.startLocating() droid.eventWaitFor('location', 12500) time.sleep(0.1) current_location = droid.readLocation().result if not current_location: current_location = droid.getLastKnownLocation().result droid.stopLocating() response.content_type = "application/json" return json.dumps( { "result": current_location }, indent=4 ) ``` This project was initiated during my Christmas vacation, inspired by [Android Barcode Scanner](https://www.mattcutts.com/blog/android-barcode-scanner/) as I was looking on the solution that I could catalogue my books by scanning the ISBN using android phone. This quick solution did save me enormous amount of time in cataloguing my books. Intriguing by the design of APIs that can interact with android intents, I decided to dig further to understand how the APIs works. In concept, there is an RPC call between android app and python interpreter within the app that acts as a bridge, which allows python APIs to trigger Android intents, upon calling related APIs. For example, using the code snippet above, when Python GPS API is executed, the API will send request to Android GPS intent via the RPC call. Once Android intent receives the request, it will grab the GPS data, and passes the GPS data back to Python GPS API through the same channel. Reference: [python-for-android](https://github.com/tangingw/python-for-android) [Qpython](http://www.qpython.org/en/guide_howtostart.html) [Android Barcode Scanner](https://www.mattcutts.com/blog/android-barcode-scanner/)

講者

Ing Wei Tang

Ing Wei is the chair for PyCon MY 2019, the co-chair of PyCon MY 2018, and vice president of MyPOP. He has spoken in various PyCons, particularly in PyCon APAC 2018, as well as involving in PyCon communities actively in Malaysia.

He uses python a lot in his daily work, especially coding the automation process and flow. During his past time, he likes to experience and perform hacking different things on operating system level.

Apart from programming language, he can also speak 5 different types of languages concurrently in one sentence. Please ask for demo if time permits.