1. Are you ready for the Galaxy S20? Here is everything we know so far!

Service and Activity connection

Discussion in 'Android Help' started by gabyft45ol, Mar 3, 2016.

  1. gabyft45ol

    gabyft45ol Lurker
    Thread Starter


    I have a device that measure the heart rate, and send the value to my application with bluetooth LE. I created a class AnDeviceBluetoothLE, that control the bluetooth. In my Activity I have an object device from this class, and functions like device.getConnexionState() or device.setMode(int m) are used in this Activity. When the device get a new value of heart rate, it display on Activity with Activity.setData(...).

    Everything works, but when I close the application the bluetooth connection stop. So I want to create a Service with my object device, and establish a communication between my Service and my Activity:

    -My Activity ask the Service the connection state, the Service give the value obtain with device.getConnexionState(), and the Activity can continue the others operations with the value.
    for example, I currently have that in my Activity:
    Code (Text):
    1.  if (device.getConnectionState() == AnDeviceBluetoothLE.STATE_CONNECTED) {...}
    So if I put the device in a Service and use BroadcastManager to send demand and receive value, will my Activity wait to receive the value before doing the if?

    -The Service must send the measurements, but only when the Application is opened.
    How the Service can know if the Activity is opened or not?


    1. Download the Forums for Android™ app!



Share This Page