SymbiSoft / android-python27

Automatically exported from code.google.com/p/android-python27
0 stars 0 forks source link

Bluetooth-based Scripts cannot launch from APK #22

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Can't seem to get Bluetooth scripts (using either SL4A Bluetooth Serial, or 
PyBluez) to run when packaged as an APK, and I'm getting precious little 
feedback from Logcat what's going wrong.

Using the same Eclipse project I can successfully deploy and launch APKs 
containing hello.py scripts which are similar in their functionality except 
they do not use bluetooth. 

These BlueZ- and SL4A- bluetooth serial scripts work consistently when launched 
interactively or within SL4A, but they die when run from the APK environment.

Having failed to get SL4A bluetoothConnect(...) to work, (errors described at 
https://groups.google.com/forum/?fromgroups=#!topic/android-scripting/aCWUGHgLqL
k) I tried instead to use PyBluez functionality which is preferred. 

Once again this works when run from the Python console on the phone, or through 
SL4A, but does not launch when deployed as an APK.

To try and incorporate PyBluez support in the APK I took the contents of the 
egg file from 
https://code.google.com/p/python-for-android/downloads/detail?name=PyBluez-0.19-
py2.6-linux-armv.egg and extracted the files and folders into the path 
/python/lib/python2.7/dynload/ in the python_27.zip within the project. I don't 
know if this is the right place, but it seemed the most consistent. I tried 
extracting to some other paths in the supporting zips too, but no luck so far.

The moment I substitute a bluetooth-using scripts in the place of hello.py, 
LogCat simply reports that the python process has died, with the following 
lines after the installation and extraction of the APK...

03-15 18:25:35.432: V/sl4a.SimpleServer:225(21946): Bound to localhost/127.0.0.1
03-15 18:25:35.472: I/BlueshrimpAPK(21946): Executing 
/data/data/com.android.python27/files/python/bin/python with arguments 
[/data/data/com.android.python27/files/hello.py, --foreground] and with 
environment {ANDROID_SOCKET_zygote=10, 
AP_HANDSHAKE=e77ff97e-91e1-4a75-a0e8-1fba1f6fc6eb, ANDROID_BOOTLOGO=1, 
EXTERNAL_STORAGE=/mnt/sdcard, ANDROID_CACHE=/cache, ANDROID_ASSETS=/system/app, 
PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, 
ASEC_MOUNTPOINT=/mnt/asec, 
PYTHONPATH=/mnt/sdcard/com.android.python27/extras/python:/data/data/com.android
.python27/files/python/lib/python2.7/lib-dynload:/data/data/com.android.python27
/files/python/lib/python2.7, AP_HOST=127.0.0.1, LOOP_MOUNTPOINT=/mnt/obb, 
TEMP=/mnt/sdcard/com.android.python27/extras/tmp, 
BOOTCLASSPATH=/system/framework/core.jar:/system/framework/bouncycastle.jar:/sys
tem/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.
policy.jar:/system/framework/services.jar:/system/framework/core-junit.jar, 
SD_EXT_DIRECTORY=/sd-ext, AP_PORT=46999, DOWNLOAD_CACHE=/cache/download, 
ANDROID_DATA=/data, PYTHONHOME=/data/data/com.android.python27/files/python, 
LD_LIBRARY_PATH=/data/data/com.android.python27/files/python/lib:/data/data/com.
android.python27/files/python/lib/python2.7/lib-dynload, ANDROID_ROOT=/system, 
ANDROID_PROPERTY_WORKSPACE=9,32768}
03-15 18:25:35.472: D/dalvikvm(21946): Trying to load lib 
/data/data/com.android.python27/lib/libcom_googlecode_android_scripting_Exec.so 
0x405183d0
03-15 18:25:35.482: D/dalvikvm(21946): Added shared lib 
/data/data/com.android.python27/lib/libcom_googlecode_android_scripting_Exec.so 
0x405183d0
03-15 18:25:35.482: D/dalvikvm(21946): No JNI_OnLoad found in 
/data/data/com.android.python27/lib/libcom_googlecode_android_scripting_Exec.so 
0x405183d0, skipping init
03-15 18:25:40.122: D/BlueshrimpAPK(21946): Process 22083 exited with result 
code 1.
03-15 18:25:40.223: I/Process(21946): Sending signal. PID: 21946 SIG: 9
03-15 18:25:40.252: I/ActivityManager(231): Process com.android.python27 (pid 
21946) has died.

Original issue reported on code.google.com by c...@cefn.com on 15 Mar 2013 at 6:29

