0

I have an android app that has both an activity and a service. The service reads bluetooth sensor data from a Samsung Galaxy Gear 2. The galaxy gear is shooting out data as fast as it gets it buts theres incredible lag on the Android side of receiving it. The service is three lines of code so it really can't get more efficient than that. I think it's probably the activity code which I have written below:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    //set global variables
    x = (TextView)findViewById(R.id.xaccel);
    y = (TextView)findViewById(R.id.yaccel);
    z = (TextView)findViewById(R.id.zaccel);

    doBindService(); //Conects to the Service
    Thread myThread = new Thread(myRunnable);
    myThread.start();
}

Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        while (mIsBound) {
            //check that the global variables have been set
            if (x!=null && y!= null && z != null) {
                x.post(new Runnable() {
                    @Override
                    public void run() {
                        if (HelloAccessoryProviderService.inputMessage != "") {
                             //read the service's global variable "inputMessage" and print it out
                            String mainData = HelloAccessoryProviderService.inputMessage;
                            x.setText("X: " + mainData.substring(0, 6));
                            y.setText("Y: " + mainData.substring(7, 13));
                            z.setText("Z: " + mainData.substring(14, 20));
                        }
                    };
                });
            }
        }
    }
};

I read that runnables were the best way to read continuous data but if there are other ways, it would be great. Thanks in advance!

josneville
  • 449
  • 2
  • 5
  • 15

1 Answers1

0

you can have the service provide a listener interface for the activity class to implement, then service can talk to activity (set the services reference to your activity as the listener where you bind/unbind in pause/resume of activity via a helper class), or you could have the service register on a bus and publish messages, where the activity would also register on the bus as a subscriber to those messages, otto bus is a nice easy drop-in: http://square.github.io/otto/..