Events Monitoring
Samsung Developers
The Galaxy Watch running Wear OS powered by Samsung can detect events like hard falls. To detect the hard falls, the watch uses a built-in accelerometer. Using the Health Services API, you can receive a fall detection event in your watch application. In this blog, we create a Wear OS application to identify a fall detection event and demonstrate how to use the Health Services API to achieve this on the Galaxy Watch.
The Galaxy Watch uses the fall detection event in its SOS feature. For more information, see Use your Samsung smart watch in an emergency situation. It can be used to take care of elderly people or patients.
How to trigger a fall detection event in your application on the Galaxy Watch
If the functionality provided with the watch is not sufficient for your solution, you can use the Health Services API to detect this event in your own application. In this section, we describe all the important steps that you must follow when building an events tracking app. As an example, we use the EventsMonitor sample project.
Project settings
Before you start writing your code, you need to import the Health Services API library in the dependencies section of the app/build.gradle file.
implementation androidx.health:health-services-client:1.0.0-beta01
Now you are ready to use the Health Services API.
Get PassiveMonitoringClient
PassiveMonitoringClient enables tracking of the data in the background (without requiring an ongoing workout) and the events that can occur. You need to get this client to make your application suscribe to the events.
private var healthServicesClient: HealthServicesClient
private var passiveMonitoringClient: PassiveMonitoringClient
init {
healthServicesClient = HealthServices.getClient(context)
passiveMonitoringClient = healthServicesClient.passiveMonitoringClient
}
Ask for permissions
In the first step, you need to modify the AndroidManifest.xml file.
- Add the
<uses-permission>
element in the global section:
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
- Add the
<queries>
element:
<queries>
<package android:name="com.google.android.wearable.healthservices" />
</queries>
It is a good practice to ask for the required permissions whenever the application tries to use this data type. First, you should check whether the user has consented to use the particular functionality.
permissionGranted = applicationContext.checkSelfPermission(
Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_GRANTED
If not, you must ask for it before using the API.
private fun requestPermissions() {
permissionLauncher.launch(android.Manifest.permission.ACTIVITY_RECOGNITION)
}
To ask about permissions, you need to create a request permissions object.
permissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { result ->
permissionGranted = result
}
This is an example of a permission request window:
Using Health Services API to get events
To asynchronously receive information about a fall detection event, provide the class which inherits from the PassiveListenerService class and override the onHealthEventReceived method.
class PassiveHealthEventService : PassiveListenerService() {
override fun onHealthEventReceived(event: HealthEvent) {
runBlocking {
Log.i(TAG, "onHealthEventReceived received with type: ${event.type}")
HealthServicesManager.getInstance(applicationContext).recordHealthEvent(event)
super.onHealthEventReceived(event)
}
}
}
Add information about this class with the permissions to the AndroidManifest.xml file.
<service
android:name=".PassiveHealthEventService"
android:exported="true"
android:permission="com.google.android.wearable.healthservices.permission.PASSIVE_DATA_BINDING" />
When you have the PassiveMonitoringClient and PassiveHealthEventService classes, you can then subscribe to the events.
private val healthEventTypes = setOf(HealthEvent.Type.FALL_DETECTED)
suspend fun registerForHealthEventsData() {
Log.i(TAG, "Registering listener")
val passiveListenerConfig = PassiveListenerConfig.builder()
.setHealthEventTypes(healthEventTypes)
.build()
passiveMonitoringClient.setPassiveListenerServiceAsync(
PassiveHealthEventService::class.java,
passiveListenerConfig
).await()
registered = true
}
If you no longer want to receive information about the fall detection event, please unregister your application from the service. This can be done using the PassiveMonitoringClient API.
suspend fun unregisterHealthEventsData() {
Log.i(TAG, "Unregistering listeners")
passiveMonitoringClient.clearPassiveListenerServiceAsync().await()
registered = false
}
The HealthEvent class contains information about the event, such as:
- Type - returns the type of the event (FALL_DETECTED, UNKNOWN).
- Instant - returns the time of the health event.
- DataPointContainer - returns the metrics associated with the event.
Test application on the Galaxy Watch
You can test this functionality in the following two ways:
Manual test
You can simulate a fall by trying to fall on a mat.
Use synthetic data
Testing is available on an emulator. Use the command line to run and execute commands for synthetic data generation. For more details about this feature, see Simulate sensor data with Health Services. With adb
, you can send subsequent commands to the device.
For testing on Wear OS 3, see more information here.
To start the synthetic data generation, run the following command:
$ adb shell am broadcast \
-a "whs.USE_SYNTHETIC_PROVIDERS" \
com.google.android.wearable.healthservices
To simulate a fall, run the following command:
$ adb shell am broadcast \
-a "whs.FALL_OVER" \
com.google.android.wearable.healthservices
When the tests are finished, to switch back to using real sensors, run the following command:
$ adb shell am broadcast \
-a "whs.USE_SENSOR_PROVIDERS" \
com.google.android.wearable.healthservices
Resources
This blog is based on the EventsMonitor application. The whole presented code comes from this application. The entire application can be downloaded from:
Enjoy your adventure creating the ultimate health application
Now you are ready to start the fall detection event in your application. We encourage you to try doing it by yourself and explore other features provided by the Health Services SDK.