This is an article published inKivyspacegameThe article on how to use python to access the sensor on your Android device. This tutorial is written for the development of Python mobile apps with Kivy . The Kivy runs very fast and is easy to use. Visit the blog for more tutorials on developing games with Kivy . You can also look at the Google Play Store helios:mining AdventureGame program.
This tutorial will focus on Plyer, a library that can read sensors, send emails, and turn text into speech, display notifications, and more. If you are using python to develop mobile applications, accessing the sensor will be a complex task. plyer greatly simplifies the work.
This tutorial covers installing plyer, building android packs using plyer , and lifting some display notifications to make the phone vibrate, allowing A small example of how Android devices speak to you.
(Operation on the mobile phone)
I. Background
Android includes a built-in API for accessing sensors . through python Access Java class is more complex and this process Pyjnius simplified. Pyjnius needs some extra errands to work with some features. For Apple devices, the Pyobjus is used . Plyer was created to simplify access to cell phone sensors and to use a platform-independent approach to Pythonic . The same plyer code can be run on the windows/linux/ios/android.
II. Installing the plyer
Under Terminal, run the following command:
sudo pip install git+ https://github.com/kivy/[email protected]
a message will inform you that the plyer has been installed successfully. You also want to make sure that the python-for-android with Plyer has been installed successfully. Otherwise, when you run your code on your phone, the program crashes because there is no Plyer library in your software package .
III. package with plyer
Unless you are already in plyer, Span style= "font-family: Arial" > Otherwise your package will not contain plyer plyer add to python-for-android :
Navigate to your python-for-android directory, for example :
Cd/home/kivy/android/python-for-android dist/
run with the correct parameters distribute.sh to include Plyer, as well as other things you need:
./distribute.sh-m plyer pyjnius jpeg png kivy
iv. code :
First, import the key modules:
Import kivykivy.require (' 1.8.0 ') from Kivy.app import appfrom kivy.uix.widget import widgetfrom Kivy.uix.label Import Labelfrom Kivy.core.window Import windowfrom plyer import notification, vibrator, TTS, email, accelerometerfrom kivy.cloc K Import Clock
Next, set up the graph:
#setup graphicsfrom kivy.config Import configconfig.set (' graphics ', ' resizable ', 0) #Graphics Fixfrom Kivy.core.window Import Window; Window.clearcolor = (0,0,0,1.)
for this example, there will be a main class for the android api connection. It contains a number of functions to implement this process.
Class GUI (widget): #this is the main Widget, which contains the game Def __init__ (self, **kwargs): Super (GUI, S ELF). __init__ (**kwargs) #add a label to advertise the blog L = label (text= ' kivyspacegame.wordpress.com ') l.x = WINDOW.WIDTH/2-L.WIDTH/2 l.y = WINDOW.HEIGHT/2 self.add_widget (l) #setup Accelerometer Try: #we ' ve already imported the accelerometer from Plyer #it ' s very easy to access Accelerometer.enable () except: #when You run the code on Linux, expect this to happen l.text = ' cant enable acceleromteer ' #setup acceleromter labels #We create a label to display accelerometer output Self.label = label (Text = ' Accelerometer: ') self.label.y = window.height*0.25 self.label.x = window.w idth*0.5 self.add_widget (Self.label) #setup timer to update accelerometer #we want to regularly read The Accelerometer Clock.schedule_interval (Self.check_accel, 1.0/60.0) #these four functions and other plyer features Android API Self.notify () self.vibrate () Self.talk () Self.email () def check_accel (SELF,DT): #update label try:self.txt = STR (round (accelerometer.acceleration[0],4) ) + ', ' + str (round (accelerometer.acceleration[1],4)) + ', ' + str (round (Acceleromet er.acceleration[2],4) self.label.text= ' accelerometer: ' + self.txt except: #expect this on linux self.label.text = ' cant read accelerometer ' def notify (self): t RY: #this notification would pop up on Ubuntu as well! ' Notification.notify (title= "Kivy notification", message= "Plyer Up and running!", app_name= "Kivy_test", app_icon= "icon. PNG ", timeout=10) except:print ' Error notifiying ' def vibrate (self): try: #the vibrator would only work on a vibrating device (ie Android) Vib Rator.vibrate (time=3) except:print ' ERROR vibrating ' Def talk (self): Try:tts.spea K (message= ' resistance is futile. Select an e-mail app. ' except:print ' cant talk ' def e-mail (self): Try:email.send ( Recipient = ' [email protected] ', subject = ' thanks! ', Text = ' enjoyed your lesson ') except: print ' Cant email '
The final code:
Class ClientApp (APP): def Build (self): #this are where the root widget goes #should be a canvas App = GUI () return app If __name__ = = ' __main__ ': ClientApp (). Run ()
v. Packaging the application
When your code is ready and running on Linux no problem, go to your Release directory packaging application:
Cd/home/kivy/android/python-for-android/dist/default
Then enter the following command, adjust the directory according to the actual situation. Be careful not to include unnecessary space in it. This command will install the application on your phone after the package is successful :
./build.py–dir/home/kivy/code/teachtocode/guestlessonsensors/–name "Plyertest" –package Com.molecularflowgames.plyertest–version 1.0–icon/home/kivy/code/teachtocode/guestlessonsensors/icon.png– Orientation landscape–permission Vibrate Debug Installd
This is an icon that can be used in your application
Original link: http://bytedebugger.wordpress.com/2014/07/06/guest-post-accessing-android-sensors-with-kivy-via-plyer/