Sensors

Manage device sensors

๐Ÿ‘

Sahha handles sensors for your app

The Sahha SDK acts as a bridge between your app and the sensors.

This simplifies the process of collecting and analyzing device data.


Sensor Status

The sensors have multiple possible statuses.

public enum SensorStatus: Int {
    case pending // Sensors pending (before prompting user for permission)
    case unavailable // Sensors not supported by user's device
    case disabled // Sensors disabled (after prompting user for permission)
    case enabled // Sensors enabled (after prompting user for permission)
}
enum class SahhaSensorStatus {
    pending, // Sensors pending User permission
    unavailable, // Sensors not supported by the User's device
    disabled, // Sensors disabled by the User
    enabled // Sensors enabled by the User
}
enum SahhaSensorStatus {
  pending // Sensors pending User permission
  unavailable // Sensors not supported by the User's device
  disabled // Sensors disabled by the User
  enabled // Sensors enabled by the User
}
export enum SahhaSensorStatus {
  pending = 0, // Sensors pending User permission
  unavailable = 1, // Sensors not supported by the User's device
  disabled = 2, // Sensors disabled by the User
  enabled = 3, // Sensors enabled by the User
}
export enum SahhaSensorStatus {
  pending = 0, // Sensors pending User permission
  unavailable = 1, // Sensors not supported by the User's device
  disabled = 2, // Sensors disabled by the User
  enabled = 3, // Sensors enabled by the User
}

๐Ÿšง

(iOS) Sensor Status - IMPORTANT INFO

Apple limits the ability to detect the true sensor status for security reasons.

Apple documentation:

To help protect the userโ€™s privacy, your app doesnโ€™t know whether the user granted or denied permission to read data from HealthKit. If the user denied permission, attempts to query data from HealthKit return only samples that your app successfully saved to the HealthKit store.

This means that if a Sensor is available, the only possible SensorStatus is:

  • pending if you have not already prompted the user for permission yet
  • enabled if you have already prompted the user for permission yet

The disabled SensorStatus is not triggered even if the user declines permission.

The disabled SensorStatus is only included in the iOS SDK to keep parity with the Android SDK.

Please read the official Apple documentation to better understand authorizing access to health data for iOS.

Authorizing access to health data for iOS


Get Sensor Status

You can check the current status of the sensors by calling getSensorStatus. This method is asynchronous and will return the updated SahhaSensorStatus in its callback.

Sahha.getSensorStatus { newStatus in
    if newStatus == .pending {
        // Show your custom UI asking your user to setup Sleep in the Health App
    }
}
Sahha.getSensorStatus([email protected]) { error, sensorStatus ->
    if (error != null) {
        println(error)
    } else {
        println(sensorStatus.name)
    }
});
SahhaFlutter.getSensorStatus().then((value) {
        setState(() {
            sensorStatus = value;
        });
        if (sensorStatus == SahhaSensorStatus.pending) {
          // Show your custom UI asking your user to setup Sleep in the Health App
        }
    }).catchError((error, stackTrace) => {
        debugPrint(error);
    });
Sahha.getSensorStatus((error: string, value: SahhaSensorStatus) => {
  if (error) {
    console.error(`Error: ${error}`);
  } else if (value) {
    if (value == SahhaSensorStatus.pending) {
      // Show your custom UI asking your user to setup Sleep in the Health App
    }
  }
});
import { Sahha, SahhaSensor, SahhaSensorStatus } from "sahha-capacitor";

Sahha.getSensorStatus()
  .then((data) => {
    const status = SahhaSensorStatus[data.status];
    if (status == SahhaSensorStatus.pending) {
      // Show your custom UI asking your user to setup Sleep in the Health App
    }
  })
  .catch((error: Error) => {
    console.error(error);
  });

๐Ÿšง

(iOS) Sleep Sensor - IMPORTANT INFO

In order for the Sahha SDK to collect data from the sleep sensor, Sleep functionality must be enabled by your mobile user BEFORE calling enableSensors.

We suggest checking if your user has seen the HealthKit permission screen before enabling the sleep sensor. If the status is pending, this is the perfect time to show your custom UI asking your user to setup Sleep in the Health App.

Please read the official Apple documentation to help your users setup Sleep for iOS.

Sleep for iOS


Enable Sensors

Before the SDK can start collecting data, you will need to enable sensors by calling enableSensors. This method is asynchronous and will return the updated SahhaSensorStatus in its callback.

Sahha.enableSensors { newStatus in
    print(newStatus.description)
}
Sahha.enableSensors([email protected]) { error, sensorStatus ->
    if (error != null) {
        println(error)
    } else {
        println(sensorStatus.name)
    }
}
SahhaFlutter.enableSensors().then((value) {
        setState(() {
            sensorStatus = value;
        });
        debugPrint(describeEnum(sensorStatus));
    }).catchError((error, stackTrace) => {
        debugPrint(error);
    });
}
Sahha.enableSensors((error: string, value: SahhaSensorStatus) => {
                if (error) {
                  console.error(`Error: ${error}`);
                } else if (value) {
                  console.log(`Sensor Status: ${value}`);
                  setSensorStatus(value);
                }
              }
            );
import { Sahha, SahhaSensor, SahhaSensorStatus } from "sahha-capacitor";

Sahha.enableSensors()
  .then((data) => {
    const status = SahhaSensorStatus[data.status];
    console.log(status);
  })
  .catch((error: Error) => {
    console.error(error);
  });

Open App Settings

It's possible for your app user to disable a sensor. In this case, you must send the user to the app settings to manually enable the sensor.

Sahha.openAppSettings()
Sahha.openAppSettings([email protected])
SahhaFlutter.openAppSettings()
Sahha.openAppSettings();
Sahha.openAppSettings();

โ—๏ธ

(iOS) WARNING

If the user enables / disables a sensor permission from the device settings menu while your app is in the background, the iOS system will force your app to terminate. This is intentional behavior and your app will need to be relaunched.


Post Sensor Data

๐Ÿ‘

Sahha will post sensor data automatically

By default, the Sahha SDK will post sensor data automatically.

However, if you set postSensorDataManually to true when you configure the SDK, you will need to post sensor data manually to the Sahha API by calling postSensorData at a regular interval of your choosing.

Please read the documentation for more info.

Configuration - Sensor Data Settings

Sahha.postSensorData { error, success in
    if let error = error {
        print(error)
    }
    print(success)
}
Sahha.postSensorData { error, success ->
    if (success) manualPost = "Successful"
    else manualPost = error ?: "Failed"
}
SahhaFlutter.postSensorData()
        .then((success) => {
            debugPrint(success.toString())
          })
        .catchError((error, stackTrace) => {
          debugPrint(error.toString())
    });
Sahha.postSensorData((error: string, success: boolean) => {
  console.log(`Success: ${success}`);
  if (error) {
    console.error(`Error: ${error}`);
  }
});
Sahha.postSensorData()
  .then((data) => {
    console.log(`Success: ${data.success}`);
  })
  .catch((error: Error) => {
    console.error(error);
  });