Advertisment

Get to Know the Ambient Temperature Through SMS

We use the temperature sensor of the mobile to create an application where your mobile will respond to an SMS inquiry about the temperature at your place

author-image
Rajkumar Maurya
New Update
Get to Know the Ambient Temperature Through SMS

We use the temperature sensor of the mobile to create an application where your mobile will respond to an SMS inquiry about the temperature at your place  

Advertisment

- Amaresh Patnaik                                                               

In my last few articles we have been developing a safety application that will respond to queries from a caretaker over SMS. The last one in the series was http://bit.ly/XUAz6t where the application responds to when it was that you last accessed your mobile phone. We extend the application in this article where your mobile will respond to an SMS enquiry about the temperature around you. This application will depend on the temperature sensor of your device. Make sure you check your handset’s documentation if it has a temperature sensor. The sensor will measure your ambient temperature, that is, the temperature of your surroundings.

The Solution

We will use the project we used in the previous article mentioned in the first paragraph. Open the SMS receiver class and add a class level variable for the hashtag to find out the temperature.

private final String FINDOUTTEMPERATURE = “#FindOutTemperature#”;

In the ‘for’ loop of on receive, add the stub if the message is to find the temperature.

if (strMessage.equals(FINDOUTTEMPERATURE)) {

Advertisment

}

From the project explorer of Eclipse, copy the light service class and paste it as the temperature service class.

Here is the revised code for the class.

Advertisment

public class TemperatureService extends Service {

private String strTo;

private SensorManager smrTemperature;

Advertisment

private float fltTemperature = Float.NaN;

private final SensorEventListener selTemperature = new SensorEventListener() {

public void onAccuracyChanged(Sensor sensor, int accuracy) { }

Advertisment

public void onSensorChanged(SensorEvent event) {

fltTemperature = event.values<0>;

sendMessage();

Advertisment

}

};

@Override

Advertisment

public IBinder onBind(Intent arg0) {

// TODO Auto-generated method stub

return null;

}

public void onCreate() {

super.onCreate();

}

public int onStartCommand(Intent intent, int flags, int startId) {

super.onStartCommand(intent, flags, startId);

Bundle bunSMS = intent.getExtras();

strTo = bunSMS.getString(SMSReceiver.PHONENUMBER);

smrTemperature = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

Sensor snrTemperature = smrTemperature.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);

if (snrTemperature != null) smrTemperature.registerListener(selTemperature, snrTemperature, SensorManager.SENSOR_DELAY_NORMAL);

return START_STICKY;

}

public void sendMessage() {

SmsManager smsLight = SmsManager.getDefault();

DecimalFormat dcfTemperature = new DecimalFormat(“##.0”);

//Round off to one place after decimal

smsLight.sendTextMessage(strTo, null, “The temperature here is: “ + dcfTemperature.format(fltTemperature) + “ degrees centigrade”, null, null);

smrTemperature.unregisterListener(selTemperature);

stopSelf();

}

}

Complete the ‘if’ stub we had started off in the SMS receiver class as follows.

if (strMessage.equals(FINDOUTTEMPERATURE)) {

processMessage(context, TemperatureService.class, strFrom);

}

Register the temperature service in the manifest. The temperature sensor works on the lines of the light sensor we have discussed previously in the safety application, so I have not discussed the working in details. You can similarly make any sensor on your device available to a caretaker through SMS. The caretaker need not have any application installed on his or her device and nor would you require an internet connection.

temperature
Advertisment