A method for waiting for sensor data
Asked Answered
S

2

2

I have a class which initiates a sensor listener when it is started. This listener writes the latest sensor value to a variable when an event is triggered. The class goes on to do some logic, and at some point, will check this variable and proceed depending on the value.

My issue is that there's no guarantee that there is any value when the variable is read, since Android sensor listeners only trigger an event when the sensor value changes (and don't necessarily trigger an event as soon as they are started).

Thus, I need my app to wait for an event to trigger so that it has data to work off (preferably with a time-out, to ensure that it doesn't wait indefinitely).

My question is, what's the best way to implement this wait? Should I have a handler that checks every X milliseconds for a value before proceeding? Should I have some sort of message passing between the listener and the handler to tell it when data has been written, and that it can now resume? Are there other options which are better?

EDIT: I should point out, the class logic is executed on the UI thread.

Spa answered 19/12, 2011 at 14:38 Comment(0)
D
1

I am using Handler for exactly same situation.

Handler handler = new Handler();
handler.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    x1.setText(String.valueOf(sensors.getValueAccX()));
        Log.d("Sensors", String.valueOf(sensors.getValueAccZ()));//using persoanl methods that are not shown here
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    Toast.makeText(ClientSideActivity.this,
                            "Server is not running", Toast.LENGTH_LONG).show();
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // TODO Auto-generated method stub

                handler.postDelayed(this, 100);
            }

        });
Depositor answered 19/12, 2011 at 14:44 Comment(0)
P
1

This is not strictly related to android, but also general java question. Mechanism to implement this is container in java.lang.Oject. Assuming you have some guard object, where producer and consumer threads have access. On consumer thread you call:

guard.wait(some_optional_delay_Look_into_javadoc);

Then cosumer thread will wait until timeout ocurs or hell frozes out or producer thread issues:

guard.notify[All]();

(read documentation)

Your service listener will be producer.

Perspicuous answered 19/12, 2011 at 14:45 Comment(4)
Do you have a link to the documentation you're referring to?Spa
Right, thanks. Are there any collections classes that implement a guarded block, or should I implement it manually?Spa
You can do it with any object. Event with plain string.Perspicuous
I understand that. I was just wondering whether there's existing data structures which handle the consumer/producer relationship.Spa

© 2022 - 2024 — McMap. All rights reserved.