0

I'm trying to develop simple Android app using light sensor. Unfortunately although my SE Xperia Arc S does have light sensor I can't get it working. Simple code presented below returns null. I was checking light sensor in Service Test using *#*#7378423#*#* and Service Test -> Ambient Light Sensor and it is working there.

Returning null:

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
return mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

This code returns list of sensors, missing light sensor:

mySensorManager.getSensorList(Sensor.TYPE_ALL);

I have checked light sensor apps in the market, doesn't work either.

Phone info:

LT18i, Android version: 2.3.4, Compilation: 4.0.2.A.0.42

Any ideas?

Thanks for help.

Jakub Krawczyk
  • 103
  • 1
  • 8

1 Answers1

0

Camera.Parameters.FLASH_MODE_TORCH replacement for Android 2.1

Check the link above. I used this to test on an Arc S. It works.

The sensor API does not support light sensor in Xperia Arc S. You need to access Light sensor using Camera API. You can use the following code.

/***
 * Attempts to set camera flash torch/flashlight mode on/off
 * @param isOn true = on, false = off
 * @return boolean whether or not we were able to set it
 */
public boolean setFlashlight(boolean isOn)
{
    if (mCamera == null)
    {
        return false;
    }
    Camera.Parameters params = mCamera.getParameters();
    String value;
    if (isOn) // we are being ask to turn it on
    {
        value = Camera.Parameters.FLASH_MODE_TORCH;
    }
    else  // we are being asked to turn it off
    {
        value =  Camera.Parameters.FLASH_MODE_AUTO;
    }

    try{    
        params.setFlashMode(value);
        mCamera.setParameters(params);

        String nowMode = mCamera.getParameters().getFlashMode();

        if (isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH))
        {
            return true;
        }
        if (! isOn && nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO))
        {
            return true;
        }
        return false;
    }
    catch (Exception ex)
    {
        MyLog.e(mLOG_TAG, this.getClass().getSimpleName() +  " error setting flash mode to: "+ value + " " + ex.toString());
    }
}

Just copied the code from the above link to make it more clear.

Community
  • 1
  • 1
Anup
  • 497
  • 3
  • 11
  • Truly speaking I can't see connection between my problem and one linked by you :P – Jakub Krawczyk Feb 14 '12 at 12:44
  • Here's more explanantion. Light sensor on an Xperia Arc S is not accessible through sensor api. But in case you want to control the sensor, you can do it through Camera API, using flash based api.The link i mentioned above has the code to control the Light sensor using Camera API. – Anup Feb 16 '12 at 17:39
  • I don't know if we are thinking of the same thing :) I'm trying to use light sensor to check amount of light in e.g. a room, not to emit light by phone's LED :) – Jakub Krawczyk Feb 18 '12 at 22:37
  • It's called a proximity sensor or ambient light sensor, try proximity sensor in your code replacing TYPE_LIGHT with TYPE_PROXIMITY. – Anup Feb 22 '12 at 18:11