简体   繁体   中英

Xperia Arc S Light Sensor

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM