Pull single value from sensor without using an event - android

I need some help getting info from the orientation sensor. As I have seen in just about every tutorial/guide out there, the values are passed to an event (onSensorChanged(SensorEvent event) in which they can be manipulated.
My problem is that I don't want to keep the electro-magnetic/orientation sensor running constantly (for the sake of battery life). I want to be able to turn it on, grab the current value and switch it off. Is there any way to do this?
I have done some searching and found that I can try multi-threading, but I'm not fully comfortable with that.
What I'm looking for is something like (Sorry for lack of formatting I can't seem to figure it out):
private void getOrientationNOW() {
m_SensorManager.registerListener(mySensorEventListener, m_MagneticSensor, SensorManager.SENSOR_DELAY_FASTEST);
//---->Something here to get the current value from the sensor
m_SensorManager.unregisterListener(mySensorEventListener);
}
If this is possible, please help me!
Thank you all in advance!

When you register a listener for a sensor the activity will be called every time the sensor values changes according to the parameters. So if you want to get the values only one once what you could do is unregister the listener for that sensor after getting the value once.

Related

Pedometer (Step Counter)

I am developing a Pedometer Android application to count number of steps taken and using the steps calculate the distance covered and calories burned. I have followed the tutorial
Create a Simple Pedometer and Step Counter in Android and done exactly like it. It detects number of steps when the sensor detects motion.
But there are some problems with it:
When I stand at the same place with my device in my hand and just move my hand or give a jerk to device, it detects the change and adds to step count.
If I move very slowly with device in my hand it does not detect the change.
If i jump, then it adds several steps in the counter.
I have checked some other applications from Play Store they do not do this kind of stuff.
I have searched but cannot find an appropriate solution or tutorial for it. Any help or suggestions. Thanks
The problem here is that your implementation is not sophisticated enough: it only checks if there is a spike in the accelerometer data and assumes that the spike is coming from a step. It has no idea where the spike in acceleration is really coming from: it might as well come from you jumping or shaking the device in your hand.
How to make it more accurate then? Well, that is a really difficult question which has been topic for scientific papers for a really long time. Even the most sophisticated fitness trackers (which use machine learning, signal processing and other statistical methods) have difficulties to determine when the step is real and when it is just noice or user playing with the device.
Luckily Android does have it's own builtin step counter and step detector, which are more sophisticated than the class in yor example.
So unless you really want to learn signal processing and AI (which I highly recommended, although I don't know much about the data science of step detection), I would suggest to use builtin detector and counter.
By implementing SensorEventListener listener within a class and overriding the two methods onSensorChanged and onAccuracyChanged you can start tracking steps.
public class StepActivity extends Activity implements SensorEventListener{
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sSensor= sensorManager .getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
...
}
Now we have initialised the SensorManager and Sensor and have the Sensor registered as a listener within the activity, we now need to implement the onSensorChanged function that will be triggered by a SensorEvent whenever there is a change to the Sensor we registered, in our case the TYPE_STEP_DETECTOR.
private long steps = 0;
#Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float[] values = event.values;
int value = -1;
if (values.length > 0) {
value = (int) values[0];
}
if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
steps++;
}
}
That's a very naive method to achieve step count. You should use Android's built-in step counter because it also uses other sensors if available such as gyroscope which can improve the step detection. You should especially use this built-in version if you are going to built things on top it. You need a reliable underlying layer. You can also try using linear acceleration sensor which is calculated by removing gravity component from the accelerometer. The gravity makes accelerometer very sensitive, that's why you see step counter increasing when you are just standing.
The details can be found here:
https://source.android.com/devices/sensors/sensor-types#step_detector
If you still want to develop your own from scratch, then look at this code:
https://github.com/bagilevi/android-pedometer
You can also try Google scholar for the latest papers on step counting algorithms. Especially try to read the latest survey on the topic.

Get accelerometer values in time

I have to get the accelerometer values in time.
I saw the Sensor class and the SensorEventListener but this listener notify only when the value changes and I need a periodic notification (also if the value do not change).
Otherwise I would like to continuously read the accelerometer value but it is not possible according to this thread: Get current SensorEvent value
What can I do?
Why do you need to continuously "read" the sensor values even when they are the same as before? If you need to calculate some output on a regular basis, then a better solution might be to set up a timer that triggers on the required intervals at which time you use the values from a set of variables that are updated whenever the sensor values change. That way even if they don't change, the timer will initiate the calculations.
Kaamel

Custom value change listener in android?

Need your help or advice.
The aim is to get continuous updates from method android.telephony.CellSignalStrengthWcdma.getDbm() (Get the mobile network signal strength as dBm) and make update of correspondent text view when the value provided by the method changes.
The first approach of the solution is to request for value in do-while cycle with predefined time interval, like 1 second, check the difference between common and previous values and make decision of textView update.
So the question is maybe there is some other better way to do this, like using kind of system listener etc?
BR,
You need to add a PhoneStateListener to your TelephonyManager using the proper method listen(). You need to pass a sub class of PhoneStateListener and the event you want to get. For example you can listen for PhoneStateListener.LISTEN_CELL_INFO; in this case the method onCellInfoChanged (List cellInfo) of your PhoneStateListener is called, providing you the info you need, avoiding a loop.

Actively query the proximity sensor once?

I need to get the current value of the proximity sensor (rather than implementing a continuous listener). On some devices, the first reported value will be a default value (e.g. "FAR") that isn't necessarily accurate, and actual values will only start appearing after the second or third reading. At the moment, I've implemented a 1-second Handler and use the last reported value (after the second has elapsed) as the "true" value, but this solution seems crude (and slow). Is there a better approach that works on all 4.0+ devices? I could simply count up until I've received 3 readings, but on some devices (e.g. GNex), the first value will be correct, and the value will only change after that if there is actually a change in the sensor.
You can do what I did:
You probably have an if statement on the listener - one logic flow for near and one for far.
Instead of waiting on the handler - do this:
if(near) {
myHandler.removeCallbacks(yourRunnableForFar);
myHandler.postDelayed(yourRunnableForNear,100);
else {
myHandler.removeCallbacks(yourRunnableForNear);
myHandler.postDelayed(yourRunnableForFar,100);
}
Notice that the inaccurate first reading(s) will immediately be followed by an accurate one, so the last one "wins".
This code works well if you didn't register sensors other than proximity. If you have a flow of readings from other sensors, than use a static flag (such as the boolean near) to trigger the handler calls only on state change.
Elaboration:
yourRunnableForFar and yourRunnableForNear - are placeholders that implement Runnable to hold your app logic on what to do when the proximity sensor returns "near" (event.values[0] == 0) or "far" (not 0).
myHandler is just any Handler you might created, or declare one just for this with Handler myHandler = new Handler(Looper.getMainLooper());
You might want to acquire a proximity lock on near, and release it and clear the listener on far. But this is app logic that might be completely different from app to app.

How to intercept tilting in Android?

I would like to write a program using tilting functionality in android.
Is there any way to intercept it? What do I get back? a vector indicating the direction of the tilt?
The main thing to get your head around it the concept of a listener.
In Android there isn't a method called getXtilt(), getYtilt() etc to get the orientation.
Instead you need to create a listener which you register with the system.
Look at this.
See the onSensorChanged(SensorEvent event) method? The Android system will invoke that method every time the sensor changes (which is very frequently). In this case it will be the TYPE_ACCELEROMETER sensor readings you will be receiving.
So when you get the SensorEvent 'event' have a look at the event.values[] array. It will contain the sensor readings. In the example code in the Android doc they register the Sensor.TYPE_ACCELEROMETER. You should register the
Sensor.TYPE_ORIENTATION. Have a look at the values array for
Sensor.TYPE_ORIENTATION. They are the tilt values you are looking for.
hope that helps
Sounds like you would have to use the Accelerometer and interpret the values and then make a decision based on them. Look up SensorEventListener and it should get you in the right direction.

Categories

Resources