python for android : 传感器

参考: file:///sl4a/doc/SensorManagerFacade.html

sensor.py

import android, time
droid = android.Android()
droid.startSensingTimed(1, 250)
time.sleep(1)
s1 = droid.readSensors().result
s2 = droid.sensorsGetAccuracy().result
s3 = droid.sensorsGetLight().result
s4 = droid.sensorsReadAccelerometer().result
s5 = droid.sensorsReadMagnetometer().result
s6 = droid.sensorsReadOrientation().result
droid.stopSensing()

print ‘s1=‘,s1
print ‘s2=‘,s2
print ‘s3=‘,s3
print ‘s4=‘,s4
print ‘s5=‘,s5
print ‘s6=‘,s6


在手机上测试了多次,观察数据后写出function

def sensor():
    droid.startSensingTimed(1, 250)
    time.sleep(1)
    s4 = droid.sensorsReadAccelerometer().result
    X,Y,Z =s4    
    if X<1.0 and Y>3.0 :
        droid.makeToast(‘portrait  纵向 ‘)
    if X>3.0 and Y<1.0 :
        droid.makeToast(‘landscape 横向 ‘)
    if X<1.0 and Y<1.0 :
        droid.makeToast(‘horizontal 水平‘)
    return



python for android : 传感器

上一篇:Android菜鸟的成长笔记(16)—— 再看Android中的Unbounded Service


下一篇:Android精品开源项目整理_V20140221(持续更新中..)