I am developing an application to communicate with a device via bluetooth.
Same as the BluetoothChat sample, my application makes a service object, and call service.connect() to get a connectedThread for data transmission.
and I have a command method, which takes an integer as its parameter.
the problem is, sometimes, that device returns invalid data.
So when I want to execute the command with ten different parameter, I need to do something like :
for (i = 0; i < 10; i++) {
myService.write(command(i));
packet = waitForResponse(myService);
if (packet is valid) {
do something;
} else {
i--;
}
}
My program is using handler to deal messages, e.g. when the connectedThread read some data, it will
mHandler.obtainMessage(ControlActivity.MESSAGE_READ, -1, -1, data)
.sendToTarget();
And there is a handler in my Activity class to deal with these messages.
How can I do something like “waitForResponse” on Android?
or Is there any other way to meet my requirement?
API Level: 13
Instead of waiting, run your activity normally and just listen from broadcasts from the service.
From the service class send a global broadcast when new data is ready, and have the activity class listen and respond for that particular message.