benjymous/MWM-for-Android — pull request #60 by ceejaybee (2012-08-28 23:09:26+00:00)
Title: MetawatchService Performance Issues with ICS

Hi - I sent you a message over on the Metawatch forums, but not sure how often you are on there. I'm new to the whole github thing, so I hope this is okay...

I recently upgraded my phone to an ICS based ROM, and I was finding that if my watch was out of range, and MWM was in the 'Connecting' state, my Wifi connection would die, and MWM would seem to be working the CPU a lot, which was odd. So I did a bit of digging in the MWM service, and in the process of trying to work out what was going on, reworked a few things - I think that the trouble was that the main thread that does the communication with the watch was using a while loop, and Thread.sleep() for 3s in the run() method when connecting, and from what I could make out, the thread was not sleeping properly, and kept calling connect() in very quick succession which, for some completely random reason (Android driver issue, perhaps?) was causing my WiFi connection to drop out.

Anyway, I reworked the thread in the service to use a Looper/Handler message queue for its run() method, which I believe is the correct Android way of doing these things, and increased the reconnect try time to 10s, which seems to have fixed my dropped WiFi and high CPU usage problems. While I was in there, it seems that partial wakelocks were being taken out in a couple of places for 5s or 10s, but not explicitly released if the process completed before that, which is most of the time, so I put in some calls to explicitly release the locks too.

Also, it seemed that it was possible for the service start/stop button to get out of sync with the actual service state, which I think I've fixed.

I've been using my version for a couple of days, and my dropped wifi problem has not re-appeared, and my battery usage by MWM & Bluetooth seems to have improved. That said, this is my first bit of Android development, although I am a commercial programmer, so my changes might not be even near 100% right :-)

Give me a shout back if this is of any use...

Cheers,

Chris