×
Namespaces

Variants
Actions
Revision as of 07:41, 3 October 2012 by hamishwillee (Talk | contribs)

Archived:Detecting device orientation using sensors in Flash Lite

From Nokia Developer Wiki
Jump to: navigation, search

This code snippet demonstrates how to display data of the orientation channel using the Sensor Platform Service for Flash Lite supported from S60 5th Edition onwards.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition and later
Symbian
S60 5th Edition
Article
Keywords: Service.Sensor, sensors.FindSensorChannel(), sensors.RegisterForNotification()
Created: User:Nokia Developer KB (26 Jan 2009)
Last edited: hamishwillee (03 Oct 2012)

Contents

Source

// Import Platform Service Interface
import com.nokia.lib.Service;
// Heading of the application
heading_txt.text = "Checking device orientation";
 
// Prevent scale of the stage
Stage.scaleMode = "noScale";
var screen;
 
// Create new Service object which has Sensor data
var sensors = new Service("Service.Sensor", "ISensor");
 
// Define input parameters for picking Rotation sensor
var inParam = {SearchCriterion:"Orientation"};
// Define result value
var outParams = sensors.FindSensorChannel(inParam);
// Define channel info (rotation)
var channelInfo = outParams.ReturnValue;
// Define valid values for channel info
var channelId = channelInfo[0].ChannelId;
var contextType = channelInfo[0].ContextType;
var quantity = channelInfo[0].Quantity;
var channelType = channelInfo[0].ChannelType;
var location = channelInfo[0].Location;
var vendorId = channelInfo[0].VendorId;
var dataItemSize = channelInfo[0].DataItemSize;
var channelDataTypeId = channelInfo[0].ChannelDataTypeId;
var channelInfo = {
ChannelId:channelId, ContextType:contextType, Quantity:quantity,
ChannelType:channelType, Location:location, VendorId:vendorId,
DataItemSize:dataItemSize, ChannelDataTypeId:channelDataTypeId
};
 
// Define input parameters for listening orientation channel data
var inParams = {ListeningType:"ChannelData", ChannelInfoMap:channelInfo};
 
// The RegisterForNotification method registers the user to receive data from
// one sensor channel asynchronously
sensors.RegisterForNotification(inParams, callBack);
 
// Because this is an asynchronous method you need to define callback function
// Callback function includes all the channel data
function callBack(transactionID:String, eventID:String, outParam:Object) {
if (outParam.ErrorCode == 0) {
var channelData = outParam.ReturnValue;
var deviceOr = channelData.DeviceOrientation;
// Build stage again when changing to vertical or horizontal screen
if(deviceOr == "DisplayUp") {
Stage.width = 360;
Stage.height = 640;
heading_txt._x = 31;
heading_txt._y = 20;
text_txt._x = 31;
text_txt._y = 100;
exit_mc._x = 360-84;
exit_mc._y = 640-47;
screen = "Vertical";
Stage.align = "TL";
} else if(deviceOr == "DisplayRightUp") {
Stage.width = 640;
Stage.height = 360;
heading_txt._x = 31;
heading_txt._y = 20;
text_txt._x = 31;
text_txt._y = 100;
exit_mc._x = 640-84;
exit_mc._y = 360-47;
screen = "Horizontal";
fscommand2( "FullScreen", true );
Stage.align = "TL";
}
text_txt.text = screen+" screen.\rOrientation: "+deviceOr;
} else {
var errorId = outParam.ErrorCode;
text_txt.text = "Error: "+errorId;
};
};

Postconditions

Status of the device orientation is displayed. The application layout will change between vertical(DisplayUp) and horizontal(DisplayRightUp) screen.

Example application

The following sample application has been tested in the Nokia 5800 XpressMusic (S60 5th Edition, Flash Lite 3.0): Checking Device Orientation

See also

68 page views in the last 30 days.
×