开发者

Find android device's direction/position relative to user?

Is it possible to find whether my device is pointing towards the sky or ahead by the way the user holds it?

I couldn't find any开发者_运维问答thing in the documentation by which I can know whether this is possible or not.

Thanks.


Yep, it's possible. What you're looking for is "orientation sensing." I found what looks like a good introduction to using orientation sensing here.

Also, here's the Android documentation.


The only thing you can detect - the orientation of the device. Use gyroscope API for this.

But please be aware - gyroscope doesn't present on all android devices.


For me Elixir was quite helpful. Under sensors tab it lists all the ones available on the device. By pressing teh play button, you can see the actual values the sensor receives. Great for getting the idea how the specific sensor works.

If you want to get information on the device's physical orientation, you can use 4 different sensors:

  • Sensor.TYPE_ACCELEROMETER (0, 0, 9.8)
  • Sensor.TYPE_GRAVITY (0, 0, 9.8)
  • Sensor.TYPE_ORIENTATION (x, 0, 0)
  • Sensor.TYPE_ROTATION_VECTOR (x, 0, 0)

Values in braces denote value of the values[] in SensoreEvent when device is facing the sky. x notes nonspecific value...

The values returned to your SensorEventListener impl are detailed here: http://developer.android.com/reference/android/hardware/SensorEvent.html#values


Apart from Gyroscope sensor u can also accelerometer sensor to find orientation but it returns m/s^2 in x,y,z direction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