Game Development Reference
In-Depth Information
Once we have the Context reference, we use it to obtain a reference to the Android
SensorManager class that is responsible for controlling input devices, including the
gyroscope. If no reference is available, a null reference will be returned.
The GetGyroscopeSensor method lets us check for the presence of a gyroscope by
requesting the SensorManager class for the default gyroscope handler. If a suitable
handler is not found (that is, a return value of null ), there is no gyroscope hardware
available on this device.
Now we can start implementing the API by looking at the GyroscopeSupported
method. This function needs to return true only if the device has gyroscope
hardware. We can do this as follows:
public boolean GyroscopeSupported()
{
Sensor lSensor = GetGyroscopeSensor();
return lSensor != null;
}
It is now time to implement the function that will allow us to start receiving
gyroscope data. Find the GyroscopeStart method and change it to the following
code snippet:
public void GyroscopeStart()
{
x = 0.0f;
y = 0.0f;
z = 0.0f;
mUsesDegrees = false;
Sensor lGyroscope = GetGyroscopeSensor();
if (lGyroscope != null)
{
mUsesDegrees = lGyroscope.getMaximumRange() > 100;
GetSensorManager().registerListener(this, lGyroscope,
SensorManager.SENSOR_DELAY_FASTEST);
}
}
In this method we start by ensuring that the cached gyroscope values are zero and we
assume that the device will return values in radians per second. We then obtain the
gyroscope's Sensor class instance using our private GetGyroscopeSensor method.
 
Search WWH ::




Custom Search