How to detect if sensor exist in the device that an app is running???

405 views
Skip to first unread message

JAUREGUI

unread,
Jul 30, 2015, 12:57:04 PM7/30/15
to AndroidScript
I try my app in 2 different tablets(Kindle fire and Dragon ,,,), one of the sensors I use was the Orientation sensor, the level worked fine (pitch and roll) in one tablet but the azimuth didn't work.
 it seems that the tablet doesn't have the compass function or different sensor.
also in the kindle tablet the .wav sound files didn't play. 

Is it a way to know if the sensor that my app uses the device has it and if the devise can play the sound or video format that the app uses?

Steve Garman

unread,
Jul 30, 2015, 1:01:30 PM7/30/15
to AndroidScript
Not sure exactly what strings you need to search for but try and.GetNames.

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

  sns = app.CreateSensor(  );
  lst = app.CreateList( sns.GetNames());
  lay.AddChild( lst );
  app.AddLayout( lay );
}

Timo Octazid

unread,
Jul 30, 2015, 1:03:52 PM7/30/15
to AndroidScript
Steve, your faster!

Hi,
Try sns.GetNames() to list all your sensors.

Many Greetings
Timo

JAUREGUI

unread,
Jul 30, 2015, 3:05:52 PM7/30/15
to AndroidScript, lrjau...@gmail.com
Thanks again for your help,  I tried in my phone and gave ma a bunch of sensors that looks interesting, now the big question is where can I
find some documentation for each sensor like variables or methods? 

I'll keep working with this, sorry for bother you but I might need your help later for sure, THANKS.

Steve Garman

unread,
Jul 30, 2015, 4:44:55 PM7/30/15
to AndroidScript
There is a list of types at
http://wiki.droidscript.me.uk/doku.php?id=built_in:sensors&s[]=sensor#description

Understanding the arguments in the callback takes a bit more digging as I don't know of any DroidScript documentation.

I look in the following page, in the "Fields" section at the field called "Values".
http://developer.android.com/reference/android/hardware/SensorEvent.html#values

It's not written with JavaScript in mind but it's the only reference I've used so far.

JAUREGUI

unread,
Aug 4, 2015, 4:57:31 PM8/4/15
to AndroidScript
thanks for your help I will check....

BareK

unread,
Feb 8, 2019, 10:09:59 AM2/8/19
to DroidScript
Using GetNames I found those sensors names/types:

Step Detector
Step Counter
Significant Motion Detector
Basic Gestures
Pedometer

But if I try to use them, logcat shows this: 

E SensorManager: sensor or listener is null

Here is an example:

function OnStart()
{    
   
var pedometer = app.CreateSensor( 'Step Counter' );
    pedometer
.SetOnChange( pedometer_OnChange );
    pedometer
.Start();
}

function pedometer_OnChange()
{    
    console
.log( 'pedometer_OnChange() called' );    
}

So is there a reliable way to determine weither or not a sensor is available on a device?

ProfH2SO4

unread,
Feb 9, 2019, 2:37:17 AM2/9/19
to DroidScript
Hi,
download bluetooth terminal and you will see if you are recieving data, In droidscript find function on bluetooth or if device is connected select ( "checked")

Dave

unread,
Feb 9, 2019, 8:39:04 AM2/9/19
to DroidScript
These are the keywords that work with app.CreateSensor()

"Accelerometer"
"MagneticField"
"Orientation"
"Light"
"Proximity"
"Temperature"
"GameRotation"
"GeomagneticRotation"
"Gravity"
"Gyroscope"
"HeartRate"
"Acceleration"
"Pressure"
"Humidity"
"Rotation"
"Motion"
"StepCounter"
"StepDetector"


You can also use the strings from the Android SDK such as the one given by Sensor.TYPE_AMBIENT_TEMPERATURE



Samuel Waller

unread,
Nov 29, 2021, 6:56:18 PM11/29/21
to DroidScript
Could you clarify how to use strings from the Android SDK? As in, could you give an example snippet of using Sensor.TYPE_AMBIENT_TEMPERATURE with app.CreateSensor()?

Steve Garman

unread,
Nov 30, 2021, 5:08:24 AM11/30/21
to DroidScript
You should be able to use the appropriate numeric constant for the sensor you need provided your device has access to that sensor


will lead you to 


and eventually to


which tells you it has a
Constant Value: 13

so you should be able to use:


//////////
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( 13 );
    sns.SetOnChange( sns_OnChange );
    sns.Start();

}

function sns_OnChange( v1,v2,v3, time )
{
    txt.SetText( "v1=" + v1 + "\n v2=" + v2 + "\n v3=" + v3 );
}
//////////


I don't have access to that sensor so, to test my code,  I looked up
and use constant value 1 instead of 13

Alan Hendry

unread,
Nov 30, 2021, 7:41:15 AM11/30/21
to DroidScript
HI,

If CreateSensor-GetNames doesn't return this sensor (and/or others) ...
I guess we can try to use the sensor (by number), 
and if the onchange hasn't fired in a reasonable time then assume it';s not there.
DS sensors only seem to have onchange.
Can we Set MinChange to zero to get an initial reading, then OnChange increase MinChange?

Sensors have .Method (not sure what we can do with it).

Regards, ah
Reply all
Reply to author
Forward
0 new messages