1

Ok so the issue here that I have a video camera which is connected to a phone via Bluetooth. All communication with the device is via an android Service. The Service periodically updates the camera with GPS coordinates. The service unfortunately is not written by my company and we only have control over starting it and sending it commands (however it does the GPS on its own, if connected to a device). Once the service is started by our application we try to connect to any paired devices that contain my companies OUI. Once a device disconnects (maybe its turned off or battery runs out), it will not be automatically reconnected until the user re-launches our application. Thus GPS coordinates will not get sent to the camera in that scenario.

This is a problem because the user (who doesn't know anything about these services and what not) would think if he turned the device back on it should be connected.

So I was thinking I could use AlarmManager with a broadcast receiver or a service (probably an intent service because the connection takes several seconds to complete). Perhaps once ever few minutes I could check for a connection to the camera. If its present just quit, and if not attempt to connect (if there are paired devices).

However the problem is what if my application is already running? If it is then the user controls connecting to the device (they are prompted for it). Do I need to use shared preferences or something like that to tell if my application is running/shut down? Is there any way to tell from a service if the application is running? I can see this getting tricky. If anyone has ideas how to handle this let me know.

4

2 に答える 2

1

I would suggest the following method:

  1. Set alarm to start service in X minutes
  2. Immediatly set timer for the application for X-1 minutes
  3. When the timer of the application expires: cancel current and set a new alarm for the service, and new timer for the application.

This way, the service will not start if the application is still running, and once the application shutdown the alarm will not be stopped.

于 2012-04-24T16:19:43.253 に答える
1

Binyamin Sharet's answer was good but unfortunately it didn't solve my problem because when the application is no longer visible or in use it may still be running, and it takes quite awhile for the application to actually shut down. The other thing to note is when your service/listener or any other component is started up that will instantiate your Application class which can make things tricky.

Perhaps I worded my question poorly. However what I wanted was to prevent my application from attempting to re-establish connection while it was actively open (or being used).

This proved to be much easier to accomplish by simply writing to shared preferences the state (busy/not busy) and checking that in my code that ran from alarm manager. It was a bit of extra state to maintain which I didn't want but really wasn't too difficult.

I used commonsware wakeful intent to wake up every minute and do the following:

  1. Check if application is controlling this (from shared prefs), if so exit
  2. Check if bluetooth is on, if not exit
  3. Bind to the service which talks to the device and check if one is already connected, if so exit
  4. If no devices connected (but there is a device paired) then attempt to connect

Although I may be able to optimize this some it seems to work and be pretty fail proof. I know it's not great for the battery but I still don't see my application in the list of apps using my battery (although android seems to be using more).

The only real improvements I can think of are:

  1. If I'm already connected stop waking the service up every minute until it is disconnected.
  2. when bluetooth is disabled stop waking up the service. Once it is re-enabled start trying to reconnect every minute again. I believe there is a way to be notified when bluetooth is enabled by using intent filters.

For simplicity I have not implemented these yet though.

于 2012-04-30T23:22:38.967 に答える