GoogleCodeExporter commented 8 years ago
For source, an example which fails is 
https://github.com/cefn/blueshrimp/blob/master/Androino/bluezsensordisplay/bluez
sensordisplay.py 
...which relies on metaserial to create a consistent wrapper around Bluetooth, 
PySerial or SL4ASerial...
https://github.com/cefn/blueshrimp/tree/master/MetaSerial/metaserial

This code works when launched in SL4A, but I just need to know how to package 
pybluez and metaserial and configure the APK so it will play ball with 
Bluetooth.

Original comment by c...@cefn.com on 15 Mar 2013 at 6:37

GoogleCodeExporter commented 8 years ago
There is no bluetooth module included in the Python build, so you hit an 
ImportError which kills the process.

You would need either to add a bluetooth module in the Python build of this 
project, or use PY4A Python build in this project as explained here: 
https://code.google.com/p/android-python27/wiki/TutorialHowToChangePythonBuilds

Original comment by anthony....@gmail.com on 18 Mar 2013 at 6:17

GoogleCodeExporter commented 8 years ago
NB: you cannot use PyBluez-0.19-py2.6-linux-armv.egg as it's linked against 
libpython2.6.so (and this project uses Python 2.7 by default)

Original comment by anthony....@gmail.com on 18 Mar 2013 at 6:20

GoogleCodeExporter commented 8 years ago
That's incredibly useful to learn. 

I hadn't recognised the hard dependency on the python version (or figured the 
importance of different version numbers between Py4A and the canonical APK 
build). That's probably why it was dying all the time. I tried adding the .egg 
contents to a variety of locations within the zips representing the python 
build in the APK, but it would never work without the right version.

I'm trying to apply your suggestion right now - rolling back the version of the 
python builds to 2.6 and then extracting the .egg contents into 
python_extras_r14.zip under the path /python. Does anything else need updating 
to notify that pybluez support is there?

If this works and I can indeed carry on using Py4A for prototyping and 
deployment, it raises a number of questions if anyone has time to help.

Is there any way I could have seen the ImportError as I couldn't figure out 
what the failure condition actually was in the python engine. I must have been 
looking in the wrong place but don't know where the relevant logging goes to. 
There didn't seem to be any error condition as such in the Logcat, which is 
emphasised for bug reporting.

Does the fact Google is migrating away from Bluez (announced at 
http://developer.android.com/about/versions/jelly-bean.html) mean that PyBluez 
is never going to work beyond JellyBean 4.2 so I should immediately be working 
out how to phase it out?

=== Separate Bug/Feature ===

I wouldn't need to use PyBluez at all, if it wasn't that the SL4A 
BluetoothFacade triggers bugs (possibly mine) but only when I deploy in an APK. 
This could potentially be associated with the version too? 

From the bug I experience (connid not ready) it could be that the blocking 
behaviour promised by bluetoothConnect in newer versions of the SL4A facades 
http://www.mithril.com.au/android/doc/BluetoothFacade.html isn't actually 
happening in the APKed version, perhaps because I'm not getting the script.jar 
from the correct/latest source somehow. If it doesn't block, then I'm going 
straight to accessing an unready connection. 

Alternatively it's just failing to connect in the APKed version for some 
different reason, even whilst the console-run version works fine. Are there 
differences between the deployment environments which could explain the same 
SL4A code which works on the console failing on the APK version (originally I 
wasn't using PyBluez at all).

In any case, this SL4A Bluetooth Facade issue is probably a completely 
different bug/feature. Let me know if I should re-file this issue separately.

Original comment by c...@cefn.com on 18 Mar 2013 at 8:16

GoogleCodeExporter commented 8 years ago
>Is there any way I could have seen the ImportError as I couldn't figure 
>out what the failure condition actually was in the python engine. I must
>have been looking in the wrong place but don't know where the relevant 
>logging goes to. There didn't seem to be any error condition as such in
>the Logcat, which is emphasised for bug reporting.

You should run the script from the command line. Once the apk is installed you 
have all the Python file you need for that, just export the env vars as here: 
https://code.google.com/p/android-python27/wiki/TutorialHowToRunPythonfromShell

Original comment by anthony....@gmail.com on 19 Mar 2013 at 9:18

GoogleCodeExporter commented 8 years ago
I find that BluetoothDeviceList activity [in script.jar] can not be started 
normally, but do't know the reason. 
I try to add another BluetoothDeviceList activity [from SL4A source code] to 
the project and start this activity from bluetoothConnect() in 
BluetoothFacade.java [also from SL4A, compile new ones to replace the old ones 
in script.jar] then Bluetooth can work.

Original comment by chunglon...@gmail.com on 19 Jul 2013 at 6:23