com.samsung.android.sdk.healthdata

Class HealthDeviceManager



  • public class HealthDeviceManager
    extends Object
    This class provides device information that is registered in the data framework and is able to register a new source device that provides health data.

    If a new data is saved to Samsung Health and its source device is new, the source device is registered to the health data framework automatically.
    A device with Samsung Health installed is recognized as the local device.

    Registering a new source device separately is availble with HealthDevice.Builder.build() and registerDevice(HealthDevice).

    The user's device that installed your application can meet many environments like:

    • Changing a phone and synchronizing Samsung Health data with the user's Samsung account. All data in the old phone are moved to the new one.
    • Using an accessory such as Galaxy Watch with the phone.

    A data query need to be requested with varied considerations for your application and target devices.

    If you want to make that the query result contains only data your intended devices, set data's source devices for the data query such as with setSourceDevices() of HealthDataResolver.ReadRequest.Builder.
    Otherwise, the query result can contain unintentional data of registered other accessories to Samsung Health.

    Getting Current Device Information

    The current device that Samsung Health runs is registered as the local device in the health data framework.
    If your application runs the current device and adds a new data to Samsung Health, set the data's source device as the local device. The following example shows how to get the current device information.

       public class HeathDataExample {
           // The state of connection
           private HealthDataStore mStore;
      
           void createHealthData() {
               HealthData data = new HealthData();
      
               // Sets required properties
      
               // Sets source device's UUID
               data.setSourceDevice(new HealthDeviceManager(mStore).getLocalDevice().getUuid());
      
               // Adds new data to Samsung Health
               HealthDataResolver resolver = new HealthDataResolver(mStore, null);
               InsertRequest request = new InsertRequest.Builder().setDataType(Nutrition.HEALTH_DATA_TYPE).build();
               request.addHealthData(data);
           }
       }

    Getting All Registered Devices' Information

    Samsung Health works with accessories such as Galaxy Watch by the user's registration and can save the registered accessory's health data. Also your application can work with a linked accessory and inserts its measured data to Samsung Health. You can get all registered devices to Samsung Health with getAllDevices() and figure out other devices beside the current device that Samsung Health runs as the following example.

       //Gets the current device
       HealthDeviceManager deviceManager = new HealthDeviceManager(mStore);
       String localUuid = deviceManager.getLocalDevice().getUuid();
      
       // Gets all registered devices
       List<HealthDevice> healthDevices = new HealthDeviceManager(mStore).getAllDevices();
      
       // Checks registered devices beside the current device
       for (HealthDevice device : healthDevices) {
           if (!device.getUuid().equals(localUuid)) {
               Log.d(APP_TAG, "Accessory's UUID: " + device.getUuid());
           }
       }

    Registering Device to Samsung Health

    If you add data to Samsung Health and the data's source device is not registered yet on Samsung Health, register the data's source device before to adding data with registerDevice(HealthDevice).
    Since:
    1.0.0
    • Constructor Detail

      • HealthDeviceManager

        public HealthDeviceManager(HealthDataStore store)
        Constructs and initializes an instance of HealthDeviceManager.
        Parameters:
        store - A connection with the health data store
        Since:
        1.0.0
    • Method Detail

      • getLocalDevice

        public HealthDevice getLocalDevice()
        Gets a HealthDevice object of the current device.
        Samsung Health SDK for Android registers the current device where Samsung Health is installed.
        Returns:
        The health device object
        Throws:
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0
      • getAllDevices

        public List<HealthDevice> getAllDevices()
        Gets a list of all source devices in Samsung Health's saved data.
        It's different with getting all connected devices with Samsung Health. If a connected device didn't save its data to Samsung Health, the device is not included in this API's return. Because this API gives only all device IDs based on the saved data.
        An accessory that the user removes from Samsung Health's accessory list can be contained in its result if the accessory's data is saved in Samsung Health.
        Returns:
        A list of registered all health device objects
        Throws:
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0
      • registerDevice

        public String registerDevice(HealthDevice device)
        Registers a new source device that provides health data to the health data framework.
        Parameters:
        device - The health device object to register
        Returns:
        The registered device's ID
        Throws:
        IllegalArgumentException - If the argument is null
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0
        See Also:
        HealthDevice.Builder.build()
      • getDeviceBySeed

        public HealthDevice getDeviceBySeed(String seed)
        Gets a health device specified by a seed.
        Parameters:
        seed - The unique value that identifies the device. It can be a Wi-Fi Mac address, Bluetooth address, or device's serial number.
        Returns:
        A health device object for the seed
        Throws:
        IllegalArgumentException - If the argument is null
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0
      • getDeviceByUuid

        public HealthDevice getDeviceByUuid(String uuid)
        Gets a health device specified by an UUID.
        Parameters:
        uuid - The UUID that represents the device
        Returns:
        A health device object for the UUID
        Throws:
        IllegalArgumentException - If the argument is null
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0
      • getDeviceUuidsByCustomName

        public List<String> getDeviceUuidsByCustomName(String name)
        Gets UUIDs of health devices for a given custom name.
        Parameters:
        name - The custom name
        Returns:
        A list of UUIDs for devices that are specified with the custom name.
        Throws:
        IllegalArgumentException - If the argument is null
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0
      • getDeviceUuidsByModel

        public List<String> getDeviceUuidsByModel(String model)
        Gets UUIDs of health devices for a given device model.
        Parameters:
        model - The model name
        Returns:
        A list of UUIDs for devices that are specified with the model name
        Throws:
        IllegalArgumentException - If the argument is null
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0
      • getDeviceUuidsByManufacturer

        public List<String> getDeviceUuidsByManufacturer(String manufacturer)
        Gets UUIDs of health devices for a given manufacturer.
        Parameters:
        manufacturer - The manufacturer name
        Returns:
        A list of UUIDs for devices that are specified with the manufacturer
        Throws:
        IllegalArgumentException - If the argument is null
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0
      • changeCustomName

        public boolean changeCustomName(String uuid,
                                        String name)
        Changes a custom name of the health device for a given device ID.
        Parameters:
        uuid - The device ID
        name - The new custom name of the device
        Returns:
        true if the change is successful, or
        false if otherwise
        Throws:
        IllegalArgumentException - If the argument is null
        IllegalStateException - If the connection to the health data store is invalid or a remote-invocation error occurs on the connection
        Since:
        1.0.0