使用kotlin的Android坠落检测:问题
我正在尝试使用android的加速计实现一个简单的坠落检测算法使用kotlin的Android坠落检测:问题,android,kotlin,accelerometer,sensors,Android,Kotlin,Accelerometer,Sensors,我正在尝试使用android的加速计实现一个简单的坠落检测算法 override fun onSensorChanged(event: SensorEvent?) { x=event!!.values[0].toDouble() y=event!!.values[1].toDouble() z=event!!.values[2].toDouble() var sqroot= sqrt(x.pow(2) + y.pow(2)+z.pow(2)) te
override fun onSensorChanged(event: SensorEvent?)
{
x=event!!.values[0].toDouble()
y=event!!.values[1].toDouble()
z=event!!.values[2].toDouble()
var sqroot= sqrt(x.pow(2) + y.pow(2)+z.pow(2))
textView.setText("x= ${event!!.values[0]}\n\n"+"y= ${event!!.values[1]}\n\n\n"+
"z= ${event!!.values[2]}"+"\n\n\n acceleration=$sqroot")
if (sqroot<3 && minv==false && status==false)
{
minv=true
lasttime=System.currentTimeMillis()
Log.i("min thresholds","free fall to ground $sqroot" )
Toast.makeText(this,"free fall to ground $sqroot",Toast.LENGTH_SHORT).show()
}
if (minv)
{
counter++
if (sqroot>=30 && maxv==false && status==false) {
newtime = System.currentTimeMillis()
diff = newtime - lasttime
Toast.makeText(this,"last:$lasttime && new:$newtime \n diff:$diff\n\n $sqroot",Toast.LENGTH_LONG).show()
if (diff > 150 && diff<9000)
{
maxv = true
Log.i("hit the ground", "hiting to ground $sqroot")
Toast.makeText(this, "hit to ground $sqroot", Toast.LENGTH_SHORT).show()
status = true
}
}
}
if(maxv==true && minv==true && status==true)
{
Log.i("fall detected ","fall detected $sqroot")
Toast.makeText(this,"fall detected $sqroot",Toast.LENGTH_SHORT).show()
textView2.text="x=$x\ny=$y\nz=$z \n acc=$sqroot \n\n\n last:$lasttime && new:$newtime \n" +
" diff:$diff"
x=0.0
y=0.0
z=0.0
minv=false
maxv=false
status=false
}
if (counter>10)
{
counter=0
minv=true
maxv=true
}
}
override fun onSensorChanged(事件:SensorEvent?)
{
x=event!!.values[0].toDouble()
y=event!!.values[1].toDouble()
z=event!!.values[2].toDouble()
var sqroot=sqrt(x.pow(2)+y.pow(2)+z.pow(2))
textView.setText(“x=${event!!.values[0]}\n\n”+“y=${event!!.values[1]}\n\n”+
“z=${event!!.values[2]}”+“\n\n\n加速度=$sqroot”)
if(sqroot=30&&maxv==false&&status==false){
newtime=System.currentTimeMillis()
差异=新时间-上次时间
Toast.makeText(这是“last:$lasttime&&new:$newtime\n diff:$diff\n\n$sqroot”,Toast.LENGTH\u LONG.show()
如果(差异>150和&diff10)
{
计数器=0
minv=true
maxv=true
}
}
我使用简单的阈值系统来检测时间系统上的跌落和加法意味着我跌落到地面的自由时间差大于900毫秒,您可以尝试此库: 用法:
FreeFallService.startService(context,
object : OnSensorChanged {
override fun onFall(fallObject: FallObject) {
// code when free fall is detected
}
},
object : OnFallsListFetch {
override fun onFallsFetch(fallsList: ArrayList<FallObject>) {
// handling call for fetching all events from the db
}
})
FreeFallService.startService(上下文、,
对象:OnSensorChanged{
覆盖秋天的乐趣(法对象:法对象){
//检测到自由下落时的代码
}
},
对象:OnFallsListFetch{
覆盖FallsFetch(fallsList:ArrayList){
//处理从数据库获取所有事件的调用
}
})
那么,您能告诉我如何准确地识别坠落检测代码吗?您认为这在三星手表上也适用吗?