Java Code Examples for android.hardware.SensorManager#getSensorList()

The following examples show how to use android.hardware.SensorManager#getSensorList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From batteryhub with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    LogUtils.logI(TAG, "onCreate() called");

    loadComponents();

    Intent intentFromNotifier = getIntent();

    if (intentFromNotifier != null) {
        int tab = intentFromNotifier.getIntExtra("tab", -1);
        if (tab != -1) {
            mViewPager.setCurrentItem(tab);
        }
    }
    mSensorManager = (SensorManager) getBaseContext().getSystemService(Context.SENSOR_SERVICE);
    mSensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
}
 
Example 2
Source File: SensorUtil.java    From Sensor-Disabler with MIT License 6 votes vote down vote up
public static Sensor getSensorFromUniqueSensorKey(Context context, String key) {
    String[] data = key.split(Pattern.quote(SENSOR_SEPARATOR)); //Regex, man. Regex.
    if (data.length >= 4) {
        try {
            String name = data[0];
            String vendor = data[1];
            int version = Integer.parseInt(data[2]);
            int type = Integer.parseInt(data[3]);
            SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
            for (Sensor sensor : manager.getSensorList(type)) {
                if (sensor.getName().equals(name)
                        && sensor.getVendor().equals(vendor)
                        && sensor.getVersion() == version) {
                    return sensor;
                }
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Unable to get unique sensor from key.");
        }
    } else {
        Log.e("SensorUtil", "Unable to parse \"" + key + "\" as Sensor data.");
    }
    return null;
}
 
Example 3
Source File: MainActivity.java    From wearable with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setAmbientEnabled();

    mContainerView = (BoxInsetLayout) findViewById(R.id.container);
    mTextView = (TextView) findViewById(R.id.text);
    mData = (TextView) findViewById(R.id.data);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    //write out to the log all the sensors the device has.
    sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
    if (sensors.size() < 1) {
        Toast.makeText(this, "No sensors returned from getSensorList", Toast.LENGTH_SHORT).show();
        Log.wtf(TAG,"No sensors returned from getSensorList");
    }
    Sensor[] sensorArray = sensors.toArray(new Sensor[sensors.size()]);
    for (int i = 0; i < sensorArray.length; i++) {
        Log.wtf(TAG,"Found sensor " + i + " " + sensorArray[i].toString());
    }
}
 
Example 4
Source File: ListSensorFragment.java    From android-sensor-example with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // we need to retrieve the system service on the parent activity
    mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

    // this is how to list all sensors
    deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);

    // check for a specific type of sensor
    //if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
        // Success! There's a magnetometer.
    //}
    //else {
        // Failure! No magnetometer.
    //}

}
 
Example 5
Source File: AccelerometerManager.java    From Women-Safety-App with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true if at least one Accelerometer sensor is available
 */
public static boolean isSupported(Context context) {
    aContext = context;
    if (supported == null) {
        if (aContext != null) {
             
             
            sensorManager = (SensorManager) aContext.
                    getSystemService(Context.SENSOR_SERVICE);
             
            // Get all sensors in device
            List<Sensor> sensors = sensorManager.getSensorList(
                    Sensor.TYPE_ACCELEROMETER);
             
            supported = new Boolean(sensors.size() > 0);
             
             
             
        } else {
            supported = Boolean.FALSE;
        }
    }
    return supported;
}
 
Example 6
Source File: MainWearActivity.java    From wear-os-samples with Apache License 2.0 6 votes vote down vote up
public void onClickWearBodySensors(View view) {

        if (mWearBodySensorsPermissionApproved) {

            // To keep the sample simple, we are only displaying the number of sensors.
            SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
            int numberOfSensorsOnDevice = sensorList.size();

            logToUi(numberOfSensorsOnDevice + " sensors on device(s)!");

        } else {
            logToUi("Requested local permission.");
            // On 23+ (M+) devices, GPS permission not granted. Request permission.
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.BODY_SENSORS},
                    PERMISSION_REQUEST_READ_BODY_SENSORS);
        }
    }
 
