androidwear-os

Android Sleep API Codelab on Watch: API is not supported yet


I am trying to run the Android Sleep API Codelab (source code on GitHub) to run directly on a Pixel Watch 2

Under Settings > System > About

Wear OS by Google 4.0

Model Google Pixel Watch 2

Under Settings > System > About > Versions

Build number TWD9.240205.001.A

Wear Core Services 1.8.1.561085711

System UI 1.6.32.567711630

Google Play Services 23.42.12 (240304-574052649)

Android security patch March 5, 2024

I made only minor changes in the AndroidManifest.xml to run on the watch:

<uses-feature android:name="android.hardware.type.watch" />
<meta-data android:name="com.google.android.wearable.standalone" android:value="true" />

Following the instructions in the codelab, when running this code:

val task = ActivityRecognition.getClient(context).requestSleepSegmentUpdates(
   pendingIntent,
   // Registers for both SleepSegmentEvent and SleepClassifyEvent data.
   SleepSegmentRequest.getDefaultSleepSegmentRequest()
)

task.addOnSuccessListener {
   mainViewModel.updateSubscribedToSleepData(true)
   Log.d(TAG, "Successfully subscribed to sleep data.")
}
task.addOnFailureListener { exception ->
   Log.d(TAG, "Exception when subscribing to sleep data: $exception")
}

The following exception is logged:

Exception when subscribing to sleep data: java.lang.SecurityException: This API is not supported yet.

What would cause the API not to be supported? The Fitbit app correctly records sleep data overnight


Solution

  • It seems that it is not supported on wearables if you check https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionClient#requestSleepSegmentUpdates(android.app.PendingIntent,%20com.google.android.gms.location.SleepSegmentRequest)

    This API is not supported for wearable devices. Devices with the system feature of PackageManager#FEATURE_WATCH don't receive detection callbacks from this API.