Detect When a Galaxy Watch Is Being Worn
some features on galaxy watch running wear os powered by samsung function only when the watch is being worn on the user’s wrist. notifications are an important example of one such feature. when the watch is not being worn, phone notifications are not synchronized to the watch and local notifications generated on the watch are muted. off-body detection enables you to determine if the user is wearing the watch. to enable this, the watch contains a low latency off-body sensor. the data from this sensor allows you to enhance your application by implementing it to, for example, send or avoid notifications or to show or hide sensitive information, as appropriate. this tutorial demonstrates how to detect if the watch is being worn, and a sample application is provided so you can examine how the code works in practice. the tutorial also describes how you can override the off-body sensor for testing purposes. implementing off-body detection for galaxy watch to access the low latency off-body sensor data on a galaxy watch running wear os powered by samsung, the sensormanager library provides the type_low_latency_offbody_detect key, which enables you to check if the watch is being worn. to detect if the watch is being worn: in android studio, to create a wearable application project, select "new project > wear os > blank activity > finish." because the low latency off-body sensor is a body sensor, in the application manifest file, define the required permission to use the body sensors: <uses-permission android:name="android.permission.body_sensors" /> for android 6 (api level 23) and higher, to access sensor-related information, the application must obtain permission from the user. in the application code, check that the user has granted access to sensor data: if (checkselfpermission(manifest.permission.body_sensors) != packagemanager.permission_granted) { requestpermissions( new string[]{manifest.permission.body_sensors}, 1); } else { log.d(tag, "already granted"); } if the user has not yet granted permission to access sensor data, they are prompted to do so. your application can extract sensor information only if the user selects "allow." if they select "deny," the application cannot access any information from the sensor. for more information about runtime permissions, see request permissions. in the application code, create an instance of the sensormanager class: msensormanager = (sensormanager) getsystemservice(getapplicationcontext().sensor_service); implement the low latency off-body sensor: offbodysensor = msensormanager.getdefaultsensor(sensor.type_low_latency_offbody_detect); to implement an event listener to notify when the sensor value changes, override onsensorchanged(): float offbodydatafloat = sensorevent.values[0]; int offbodydata = math.>round(offbodydatafloat); if (offbodydata == 0) { mtextview.settext("the watch is not being worn!"); mtextview.settextcolor(color.parsecolor("#ff0000")); } else { mtextview.settext("the watch is being worn!"); mtextview.settextcolor(color.parsecolor("#76ba1b")); } when the offbodydata value is 1, the watch is on the user’s wrist. otherwise, its value is 0, which means the watch is not being worn. to activate the listener when the application is running, register it by overriding the onresume() method: msensormanager.registerlistener(mainactivity.this, offbodysensor, sensormanager.sensor_delay_normal); alternatively, you can implement registering the listener through a button in the application ui. when the listener is no longer needed, unregister it by overriding the onpause() method: msensormanager.unregisterlistener(this); alternatively, you can implement unregistering the listener within the onpause() method, or through a button in the application ui. sample application to see for yourself how the off-body sensor works, download the following sample application that supports galaxy watch4 or higher. offbodysensorexample (242kb) jun. 14, 2023 extract the application files and open the "offbodysensorexample/build.gradle" file in android studio. to run the sample application on a galaxy watch4 or higher, enable usb debugging mode on the watch. connect the watch to your computer and run the application through android studio. when you have granted the application permission to access the watch’s sensor data, tap the "check" button to check if the watch is currently being worn. figure 1: sample application manually overriding the off-body sensor for testing during application development, it can be inconvenient to wear the watch while testing. for testing purposes, you can manually override the off-body sensor: connect the watch to your computer. in the adb shell, to override the off-body sensor manually: wearing the watch $ adb shell am broadcast -a com.samsung.android.hardware.sensormanager.service.offbody_detector --ei force_set 1 not wearing the watch $ adb shell am broadcast -a com.samsung.android.hardware.sensormanager.service.offbody_detector --ei force_set 2 when the command is successful, you see a message similar to "broadcast completed: result=0." to receive notifications, make sure the watch is not in charging mode: a. to simulate disconnecting the charger, use the following adb command: dumpsys battery unplug b. to re-enable detecting the watch’s charging state normally: dumpsys battery reset disconnect the watch from your computer and test your application. after testing, return the watch to its default state. to disable the sensor override, use the following adb command: $ adb shell am broadcast -a com.samsung.android.hardware.sensormanager.service.offbody_detector --ei force_set 0 summary various galaxy watch features, such as notifications, work only when the user is wearing the watch. the low latency off-body sensor enables you to implement application features that react to if the watch is being worn. to ease application development, you can override the sensor so the watch does not need to be physically worn during testing. if you have questions about or need help with the information in this tutorial, you can contact samsung developer support.