Example 7
Source File: FtcTestAndroidSensors.java    From FtcSamples with MIT License 5 votes vote down vote up
@Override
    public void initRobot()
    {
        hardwareMap.logDevices();
        dashboard = HalDashboard.getInstance();
        FtcRobotControllerActivity activity = (FtcRobotControllerActivity)hardwareMap.appContext;
        dashboard.setTextView((TextView)activity.findViewById(R.id.textOpMode));
        //
        // Enumerates all Android sensors.
        //
        SensorManager sensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
        List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
        dashboard.displayPrintf(0, "Android Sensors:");
        int lineNum = 1;
        for (Sensor sensor: sensorList)
        {
            dashboard.displayPrintf(lineNum, "%02d->%s, %s, %d",
                                    sensor.getType(), sensor.getVendor(), sensor.getName(), sensor.getVersion());
            lineNum++;
            if (lineNum >= dashboard.getNumTextLines())
            {
                break;
            }
        }

        //
        // Create Android sensors.
        //
        accel = FtcAndroidSensor.createInstance("accel", Sensor.TYPE_ACCELEROMETER, 3);
        gravity = FtcAndroidSensor.createInstance("gravity", Sensor.TYPE_GRAVITY, 3);
        gyro = FtcAndroidSensor.createInstance("gyro", Sensor.TYPE_GYROSCOPE, 3);
        linearAccel = FtcAndroidSensor.createInstance("linearAccel", Sensor.TYPE_LINEAR_ACCELERATION, 3);
        rotation = FtcAndroidSensor.createInstance("rotation", Sensor.TYPE_ROTATION_VECTOR, 4);
        magnetic = FtcAndroidSensor.createInstance("magnetic", Sensor.TYPE_MAGNETIC_FIELD, 3);
//        orientation = FtcAndroidSensor.createInstance("orientation", Sensor.TYPE_ORIENTATION, 3);
        proximity = FtcAndroidSensor.createInstance("proximity", Sensor.TYPE_PROXIMITY, 1);
        light = FtcAndroidSensor.createInstance("light", Sensor.TYPE_LIGHT, 1);
    }
 
Example 8
Source File: DiagnosticsActivity.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
private void probeOrientation() {
    StringBuilder sb = new StringBuilder();
    SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
    for (Sensor s : sensorList) {

        sb.append(s.getName() + ":" + s.toString() + "\n");
    }
    output.setText(sb.toString());
}
 
Example 9
Source File: TemperatureActivity.java    From LLApp with Apache License 2.0 5 votes vote down vote up
@Override
protected void onInitView() {

    if(humidityView != null){
        humidityView.setPanelText("当前湿度");
        humidityView.setUnit(" %");
        humidityView.setTikeCount(40);
    }

    //获取传感器
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    //获取温度传感器
    mTemperaSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
    //获取湿度传感器
    mHumiditySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY);

    List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    for (Sensor sensor : deviceSensors) {
        Log.i("sensor", "------------------");
        Log.i("sensor", sensor.getName());
        Log.i("sensor", sensor.getVendor());
        Log.i("sensor", Integer.toString(sensor.getType()));
        Log.i("sensor", "------------------");
    }

    if(mTemperaSensor == null){
        ToastUtil.showToast("您的设备暂不支持温度传感器功能");
    }

    if(mHumiditySensor == null){
        ToastUtil.showToast("您的设备暂不支持湿度传感器功能");
    }

    //给传感器注册一个时间监听器     监听传感器的数据的变化
    tempListener = new TempListener();
    humidityListener = new HumidityListener();

    mSensorManager.registerListener(tempListener,mTemperaSensor,SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(humidityListener,mHumiditySensor,SensorManager.SENSOR_DELAY_NORMAL);
}
 
Example 10
Source File: Device.java    From unity-ads-android with Apache License 2.0 5 votes vote down vote up
public static List<Sensor> getSensorList () {
	if (ClientProperties.getApplicationContext() != null) {
		SensorManager sensorManager = (SensorManager)ClientProperties.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
		return sensorManager.getSensorList(Sensor.TYPE_ALL);
	}

	return null;
}
 
Example 11
Source File: HostProximityProfile.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * Proximityのセンサーリストを取得する.
 * <p>
 * Proximityのセンサーに対応していない場合には、空の配列が返却される。
 * @return センサーリスト
 */
private List<Sensor> getSensorList() {
    SensorManager mgr = getSensorManager();
    if (mgr == null) {
        return new ArrayList<Sensor>();
    }
    return mgr.getSensorList(Sensor.TYPE_PROXIMITY);
}
 
Example 12
Source File: SensorListFragment.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
	super.onViewCreated(view, savedInstanceState);

	mAdapter = new SensorListAdapter( getActivity() );
	setListAdapter(mAdapter);

	mSensorManager = (SensorManager) getActivity().getSystemService( Context.SENSOR_SERVICE );

	for( Sensor sensor : mSensorManager.getSensorList( Sensor.TYPE_ALL ) )
		mAdapter.add( sensor );
}
 
