Getting sensors list?

Hello!
I'm trying to retrieve a list of all the sensors in the device but unable to do so.
Here's the code:

import android.content.Context;
import android.util.Log;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.ComponentCategory;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import java.util.Arrays;
import java.util.List;

@DesignerComponent(version = 1,  description = "Simple extension that returns all the available sensors in device. <br>Made by: Fahad Ahmad",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,   iconName = "http://appyBuilder.com/extensions/icons/extension.png")
@SimpleObject(external = true)
public class SensorsList extends AndroidNonvisibleComponent {
    private ComponentContainer container;
    private SensorManager sensorManager;
	
  
    public SensorsList(ComponentContainer container) {
        super(container.$form());
      	context = (Context) container.$context();
        this.container = container;
      	sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }
  
    @SimpleFunction(description = "Return list of available sensors.")
    public List<Sensor> getAllSensors() {
      	List<Sensor> deviceSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
      	return deviceSensors;
    	
    }
  
}

What am I doing wrong?

Hi @fahadboss10
An app Inventor block can return a very limited number of types, It can't return neither a list nor a Sensor So you can run a loop to get (for example) the name of every sensor as a string:

List<String> SensorsNames;
for (Sensor s : deviceSensors ){
SensorsNames.add(s.getName());
}

then you can get a YailList ( which is the app Inventor list) object from your list using:

YailList list = YailList.makeList(SensorNames);

You can find the YailList code here:


P.s : you should share your error log, so it's easier for u to know where is your problem :wink:

2 Likes

Thanks for the response @mohamed_tamer!
Here's the error log:

Buildfile: /projects/goldv2/appinventor-sources/appinventor/build.xml

extensions:

clean:
   [delete] Deleting directory /projects/goldv2/appinventor-sources/appinventor/components/build

init:

common_CommonUtils:

init:

CommonUtils:

common_CommonVersion:

init:

CommonVersion:
     [exec] Result: 128
     [exec] Result: 128

CopyToRunLibDir:

components_AndroidRuntime:

init:

CommonConstants:
    [javac] Compiling 6 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
    [javac] 1 warning

HtmlEntities:
    [javac] Compiling 1 source file to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
    [javac] 1 warning

common_CommonVersion:

init:

CommonVersion:
     [exec] Result: 128
     [exec] Result: 128

AndroidRuntime:
    [javac] Compiling 332 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
    [javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/fahadboss10/SensorsList/SensorsList.java:25: error: cannot find symbol
    [javac]       	context = (Context) container.$context();
    [javac]       	^
    [javac]   symbol:   variable context
    [javac]   location: class SensorsList
    [javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/fahadboss10/SensorsList/SensorsList.java:27: error: cannot find symbol
    [javac]       	sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    [javac]       	                                ^
    [javac]   symbol:   method getSystemService(String)
    [javac]   location: class SensorsList
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 2 errors
    [javac] 1 warning

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 7 seconds
ents/src/com/appybuilder/fahadboss10/SensorsList/SensorsList.java:25: error: cannot find symbol
    [javac]       	context = (Context) container.$context();
    [javac]       	^
    [javac]   symbol:   variable context
    [javac]   location: class SensorsList
    [javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/fahadboss10/SensorsList/SensorsList.java:27: error: cannot find symbol
    [javac]       	sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    [javac]       	                                ^
    [javac]   symbol:   method getSystemService(String)
    [javac]   location: class SensorsList
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 2 errors
    [javac] 1 warning

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 7 seconds

You should define The Context variable on order to use it:

private Context context;

You should cross getSystemService method with a context:

context.getSystemService(ar0);

,etc..

3 Likes

Thank you so much @mohamed_tamer for your detailed explaination!
Successfully compiled! :+1:

2 Likes

You can directly do that, you can refer more

Or you can use this way. :point_up_2: :point_up_2:

1 Like