In general, you're probably thinking about this the wrong way. If
something is implemented asynchronously, you should also implement it this
way... So for example, you should write your code such that in the
handling takes care of it.
Conceptually you can think of a transformation like this as slicing the
code off at a continuation, and then taking your original function and
decomposing it into several pieces.
In this case you can move bits and pieces of your loop around and write
your handler something like this:
handleMessage:
if (i >= 10) then
..
if packet is valid, then
...
else
--i;
sendMessage();
Trying to shoehorn the synchronous model into the asynchronous model is
probably a bad idea, however..
One more thing, you probably shouldn't be handling these messages within
the activity class, right? Unless you are doing UI stuff where it makes
sense, you should have this done in another thread...
kris
On Wed, Dec 7, 2011 at 12:36 AM, Vincent <[email protected]> wrote:
> Hi,
>
> 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
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en