0

I developed a Ble Android App composed from three activity and one service: - the first one to scan the device - the second one to connect to device - the third one to write communication result on the screen - inside the service there are some functions to connect to device, to check the connection , to automatically reconnect etc etc and this service is a started service and binded to each activity (the first app start this service)

On some tutorial I have seen that in this case is being used a not started service but a binded one. But I wonder myself, when we switch between two activity, if the service is not "started" one, is there the risk of ower service may be closed from the system, because in the switch between activity the service is binded with nothing ?

aeroxr1
  • 1,014
  • 1
  • 14
  • 36

1 Answers1

1

Two answer your question - there is a risk your service will get stopped at any time. If you design your app around the idea that the service will be on as you switch through activities then you're going to have a much more complicated design than you need.

You might notice that there is a service defined in the example: http://developer.android.com/guide/topics/connectivity/bluetooth-le.html

That service, as it's written does just one thing. It scans for a device and when it finds one it broadcasts the BluetoothDevice (which is a Parcelable). Then the service stops scanning. This scanning code could just as easily be in an application. But the key is that the scanning shuts down and the BluetoothDevice is passed via intent to some other component.

A good thing to keep in mind too, is that when you use BLE your application is talking to a service already. So defining a new service to wrap the BLE connection is completely redundant (technically speaking). Having multiple activities to bind to a service to talk to one device ... possible, but not without its complications. The number of edge cases you'll run into makes the effort much more work than just having your activity talk to the device directly.

Hope that helps.

Cheers.

Coder Roadie
  • 838
  • 1
  • 8
  • 11
  • You are crystal clear thanks! The Best way to gives the possibility to all app's activity to talk with the same ble device ? I have to keep in memory the reference to the device in some way. But at this point I don't understand way in the example on android developer that you Linked they use a service.. Why they doesn't do the scansion in the activity ? – aeroxr1 Apr 13 '16 at 00:23
  • 1
    The example is using the service to show how a service can be used to do a BLE scan. It also shows that BluetoothDevice is a Parcelable. – Coder Roadie Apr 13 '16 at 00:29
  • 1
    You can save your BluetoothDevice to the bundle passed to Activity.onSaveInstanceState. Saving a BluetoothDevice to disk is a bad idea. Once the application shuts down you should assume any BluetoothDevice is invalid. Instead, scan for a new one. You can filter your scan with BluetoothLeScanner.startScan(List, ScanSettings, ScanCallback). The UUID however can be persisted (string). – Coder Roadie Apr 13 '16 at 00:38
  • Thanks ! i wrote memory but I would have wrore static variable or something where each activity can access to keep the current connected device – aeroxr1 Apr 13 '16 at 05:43