I used the following script to attempt to retrieve magnetic sensor information:
import android, time
droid = android.Android()
droid.startSensingTimed(1, 250)
time.sleep(1)
s5 = droid.sensorsReadMagnetometer().result
droid.stopSensing()
droid.setClipboard(s5)
I’m no Python programmer, but it seems to me a straightforward combination of these two script examples:1 – script example for retrieving sensor info into Python variablesLhttp://www.mithril.com.au/android/doc/SensorManagerFacade.html
2 - script from Baudi for putting something into the clipboard:
http://tasker.wdfiles.com/local--files/filetoclip/CopyFileToClipboard.py
The problem is, the clipboard comes up empty.I don’t think it’s my installation... I can run the example scripts like “hello_world.py” just fine either from tasker or from sl4aI put my above script (magnetic.py) into the same directory as “hello_world.py” and it doesn’t work in either tasker or from sl4aI also tried starting a separate compass application “super compass” (in order to ensure the sensor was putting out values) and then repeated the test... same result. Any ideas why this didn’t work? Is there another way to pass a Python
variable back to Tasker other than thru the clipboard?>>> import android, time
>>> droid = android.Android()
>>> droid.startSensingTimed(1,250)
import androidimport timedroid = android.Android()droid.startSensingTimed(3, 250)
time.sleep(1)s5 = droid.sensorsReadMagnetometer().resultdroid.stopSensing()
s5str = str(s5)droid.setClipboard(s5str)droid.makeToast("completed putting mag field into clipboard")
If anyone’s interested, the final code I used to determine heading as angle measured CW from North:
Tasker Routine:
A1: Run SL4A Script [ Name:ReadOrientation.py Terminal:Off Pass Variables: ]
A2: Wait [ MS:0 Seconds:4 Minutes:0 Hours:0 Days:0 ]
A3: Variable Set [ Name:%Heading To:%CLIP Do Maths:Off Append:Off ]
A4: Variable Split [ Name:%Heading Splitter:[ Delete Base:On ]
A5: Variable Split [ Name:%Heading2 Splitter:, Delete Base:Off ]
A6: Variable Set [ Name:%HeadingDeg To:%Heading21 * 180/3.14159 Do Maths:On Append:Off ]
A7: Popup [ Title: Text:%HeadingDeg Background Image: Layout:Popup Timeout (Seconds):17 Show Over Keyguard:On ]
The python script ReadOrientation.py is as follows:
import android
import time
droid = android.Android()
droid.startSensingTimed(1, 250)
time.sleep(1)
s5 = droid.sensorsReadOrientation().result
droid.stopSensing()
s5str = str(s5)
droid.setClipboard(s5str)
droid.makeToast("completed putting orientation into clipboard")
Notes:
1 – I changed sensorsReadMagnetometer().result to sensorsReadOrientation().result. The latter is the one that is most suitable for telling us the heading.
2 – The “wait” statement is necessary because otherwise Tasker proceeds without waiting for the SL4A script to finish and you’ll get an old value from the previous time the routine was run. It’s odd that the steps aren’t sequential but apparenlty Tasker just launches the script and doesn’t wait for it to finish before proceeding to the next step
3 – The heading is based on the position of the phone, but is valid as you change positions of the phone. Let me say it another way. Assune say you’re standing facing a given direction with your phone directly in front of you. Then it tells you the direction that you are facing and it gives the same correct direction regardless of whether you are holding the phone perpendicular to your body (phone face up) or parallel to your body (phone facing you).