Is there any Android api to find/sense room temperature programmatically in android code?

前端 未结 9 1202
天命终不由人
天命终不由人 2021-01-04 04:47

I am developing an android app for my project, I need to find room temperature as part of it. I am using Droid 2 A955 model for testing.

My Questions are:

相关标签:
9条回答
  • 2021-01-04 05:25

    You'll want to check out the Sensor reference docs. Offhand, I don't think there are accessible temperature sensors on-board most handhelds though.

    0 讨论(0)
  • 2021-01-04 05:25

    Try this one:

    private float temperature = 0;
    

    In onCreate put wìthis code:

        SensorManager mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor AmbientTemperatureSensor
                = mySensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
        if (AmbientTemperatureSensor != null) {
            mySensorManager.registerListener(
                    AmbientTemperatureSensorListener,
                    AmbientTemperatureSensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    

    And the method below:

    private final SensorEventListener AmbientTemperatureSensorListener
                = new SensorEventListener() {
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void onSensorChanged(SensorEvent event) {
                if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
                    temperature = event.values[0];
                }
            }
    
        };
    
    0 讨论(0)
  • 2021-01-04 05:30

    To answer all three of your questions in one fell swoop, no, I don't believe so. There can be a temperature sensor in android devices, but it senses the temperature of the battery, not the outside temperature. It would not provide an accurate gauge for that purpose.

    I'm not sure how an ambient light sensor would help with temperature, it can be very bright out but it could be in an air conditioned room.

    Lastly: there are lots of examples of temperature apps, but again, most are related to the battery.

    Edit: Official documentation says:

    Device implementations MAY but SHOULD NOT include a thermometer (i.e. temperature sensor.) If a device implementation does include a thermometer, it MUST measure the temperature of the device CPU. It MUST NOT measure any other temperature. (Note that this sensor type is deprecated in the Android 2.3 APIs.)

    Update:

    API level 14 (i.e. Android 4.0) onwards, support for measuring ambient temperature has been added (via TYPE_AMBIENT_TEMPERATURE). [Android doc reference]

    This, however, will work only on devices with ambient temperature sensor.

    0 讨论(0)
  • 2021-01-04 05:38

    You can use the iCelsius Wireless from Kickstarter. It has an API. It is impossible to measure accurately temperature using an internal sensor from a SmartPhone. It will always be few degree to high.

    0 讨论(0)
  • 2021-01-04 05:39

    i have same your question long time ago. And what my found is :

    • check this offical site for Environment sensor
    • this tuts for environment sensor : Ambient Temperature (room temp) Ambient Light ... Tutsplus. on my phone only Ambient Light work
    • now as i know only samsung galaxy S4 have Ambient Temperature. if you have S4 search Holo Ambient Temperature on gg playstore .
    0 讨论(0)
  • 2021-01-04 05:40

    It seems that there is a new sensor in the API.

    This one is for the ambient temperature, but probably there are just a few devices with this implemented.

    UPDATE: it seems that the Galaxy S4 is the first phone integrating ambient temperature sensor

    0 讨论(0)
提交回复
热议问题