0

Anyone knows why does the Bluetooth socket shut down automatically when the app tries to take photo using Camera Activity?

The Bluetooth service is bound to the Camera Activity.

Thanks in advance!

shannon
  • 579
  • 2
  • 10
  • 22

2 Answers2

2

Your activity may be destroyed when Camera activity takes the screen. One of the reasons is memory constraints, and there is little you can do about this. Another reason could be that your activity gets restarted for landscape orientation (note that the Camera activity is fixed to landscape). When your activity is gone, the service has no reason to stay alive.

The ways to fix it are: use "custom camera" inside your app; make sure your activity handles orientation change without restart; arrange the BT service to persist even if the activity is destroyed.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Thanks for your answer! However, the Bluetooth connection now actually stays on when the new camera activity is created and it only turns off when the 'capture' button is clicked on which calls Camera.PictureCallback. Why is this so? – shannon Jan 29 '14 at 06:52
  • I am confused. Do you use [MediaStore.ACTION_IMAGE_CAPTURE](http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE) intent, or you call [Camera.takePicture()](http://developer.android.com/reference/android/hardware/Camera.html#takePicture(android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback)) in your Activity? – Alex Cohn Jan 29 '14 at 10:22
  • I used Camera.takePicture(). It's a custom camera activity. – shannon Jan 29 '14 at 14:40
  • I'm sorry. My answer was not for this scenario. My fault, I made a wrong assumption. Let's try another one. – Alex Cohn Jan 29 '14 at 16:22
1

If this happens in a custom camera activity, I suspect multithreading (or actually, lack thereof). If you open the camera on the UI thread, then pictureTaken() callback will also be called on the same thread, freezing the UI (unpleasant, but maybe tolerable), and also any BT service communication, which may be lethal for the latter.

The first cure would be to take communications with BT to secondary thread, because there may be other interlock conditions, too.

Second, open the camera on a secondary event thread (see an example), and then the camera callbacks will not freeze the UI and services that use the UI thread.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307