Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a923a32

Browse files
committed
extends.rst
cleaned up and integrated the explanation of how to extend python for android's access to native android api.
1 parent bffaf4d commit a923a32

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

docs/source/extend.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Extending Python for android native support
2+
===========================================
3+
4+
So, you want to get into python-for-android and extend what's available
5+
to python on android ?
6+
7+
Turns out it's not very complicated, here is a little introduction on how to go
8+
about it.
9+
10+
Theory
11+
------
12+
13+
Think about acceleration sensors : you want to get the acceleration values in
14+
python nothing is available natively, but you have a java API for that : the
15+
google API is available here
16+
http://developer.android.com/reference/android/hardware/Sensor.html
17+
18+
You can't use it directly, you need to do your own API to use it in python,
19+
this is done in 3 steps
20+
21+
Step 1 : write the java code to create very simple functions to use
22+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23+
24+
like : accelerometer Enable/Reading
25+
In our project, this is done in the Hardware.java:
26+
https://github.com/kivy/python-for-android/blob/master/src/src/org/renpy/android/Hardware.java
27+
you can see how it's implemented
28+
29+
Step 2 : write a jni wrapper
30+
++++++++++++++++++++++++++++
31+
32+
This is a C file to be able to invoke/call Java functions from C, in our case,
33+
step 2 (and 3) are done in the android python module. The JNI part is done in
34+
the android_jni.c:
35+
https://github.com/kivy/python-for-android/blob/master/recipes/android/src/android_jni.c
36+
37+
Step 3 : you have the java part, that you can call from the C
38+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
39+
40+
You can now do the Python extension around it, all the android python part is
41+
done in
42+
https://github.com/kivy/python-for-android/blob/master/recipes/android/src/android.pyx
43+
44+
→ [python] android.accelerometer_reading ⇒ [C] android_accelerometer_reading
45+
⇒ [Java] Hardware.accelerometer_reading()
46+
47+
The jni part is really a C api to call java methods. a little bit hard to get
48+
it with the syntax, but working with current example should be ok
49+
50+
51+
Example with bluetooth
52+
----------------------
53+
54+
Start directly from a fork of https://github.com/kivy/python-for-android
55+
56+
The first step is to identify where and how they are doing it in sl4a, it's
57+
really easy, because everything is already done as a client/server
58+
client/consumer approach, for bluetooth, they have a "Bluetooth facade" in
59+
java.
60+
61+
http://code.google.com/p/android-scripting/source/browse/android/BluetoothFacade/src/com/googlecode/android_scripting/facade/BluetoothFacade.java
62+
63+
You can learn from it, and see how is it's can be used as is, or if you can
64+
simplify / remove stuff you don't want.
65+
66+
From this point, create a bluetooth file in
67+
python-for-android/tree/master/src/src/org/renpy/android in Java.
68+
69+
Do a good API (enough simple to be able to write the jni in a very easy manner,
70+
like, don't pass any custom java object in argument).
71+
72+
Then write the JNI, and then the python part.
73+
74+
3 steps, once you get it, the real difficult part is to write the java part :)
75+
76+
Jni gottchas
77+
------------
78+
79+
- package must be org.renpy.android, don't change it.

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ your application.
2525
recipes.rst
2626
android.rst
2727
related.rst
28+
extend.rst
2829
faq.rst
2930

3031
Indices and tables

0 commit comments

Comments
 (0)