|
| 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. |
0 commit comments