I am trying to build an android app which connects to TI Simplelink SensorTag through bluetooth low energy. Though I am able to connect to the device but the problem is that I want to initiate some event like ring alarm etc on clicking the buttons on the sensor tag. I modified the onCharacteristicChanged() method to do that. But I want my app to do the same even if the app is not running in the foreground. The device does not disconnect when the app is running in the background but nothing happens when I click the button. Please help me in solving the problem.
This is how I have implemented the onCharacteristicChanged() method -
public void onCharacteristicChanged(String uuidStr, byte[] rawValue) {
Point3D v;
String msg;
if (uuidStr.equals(UUID_ACC_DATA.toString())) {
v = Sensor.ACCELEROMETER.convert(rawValue);
msg = decimal.format(v.x) + "\n" + decimal.format(v.y) + "\n"
+ decimal.format(v.z) + "\n";
mAccValue.setText(msg);
}
if (uuidStr.equals(UUID_MAG_DATA.toString())) {
v = Sensor.MAGNETOMETER.convert(rawValue);
msg = decimal.format(v.x) + "\n" + decimal.format(v.y) + "\n"
+ decimal.format(v.z) + "\n";
mMagValue.setText(msg);
}
if (uuidStr.equals(UUID_GYR_DATA.toString())) {
v = Sensor.GYROSCOPE.convert(rawValue);
msg = decimal.format(v.x) + "\n" + decimal.format(v.y) + "\n"
+ decimal.format(v.z) + "\n";
mGyrValue.setText(msg);
}
if (uuidStr.equals(UUID_IRT_DATA.toString())) {
v = Sensor.IR_TEMPERATURE.convert(rawValue);
msg = decimal.format(v.x) + "\n";
mAmbValue.setText(msg);
msg = decimal.format(v.y) + "\n";
mObjValue.setText(msg);
}
if (uuidStr.equals(UUID_HUM_DATA.toString())) {
v = Sensor.HUMIDITY.convert(rawValue);
msg = decimal.format(v.x) + "\n";
mHumValue.setText(msg);
}
if (uuidStr.equals(UUID_BAR_DATA.toString())) {
v = Sensor.BAROMETER.convert(rawValue);
double h = (v.x - BarometerCalibrationCoefficients.INSTANCE.heightCalibration)
/ PA_PER_METER;
h = (double) Math.round(-h * 10.0) / 10.0;
msg = decimal.format(v.x / 100) + "\n" + h;
mBarValue.setText(msg);
}
if (uuidStr.equals(UUID_KEY_DATA.toString())) {
SimpleKeysStatus s;
final int img;
s = Sensor.SIMPLE_KEYS.convertKeys(rawValue);
switch (s) {
case OFF_OFF:
img = R.drawable.buttonsoffoff;
break;
case OFF_ON:
r.stop(); // r is the ringtone which is initialized in the onCreate() method
img = R.drawable.buttonsoffon;
break;
case ON_OFF:
r.play();
img = R.drawable.buttonsonoff;
break;
case ON_ON:
img = R.drawable.buttonsonon;
break;
default:
throw new UnsupportedOperationException();
}
mButton.setImageResource(img);
}
}