I can't figure out how to access the led light on my android with python or kivy, I have tried installing python-for-android to be able to import the android module into my code but it's not the module can't be found. I cloned python-for-android as instructed here. I didn't install the ndk or sdk as per that page as I thought since kivy already uses them they were already installed. Can someone please point me in the right direction?
Asked
Active
Viewed 3,939 times
2
-
You can only import the android module when running on android. – inclement Jan 23 '15 at 14:15
-
So, is it possible to access the led flash functionality via kivy/python from my desktop? Or I should say, is it possible to write such an app with kivy, say a flashlight app. – Totem Jan 23 '15 at 14:20
1 Answers
4
Yes, you can write this app in Kivy from the desktop, you just won't be able to test it on the desktop. You will have to build and deploy to an Android device to test each time.
Adapted from How to turn on camera flash light programmatically in Android?:
To check if flash capability is available:
PythonActivity = autoclass('org.renpy.android.PythonActivity')
PackageManager = autoclass('android.content.pm.PackageManager')
pm = PythonActivity.mActivity.getPackageManager()
flash_available = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
To use the flashlight, your app will need the FLASHLIGHT and CAMERA permissions. You can add these to buildozer.spec or the python-for-android command line.
Finally, to turn flash on:
Camera = autoclass('android.hardware.Camera')
CameraParameters = autoclass('android.hardware.Camera$Parameters')
cam = Camera.open()
params = cam.getParameters()
params.setFlashMode(CameraParameters.FLASH_MODE_TORCH)
cam.setParameters(params)
cam.startPreview()
And off:
cam.stopPreview()
cam.release()
-
I am assuming I will need to install some additional modules for this to work, as I've never seen autoclass before. Could you tell me what these are? – Totem Jan 24 '15 at 13:10
-
1`autoclass` is part of pyjnius (`from jnius import autoclass`). It is built and added to your APK automatically by python-for-android. pyjnius is a system to access Java classes from Python, in this case allowing us to call the Android API from Kivy. You can also install pyjnius on the desktop, but you will not be able to access these same classes because they are part of the Android API. – kitti Jan 26 '15 at 15:56