Example 13
Source File: SensorCollectorManager.java    From sensordatacollector with GNU General Public License v2.0 5 votes vote down vote up
private void initSensors()
{
    // Add all sensors
    SensorManager mSensorManager = (SensorManager) this.context.getSystemService(Activity.SENSOR_SERVICE);
    List<Sensor> allSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);

    for(Sensor sensor : allSensors) {
        this.addSensorCollector(sensor.getType(), sensor);
    }

    for(int type : CollectorConstants.activatedCustomCollectors) {
        this.addCustomCollector(type, this.context);
    }
}
 
Example 14
Source File: DeviceInfo.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all the sensors available on the device as a List.
 * @return - List of all the sensors available on the device.
 */
public List<Sensor> getAllSensors() {
	SensorManager sensorManager =
                             (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
	
	return sensorManager.getSensorList(Sensor.TYPE_ALL);
}
 
Example 15
Source File: SensorUtil.java    From Compass with Apache License 2.0 4 votes vote down vote up
public static boolean hasRotationVector(Context context) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

    List<Sensor> listSensors = sensorManager.getSensorList(Sensor.TYPE_ROTATION_VECTOR);
    return !listSensors.isEmpty();
}
 
Example 16
Source File: SensorUtil.java    From Compass with Apache License 2.0 4 votes vote down vote up
public static boolean hasAccelerometer(Context context) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

    List<Sensor> listSensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
    return !listSensors.isEmpty();
}
 
Example 17
Source File: SensorUtil.java    From Compass with Apache License 2.0 4 votes vote down vote up
public static boolean hasMagnetometer(Context context) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

    List<Sensor> listSensors = sensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);
    return !listSensors.isEmpty();
}
 
Example 18
Source File: EnvironmentalSucker.java    From CameraV with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked", "deprecation" })
public EnvironmentalSucker(Context context) {
	super(context);
	setSucker(this);
	
	sm = (SensorManager)context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
	availableSensors = sm.getSensorList(Sensor.TYPE_ALL);
	
	for(Sensor s : availableSensors) {
		switch(s.getType()) {
		case Sensor.TYPE_AMBIENT_TEMPERATURE:
			sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
			break;
		case Sensor.TYPE_RELATIVE_HUMIDITY:
			sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
			break;
		case Sensor.TYPE_PRESSURE:
			sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
			break;
		case Sensor.TYPE_LIGHT:
			sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
			break;				
		case Sensor.TYPE_TEMPERATURE:
			sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
			break;
		}
			
	}
	
	setTask(new TimerTask() {
		
		@Override
		public void run() {
				if(currentAmbientTemp != null)
					sendToBuffer(currentAmbientTemp);
				if(currentDeviceTemp != null)
					sendToBuffer(currentDeviceTemp);
				if(currentHumidity != null)
					sendToBuffer(currentHumidity);
				if(currentPressure != null)
					sendToBuffer(currentPressure);
				if(currentLight != null)
					sendToBuffer(currentLight);
		}
	});
	
	getTimer().schedule(getTask(), 0, Environment.LOG_RATE);
}
 
Example 19
Source File: SensorUtil.java    From Compass with Apache License 2.0 4 votes vote down vote up
public static boolean hasGravitySensor(Context context) {
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> listSensors = sensorManager.getSensorList(Sensor.TYPE_GRAVITY);
    return !listSensors.isEmpty();
}
 
Example 20
Source File: AccelerometerSucker.java    From CameraV with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public AccelerometerSucker(Context context) {
	super(context);
	setSucker(this);
	
       df.setMaximumFractionDigits(1);
       df.setPositivePrefix("+");
	
	sm = (SensorManager)context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
	availableSensors = sm.getSensorList(Sensor.TYPE_ALL);
	
	for(Sensor s : availableSensors) {
		switch(s.getType()) {
		case Sensor.TYPE_ACCELEROMETER:
			hasAccelerometer = true;
			sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
			break;
		case Sensor.TYPE_MAGNETIC_FIELD:
			sm.registerListener(this, s, SensorManager.SENSOR_DELAY_GAME);
			hasOrientation = true;
			break;
			
		
		}
			
	}
	
	setTask(new TimerTask() {
		
		@Override
		public void run() {
			try {
				if(hasAccelerometer)
					readAccelerometer();
				if(hasOrientation)
					readOrientation();
			} catch(JSONException e){}
		}
	});
	
	getTimer().schedule(getTask(), 0, Accelerometer.LOG_RATE);
}