We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

Honey, There Is a Python in My Android Phone!

00:00

Formal Metadata

Title
Honey, There Is a Python in My Android Phone!
Title of Series
Number of Parts
130
Author
License
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
This is a small code snippet that I have written in Python for the POC of getting the GPS coordinate from android phone: 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 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 Qpython Android Barcode Scanner
61
Thumbnail
26:38
95
106
Android (robot)ArmHard disk driveComputerComputer programEmulationGoogolMobile appInterpreter (computing)Android (robot)Form (programming)Computer programmingMoore's lawProgramming languageInterpreter (computing)Systems engineeringCurveFront and back endsWeb applicationInterface (computing)System administratorNumberSoftwareInstallation artOperations support systemWindowDimensional analysisCartesian coordinate systemNeuroinformatikWebsiteState of matterPresentation of a groupInteractive televisionTouchscreenPower (physics)Library (computing)FamilyPersonal identification numberFocus (optics)Multiplication signWordMobile appAverageScripting languageCASE <Informatik>Dynamical systemBitFacebookException handlingTerm (mathematics)Pairwise comparisonArithmetic meanYouTubeBacktrackingQuicksortPrisoner's dilemmaCodeJava appletCondition numberSystem callInheritance (object-oriented programming)Pattern languageAeroelasticityMeeting/InterviewComputer animation
Exploratory data analysisGoogolScripting languageBuildingBit rateAndroid (robot)Information managementPoint (geometry)Web pageEmulationUniform resource locatorMetreData storage deviceRevision controlVarianceIntegrated development environmentTouchscreenGastropod shellAndroid (robot)CodeCASE <Informatik>WritingFile formatStudent's t-testForm (programming)Cycle (graph theory)PlotterCartesian coordinate systemLaptopSoftwareNumberParticle systemMeasurement2 (number)WebsiteHacker (term)WordOffice suiteSlide ruleBitMobile appCodeProjective planeGoogolMultiplication signScripting languageRemote procedure callLine codeFocus (optics)Social classPort scannerComputer fileLine (geometry)ResultantGravitationSystem callModule (mathematics)Network socketExecution unitBackupComputer-assisted translationPoint (geometry)DatabaseBootingInformationDemo (music)Standard deviationDerivation (linguistics)Process (computing)Arithmetic meanFormal languageCommunications protocol
Demo (music)Computer animation
Error messageObject-oriented programmingMultiplication signComputer animation
Multiplication signComputer animation
Computer animation
Uniform resource locatorInformationPoint (geometry)Queue (abstract data type)CubeComputer animation
Information securityGoodness of fit
Multiplication signRight angleReal-time operating systemReal numberForm (programming)Computer animation
Event horizonMultiplication signBitAndroid (robot)Moment (mathematics)DreizehnDifferent (Kate Ryan album)PiVariety (linguistics)Set (mathematics)Order (biology)Sheaf (mathematics)CASE <Informatik>Line (geometry)Zoom lensFigurate numberAuthorizationXML
Demo (music)Sound effectMeeting/Interview
Transcript: English(auto-generated)
Okay, let me share this. Okay, cool. So we got it here, Android. So the title will be, Honey, there's a Python in my Android form. All right, so join me in clapping for James. Thank you. Okay, so today I'll be presenting as the title
that I have, Honey, there's a Python in my Android form. Now, what am I going to share over here on this thing? Okay, so first thing I'd like to talk about the title. So title probably, I don't think quite numbers of you might heard of this movie. It was a very old movie.
This took the titles come from here, Honey, I Strung the Kicks, which is a movie in 1989. I remember I watched that when I was 10, nine or 10, can't remember it, but I like this movie. And it got one and two. So if you happen to know this movie, yeah, please watch that again. I still like this movie
because it talks about a scientist who strung the family's member and gets a lot of interesting things that happens in the movie. Okay, so what's my talk about today? So my talk today about is we're trying to look at what's happening over the 20 years, especially on the computing powers that we have.
Now, if you look at the left one, 486DX4, probably not many of you know that, that was actually my first computer. So if you compare the specs of this computer, 20 years ago, this was the best or probably the most powerful computers in town at that time, which is I think in 1994.
And not to mention that if you look at specs right now, it's not even comparable to what we have nowadays with a small Android phone. And not to mention that it is very bulky. Think about that, the dimension is about 50 centimeters to 50 centimeters, but compared to our phone today,
what we got is a pocket size. And for us, during that time in 1994, we have only those 6.22 together with Windows 3.11, which I think hardly got people hear of that nowadays, unless you are a vintage user or you try to play around with all this retro computing,
probably you heard of it. And for now, compared back to Android, what we have is Android is so powerful, it can run all the applications that we know, anything like on Facebook, YouTube or anything that we have, okay? So compared to the size of what we have now and compared to years back, the power, the computing power has changed so much
that we can't even think of it and not to mention the size. However, there's a dilemma that we face nowadays with Android phones. Because Android phone is not just Android phone, it is also a computer. And not to mention that it is also a tracker. So you have all sorts of trackers on your phone, be it to be your weather, be it to be your behavior,
be it to be your usage or the app that you use, everything, yes, it's a tracker by itself. And not to mention that on average, you change your phones for every two years. So automatically, in some ways, your phones will get lag after two years. So how are we gonna deal with the phones?
Or in other words, how are we going to handle the phones after two years? Well, possibly, what we're going to do that is we're going to throw away, not going to use it anymore, we just throw it away, or maybe we'll just keep it in some darker corner, like what we have now for my case, I have three phones in my drawer,
which never been used, but still in quite good condition. Or perhaps some of you might want to use it again, like use it for GPS application. Like let's say ways you want to use it because you just simply don't want to throw the phones away or some of them might just give it to parents, like parents, they might just want to use the old phone.
But the key things that we're focusing on, this thing of that is, can we actually program an Android phone? As in program means we can write a simple programs, like maybe a basic or in this case, Python, or maybe Perl or Shell,
any simple programming language that we have on Android phone. However, sadly, what we have on Android phone currently, the available programming language will be Java based or calling, not to mention that the latest one will be dark flutter or C sharp.
So for me, I find that this is not quite cool. Reason for that is to me, I find that Java or calling has quite a high learning curve for us. Imagine that when you try to build an app, okay, you need to have quite a numbers of skill ranging from building the interface, writing the backend in Java and so forth.
So it turns out to be quite a difficult way for us to enjoy the fun of the programming, like maybe we'll just want to run a small software or programs like in basic or Python on the phone. And not to mention that again, if you're trying to install OSS other than Android,
it'd be very difficult. For example, think about that. If let's say you want to install OpenBSD or FreeBSD on the Android phone, I think that will be nearly impossible or not to mention that you try to install maybe Linux on it. It can be done, but it might be quite impossible or quite challenging to do so.
So can we actually do a programming or maybe let's say Python on an Android phone? So these are the commands that I got when I talked to people about having a Python on the Android phone. The first would be, are you serious? Are you serious about it? Or second one would be, can you actually do that?
And the third one, I got a comment actually from one of the guys when I visit Hong Kong that time and he mentioned that, well, you know, this is funny. We can't really, you know, if you can do that, that will be interesting for us to know, okay? So this is kind of command that we got from that.
Now, having spoken for so much, I haven't really introduced myself. My name is James. I'm the chair for PyCon Malaysia. And by the way, just let you have a look at my T-shirt. We have a logo over there. This is my T-shirt for 2019. We have a T-shirt over there, okay?
And I was the chair for PyCon Malaysia 2019. I was the co-chair for PyCon Malaysia 2018. And currently I'm working in an insurance company as a senior automation engineers. And the most important thing that I would like to bring on behalf of my chair for PyCon, we call it PyCon APAC 2020,
chair is we are going to launch PyCon APAC 2020 online in September, so do check out our website. And not to mention that, Guido is also joining our conference as well. So do check out our website. So the exact date will be from 13 to 16 September. So do check the updates from our website. Now let's get back to what we know about Python
before we jumped into Android. So for present state of Python, we know that Python actually can do a lot of things. So for example, we can actually build a simple script for system administration. And that was my background before that. We can also build a web app easily by Jungle or Flust or not mention that we can write the pattern
as well using the Python as well. And of course the most important thing is we can write a very simple interaction script on RPI, Raspberry Pi, through the GPIO pins for Dell libraries. So of course, you know that Dell libraries
are written in Python. So it is very easy for us to interact with that. And this is why it makes Raspberry Pi so popular among all the, you know, nearly all the those who progress with Python. And of course, MicroPython is also one of the key focus for us to write a very simple program for the embedded systems such as ESP32,
ESP822, Microbit, you name it, everything that can be written in MicroPython. So we actually can do a lot of things with Python nowadays based on what we know. But in terms of having a Python on Android, we don't have much of the choice
except from getting an app that runs with Python interpreter. By means when I say this Python interpreter, meaning that you had an app, you had an app that runs Python interpreter on it, it is not something that like Raspberry Pi where you have something that allows you
to interact with the API, okay? Or in other words, interact with the sensors that you have on your phone. For example, gyroscope, for example, the accelerometer, other sensors like GPS,
or perhaps let's say API that allows you to make a phone call, we hardly have that, okay? So probably there's one there over there which that will be the focus of my talk today which I'm going to show that how we actually can do that. Okay, so there's one thing, okay? So yes, the answers for that is
we actually have an access on that Python but sadly, we don't have much of the application on that. So probably the question that you'd like to ask me on that is how actually can we do that? Or what kind of apps that allows us to do that? So the motivation I have to start with this project
actually start from four years back where I'm actually quite boring during my Christmas week where I'm thinking of cataloging my book as you can see the books I have right now in the back. So that was something that motivates me to do so. So when I look at my phone, my old phone, as I mentioned before I have few phones in my drawer,
I like to think of, okay, is it possible to allow me to use that phone to build a barcode scanner to scan the ISBN barcode and then it allows me to insert my book into my DB. Well, now database, okay? That was something that I have in my talk.
Is it possible for me to do so? Because a phone allows us to scan the barcode easily and not to mention that if you download the app, you can simply just scan the barcode and it returns to you some kind of a ISBN 30 number if you're a good lover. So this is my motivation. How am I gonna do that?
So I begin to start my Google and then search and then I find that somebody did that before and the first line of the search actually interest me, Android barcode scanner in six line of Python code. So I begins to wonder, how actually can you do that? Okay, how actually can you do that? So what kind of app that you use on that? So I decided to visit the Digmore, okay?
And I found that actually there's two kind of app at that time, 2016, where first app is called Android scripting environment and another one is a derivative of it is, also you call it a variance of it is a scripting language for Android. Both actually, the first one actually is the first version of Google project and then the other one,
if I do not remember wrongly, is the second version of the, probably you can say it's an enhanced version of the Android scripting environment. So what makes me interested with this project is that based on what I searched from Wikipedia, I found that actually it mentioned that this apps
allows us to assess the script, not assess these sensors from the 4D Android with a very simple script, like Perl, like shell script. And of course, in this case, that will be Python. So this interests me.
So I begin to look at the code of this mark first and realize that the coding is very, very simple. So just six line of code and then you scan it and it returns the ISBN and use the ISBN to look for the books from Google. So simple enough. So this allows us to build an app
and together with my student at that time, Vincent Liu. So we decided to come up with a script that allows us to scan a book, SBA number. And the script is named Snapbook because at that time, Snapchat was quite popular. So we decided to come up with a very cool name called Snapchat. And the Snapchat is currently still running,
it's still on my phone. And currently I'm still using Snapbook to snap my books for my collection. Okay, so this is the kind of thing. So if you want to know some personal story about the Snapbook, you can reach out to me in the channels and then I can share you more
about this Snapbook story. Okay, basically I still have the code for that. Okay, so after the Snapbook, I find that a phone with so much sensors over that, it shouldn't be just used only for protocol scanning since we are allowed to access so many sensors over that.
So I decided to dig more further, okay. I decided to look at more further. I'll say, oh, okay, what can we see? So first thing that interests me is because of the accelerometer. So I decided to go for that, write a simple script that allows me to get a accelerometer. So in this case, when I measure that, well indeed it's kind of close to 9.81,
which normally what we knows about the accelerations of the gravitational acceleration, sorry, 9.81 meter per second per second, okay. So it's indeed very accurate in this case. So another question that pops up after I wrote the script is that I begin to wonder how Python's actually
in that app allows me to assess the Android API. And of course, another question is, if we write down that accelerometer and measures the acceleration of the gravity, how accurate or how precise the return data from that API. So I'll begin to look at that
when you look at back to the script that I have previously, okay. There's two files, or perhaps there are two modules that allows us to look at it, which is androidhelper.py and then android.py. So let's just look at the dissect of the Android helper. So for this class, let's focus one day
because they have a lot of things over there. So let's focus on one scan, scan barcodes method over there. So for this scan button method over there, there's one interesting line over there, return.rpc, scan barcode. Okay, now this is interesting.
Return RPC, interesting. So I begin to look at another file, which is android.py. So I'd like to know where does this RPC comes from? So when I look at this in details, there's much more interesting information
that I can find from here. First thing, it pass the JSON data that we know from here to the socket, okay. This is one thing. So in other words, it means that actually for Python script to talk to the Android API is through the socket.
They pass the JSON to the socket and there's another socket at the Android end that allows it to pass all the data in JSON form and then returns the API. So that's quite interesting. And of course, on the Android end, this is very simple because in this case, when we send the JSON data over,
it will just activate or you can say initiate the intent for scan barcode. So from that, it will return the result that it scans from the barcode and it returns the data back to the channel. So as a conclusion, what we have that is my Python script, let's say I write a Python script,
I'll send this my request or my data, my method as a data in JSON form to Android helper as RPC and then through the Android. And once Android receive the request in data in JSON format and that you process to read the data from the sensor. Once you got the data from the sensor
and then the same path, it goes back to this cycles and then this is how we got the data from the Python script. So what kind of API we have for that? As I mentioned, we have a story meter, just go location, Wi-Fi, Bluetooth, SMS, phone call and you name it. So there's interesting about here,
which is there's two patterns, the app that I actually recommend from that. And the one that we have is QPython. QPython is very interesting and basically you get two versions that you can download. One is from the Google App Store and the other one is from the GitHub website.
And if you want to have a hacker version where it allows you to access all the Android's API, I recommend you to download it from the GitHub. So you can just go to Google QPython 3 and then you can search for the GitHub for that. So pretty much you can get things from that. Okay, so what can we do with QPython in this case?
Since we mentioned that QPython is nearly the only app that I know that allows me to access the Android API. So for that, QPython currently runs at, the last one is 2.7 and 3.6. Of course, Snapple is based on 2.5 and 2.63,
which is written earlier ago. Then the only things that probably we start from, this QPython is simple, import Android Helper. And that's how we start from that. And by the word, the Chinese word we hear means is for Lao Zhi. It means that for every mouse that you move, it always starts from the first step. So everything starts from the first step,
import Android Helper is the first step. Now, the next slide that I'm going to show you over here is the traffic logger that I got from while I'm driving to my office. That was before the COVID time, as I want to try out for my application from that. So this is how I drive.
I drive, I drove all the way from this and each point, all this point are actually a point that I plot based on the data that I got from my phone. Okay, so this is all how. So the way that I'm doing, I did that was, I have my laptop together with me and start have my laptop collecting the data for my phone and then process it.
And then I plot it using a leaflet over here. So pretty much just how I do it. Okay, so right now I'm going to have a little bit of demo time. Okay, just allows me a little bit of time to show that. And first thing, this is one thing that I'll show you from my, okay, which is, and thank you.
Okay, so it just allows me to do this.
Okay, nevermind, just give me a little time.
Okay, that's interesting. Okay, nevermind, I'll show you another one.
Okay, so this is my leaflet place.
Okay, so right now this is my location which is near to my house, okay. So I'm going to get the location. So this is my house actually, previously my point.
And then right now I'm going to get the live GPS information for my phone. Okay, some phones with that. Not sure what's going on.
Okay, got it, okay. So this is right now the GPS location compared to the one that I have over here, okay.
So if you can see, this is the real data that I got from that. And you can also get the GPS as well as the locations. So if you look at that, this is point from the cube heighten as well. So right now allows me to demonstrate with another one.
So I hope this works.
Yes, got it, good. Well, if you look at that, this one.
So if you look at that right now, if I turn my phone, if I turn my phone around, okay. Well, of course my phone is not quite sensitive nowadays because I dropped it so many times, okay.
So if I rotate this guy over that. So this is kind of the real data or real time data that I got from the phone. So it will take according to what I move. Okay, rotate that, let's do it slowly.
Do it slowly again. So this is how I rotate and move around with this phone. Okay, yep, so this is how it works from that. Now, of course, I do not have the time for the SMS. So this is why I use another one which is GPS to represent that, okay.
So these are the reference. If you like to interested, you can look at more reference for the cube heighten and all the others are Android export. With that, I'd like to ask questions and thank you for the talk for today. Thank you, James. Any questions from the phone, any questions?
So I forgot you mentioned that, feel free to use the Q&A setting on Zoom. So far, we don't have any questions. We still have four minutes to go. Okay, so yeah, so if that is the case, then four minutes, I would like to use that to promote my event, Python APAC online 2020.
So as I mentioned before, you'll be on the 13th of September to 16th of September. So that's first thing. And currently it's online and we're going to start our ticket sales hopefully by next week. We still have some technicalities that to handle with. And for this time, this Python APAC 2020
is a little bit different compared to other Python. Reason for that is we have a lot of different varieties of the activities we did. So not to mention that we have the hackathon first thing. We have the hackathon, we have the tutorial, and we do have a conference
and we have panel discussion this time. Because we find that throughout the panel discussion, we can get different kind of people to come to speak to us and to exchange different ideas. So this is one thing that we try for this Python APAC online. And also one thing, same as EuroPython,
we did manage to invite Guido to come to us to speak to us. So that will be a session. So we do have that. And of course, we also managed to invite Keith Parker,
if you know that, who's the creator of the snake. We got him as well. And then we also got Audrey Rowe-Greenfield who's also the author for the Two School of Jungle. So if you know her. And yeah, so these are the few figures that we got for our Python APAC online 2020 this time.
So yeah, so do join us. Ticket will start selling. And the last update that I remember is the ticket is about five USD, okay? So things might change a little bit, but I'm not too sure. Hopefully it will still there, yeah. So this is kind of my event for the Python APAC 2020.
Yeah, so my time is two minutes. Thanks, James. There's still no questions. If there's any questions, go to the talk channel, which is hashtag talk Python on Android. So far, I didn't see any questions at the moment.
No, there are no questions. Yeah, there is a question, demo effect. Okay, okay. Oh, okay, so. No, it's not a question. So I'll move on to the next one. Thanks, James. Thank you, thank you, thank you, bye.