Header Ads

Game developer tip 2: Querying hardware capabilities

This is the second in a series of tips that supplement the Xperia™ game developer recommendations. This tip expands on Input recommendation IN5 in the list of game developer recommendations: A game should query the possible hardware capabilities of the platform to check if they exist on the current device. You can also download a PDF of this article.
Recommendation
A game should query the possible hardware capabilities of the platform to check if they exist on the current device.
Background
Android™ runs on platforms of varied capabilities and developers often assume that they have access to certain features, which might not be supported by all Android™ devices. This makes it essential for developers to query for hardware before actually trying to access a resource. Thus, it will also help them to avoid unnecessary crashes and make their applications compatible to majority of devices.
Many devices have special hardware capabilities, such as the touch pad in the Xperia™ Play or the pressure sensor in the Xperia™ active, which should be taken advantage of when developing applications. It’s important to enumerate the capabilities of the device at run time and not assume that certain capabilities exist.
Special considerations
Graphics-based applications should query, screen size, resolution, OpenGL capability, graphics processor, etc. Other sensor-based queries can be done using SensorManager.
Examples
Some of the hardware-oriented features available with the Android platform include:
android.hardware.Camera: A class that enables your application to interact with the camera to snap a photo, acquire images for a preview screen, and modify parameters used to govern how the camera operates.
android.hardware.SensorManager: A class that permits access to the sensors available within the Android platform. Sensor types include orientation, accelerometer, light, magnetic field, proximity and temperature. Not every Android-equipped device will support all of the sensors in the SensorManager.
android.hardware.SensorListener: An interface implemented by a class that wants to receive updates to sensor values as they change in real time. An application implements this interface to monitor one or more sensors available in the hardware.
android.media.MediaRecorder: A class, used to record media samples, that can be useful for recording audio activity within a specific location. Audio clippings can also be analyzed for identification purposes in an access-control or security application.
android.FaceDetector: A class that permits basic recognition of a person’s face as contained in a bitmap, essentially providing biometrics capability on a cell phone.
A sample code snippet for the sensor is below:
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
xViewA = (TextView) findViewById(R.id.xbox);
yViewA = (TextView) findViewById(R.id.ybox);
zViewA = (TextView) findViewById(R.id.zbox);
xViewO = (TextView) findViewById(R.id.xboxo);
yViewO = (TextView) findViewById(R.id.yboxo);
zViewO = (TextView) findViewById(R.id.zboxo);
}
public void onSensorChanged(int sensor, float[] values) {
synchronized (this) {
Log.d(tag, "onSensorChanged: " + sensor + ", x: " +
values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor == SensorManager.SENSOR_ORIENTATION) {
xViewO.setText("Orientation X: " + values[0]);
yViewO.setText("Orientation Y: " + values[1]);
zViewO.setText("Orientation Z: " + values[2]);
}
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
xViewA.setText("Accel X: " + values[0]);
yViewA.setText("Accel Y: " + values[1]);
zViewA.setText("Accel Z: " + values[2]);
}
}
}
public void onAccuracyChanged(int sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}
@Override
protected void onResume() {
super.onResume();
// register this class as a listener for the orientation and accelerometer sensors
sm.registerListener(this,
SensorManager.SENSOR_ORIENTATION |SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_NORMAL);
}

A code example for querying the version of OpenGL that is supported by a phone is below:
public int getGLVersion()
{
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo info = am.getDeviceConfigurationInfo();
return info.reqGlEsVersion;
}

The types of sensors that can be queried through SensorManager are described in the Android Developer Reference guide.
About the Game Developer Tips
The Xperia™ game developer recommendations offers guidance in developing games for Xperia™ devices such as Xperia™ PLAY. The tips in this series provide additional information about some of the significant recommendations in that document. Each tip provides the following information:
Background. This is a fuller description of the recommendation. It answers the questions “why should I follow this recommendation?” and “when or where should I follow this recommendation?”.
Special considerations. This describes any special considerations, such as device-specific considerations or application type-specific considerations that pertain to the recommendation.
Examples. This shows screen captures or code snippets that further illustrate or explain the recommendation.

No comments:

Powered by Blogger.