The Galaxy Watch working Put on OS powered by Samsung can detect occasions like arduous falls. To detect the arduous falls, the watch makes use of a built-in accelerometer. Utilizing the Well being Companies API, you possibly can obtain a fall detection occasion in your watch utility. On this weblog, we create a Put on OS utility to establish a fall detection occasion and display use the Well being Companies API to realize this on the Galaxy Watch.
The Galaxy Watch makes use of the autumn detection occasion in its SOS function. For extra data, see Use your Samsung smart watch in an emergency situation. It may be used to handle aged folks or sufferers.
Methods to set off a fall detection occasion in your utility on the Galaxy Watch
If the performance supplied with the watch just isn’t enough to your resolution, you need to use the Well being Companies API to detect this occasion in your individual utility. On this part, we describe all of the vital steps that you will need to observe when constructing an occasions monitoring app. For example, we use the EventsMonitor sample project.
Venture settings
Earlier than you begin writing your code, it’s good to import the Well being Companies API library within the dependencies part of the app/construct.gradle file.
implementation androidx.well being:health-services-client:1.0.0-beta01
Now you’re prepared to make use of the Well being Companies API.
Get PassiveMonitoringClient
PassiveMonitoringClient permits monitoring of the info within the background (with out requiring an ongoing exercise) and the occasions that may happen. You want to get this shopper to make your utility suscribe to the occasions.
personal var healthServicesClient: HealthServicesClient
personal var passiveMonitoringClient: PassiveMonitoringClient
init {
healthServicesClient = HealthServices.getClient(context)
passiveMonitoringClient = healthServicesClient.passiveMonitoringClient
}
Ask for permissions
In step one, it’s good to modify the AndroidManifest.xml file.
- Add the
<uses-permission>
component within the world part:
<uses-permission android:identify="android.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:identify="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:identify="android.permission.FOREGROUND_SERVICE" />
- Add the
<queries>
component:
<queries>
<package deal android:identify="com.google.android.wearable.healthservices" />
</queries>
It’s a good follow to ask for the required permissions each time the applying tries to make use of this knowledge kind. First, you must examine whether or not the consumer has consented to make use of the actual performance.
permissionGranted = applicationContext.checkSelfPermission(
Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_GRANTED
If not, you will need to ask for it earlier than utilizing the API.
personal enjoyable requestPermissions() {
permissionLauncher.launch(android.Manifest.permission.ACTIVITY_RECOGNITION)
}
To ask about permissions, it’s good to create a request permissions object.
permissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { outcome ->
permissionGranted = outcome
}
That is an instance of a permission request window:
Utilizing Well being Companies API to get occasions
To asynchronously obtain details about a fall detection occasion, present the category which inherits from the PassiveListenerService class and override the onHealthEventReceived methodology.
class PassiveHealthEventService : PassiveListenerService() {
override enjoyable onHealthEventReceived(occasion: HealthEvent) {
runBlocking {
Log.i(TAG, "onHealthEventReceived acquired with kind: ${occasion.kind}")
HealthServicesManager.getInstance(applicationContext).recordHealthEvent(occasion)
tremendous.onHealthEventReceived(occasion)
}
}
}
Add details about this class with the permissions to the AndroidManifest.xml file.
<service
android:identify=".PassiveHealthEventService"
android:exported="true"
android:permission="com.google.android.wearable.healthservices.permission.PASSIVE_DATA_BINDING" />
When you may have the PassiveMonitoringClient and PassiveHealthEventService courses, you possibly can then subscribe to the occasions.
personal val healthEventTypes = setOf(HealthEvent.Sort.FALL_DETECTED)
droop enjoyable registerForHealthEventsData() {
Log.i(TAG, "Registering listener")
val passiveListenerConfig = PassiveListenerConfig.builder()
.setHealthEventTypes(healthEventTypes)
.construct()
passiveMonitoringClient.setPassiveListenerServiceAsync(
PassiveHealthEventService::class.java,
passiveListenerConfig
).await()
registered = true
}
If you happen to now not need to obtain details about the autumn detection occasion, please unregister your utility from the service. This may be accomplished utilizing the PassiveMonitoringClient API.
droop enjoyable unregisterHealthEventsData() {
Log.i(TAG, "Unregistering listeners")
passiveMonitoringClient.clearPassiveListenerServiceAsync().await()
registered = false
}
The HealthEvent class comprises details about the occasion, reminiscent of:
- Sort – returns the kind of the occasion (FALL_DETECTED, UNKNOWN).
- Instantaneous – returns the time of the well being occasion.
- DataPointContainer – returns the metrics related to the occasion.
Take a look at utility on the Galaxy Watch
You’ll be able to check this performance within the following two methods:
Guide check
You’ll be able to simulate a fall by making an attempt to fall on a mat.
Earlier than performing the handbook check, guarantee that you’ve got taken all security precautions for your self. For instance, use cushions to melt the autumn influence, and so on.
Artificial Information Supplier
Use the command line to run and execute instructions with the Artificial Information Supplier. For extra particulars about this function, see Synthetic Data Provider. With adb
, you possibly can ship subsequent instructions to the system.
To start out the Artificial Information Supplier, run the next command:
$ adb shell am broadcast
-a "whs.USE_SYNTHETIC_PROVIDERS"
com.google.android.wearable.healthservices
To simulate a fall, run the next command:
$ adb shell am broadcast
-a "whs.artificial.consumer.FALL_OVER"
com.google.android.wearable.healthservices
When the exams are completed, to change again to utilizing actual sensors, run the next command:
$ adb shell am broadcast
-a "whs.USE_SENSOR_PROVIDERS"
com.google.android.wearable.healthservices
Sources
This weblog is predicated on the EventsMonitor utility. The entire introduced code comes from this utility. The complete utility will be downloaded from:
Take pleasure in your journey creating the final word well being utility
Now you’re prepared to begin the autumn detection occasion in your utility. We encourage you to strive doing it by your self and discover different options supplied by the Well being Companies SDK.