Back

CreateSensor

Hello World
Content:
- Methods

The sensor object can be used to access numerous sensors of your device.

sns = app.CreateSensor( type, options ) → app object: Sensor

You can use the SetOnChange function of the Sensor to set the name of a function you want to be called when a the changes occur.

Change the rate that a sensor checks for changes by adding one the options “Fastest”, “Fast”, “Medium” or “Slow”. “Slow” is the default.

Example - GetNames

function OnStart()
{
    sns = app.CreateSensor();
    var names = sns.GetNames();
    app.Alert(names.replace(/,/g, ",\n"), "Sensor Names");

}
    Copy     Copy All       Run      

Example - Accelerometer

function OnStart()
{
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

    txt = app.CreateText( "", 0.8, 0.3, "Multiline" );
    lay.AddChild( txt );
    app.AddLayout( lay );

    sns = app.CreateSensor( "Accelerometer" );
    sns.SetOnChange( sns_OnChange );
    sns.Start();


}

function sns_OnChange( x, y, z, time )
{
    txt.SetText( "x=" + x + "\n y=" + y + "\n z=" + z );
}
    Copy     Copy All       Run      

Example - Orientation

function OnStart()
{
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

    txt = app.CreateText( "", 0.8, 0.3, "Multiline" );
    lay.AddChild( txt );
    app.AddLayout( lay );

    sns = app.CreateSensor( "Orientation" );
    sns.SetOnChange( sns_OnChange );
    sns.Start();


}

function sns_OnChange( azimuth, pitch, roll, time )
{
    var msg = " azimuth = " + azimuth.toFixed(1);
    msg += "\n pitch = " + pitch.toFixed(1);
    msg += "\n roll = " + roll.toFixed(1);
    txt.SetText( msg );
}
    Copy     Copy All       Run      

Example - Light

function OnStart()
{
    lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

    txt = app.CreateText( "", 0.8, 0.3 );
    lay.AddChild( txt );
    app.AddLayout( lay );

    sns = app.CreateSensor( "Light" );
    sns.SetOnChange( sns_OnChange );
    sns.Start();

}

function sns_OnChange( lux )
{
    txt.SetText( "level = " + lux + " lux" );
}
    Copy     Copy All       Run      

Methods

The following methods are available on the Sensor object:

GetAzimuth() → Number
GetNames() → String: comma “,” separated
GetPitch() → Number
GetRoll() → Number
GetType() → String: “Sensor”
GetValues() → List: [ time, first, second, third ]
Method( name, types, p1, p2, p3, p4 ) → all types
Start()
Stop()
Number
String
Number: milliseconds
String: “Accelerometer” or “MagneticField” or “Orientation” or “Light” or “Proximity” or “Temperature” or “GameRotation” or “GeomagneticRotation” or “Gravity” or “Gyroscope” or “HeartRate” or “Acceleration” or “Pressure” or “Humidity” or “RotationMotion” or “StepCounter” or “StepDetector”
String: comma “,” separated: “Slow” or “Medium” or “Fast” or “Fastest”
Object: { COMMAND }
List: boolean,char,byte,short,int,long,float,double,String,CharSequence,...
function( first, second, third, time )
sns.Batch
Batch method calls to be able to set all object's properties at once.
Note that you need to specify each parameter (use “” or null to leave some out)
Inherited methods can be called by appending an underscore to the function name (ie. txt.Batch({ SetBackColor_: [“red”] })
sns.GetAzimuth
Returns the first/x/azimuth value of a sensor.
sns.GetNames
Returns a list of builtin sensors in your device.
sns.GetPitch
Returns the second/y/pitch value of a sensor.
sns.GetRoll
Returns the third/z/roll value of a sensor.
sns.GetType
Returns the control class name.
sns.GetValues
Returns all values of a sensor.
sns.Method
Allows access to other functions defined on the object in Java via reflection.

Note: This function is a premium feature. Please consider subscribing to Premium to use this feature and support DroidScript in its further development.
sns.SetMaxRate
Define a minimum timeout between two OnChage calls.
sns.SetMinChange
Define a minimum threshold value which triggers a OnChange call.
sns.SetOnChange
Define a callback function which is called when a sensor value has changed.
sns.Start
Start reading from the sensor.
sns.Stop
Stop reading from the sensor.