Skill v1.0.0
currentAutomated scan100/100name: shiny-wearables description: Guide for talking to a companion Apple Watch (WatchConnectivity) or Wear OS (Data Layer) app from .NET MAUI / .NET using Shiny.Wearables - live messages with replies, shared context, queued data and file transfers, and background delivery through a delegate auto_invoke: true triggers:
- watch paired
- watch connected
- is the watch connected
- watch reachable
- watch out of range
- paired but disconnected
- watch status indicator
- IsConfigured
- IsConnected
- WearableNode
- WearableStatus
- OnStatusChanged
- wearable
- wearables
- apple watch
- watchos
- watch app
- wear os
- wearos
- android wear
- smartwatch
- companion watch app
- WatchConnectivity
- WCSession
- Wear OS Data Layer
- MessageClient
- DataClient
- IWearableManager
- IWearableDelegate
- WearableDelegate
- WearableProtocol
- WearableData
- WearableManagerExtensions
- GetValues
- send json to watch
- send string to watch
- WearableException
- WearableErrorCode
- WearableStatus
- WearableMessage
- WearableContext
- WearableTransfer
- WearableFile
- WearableTransferInfo
- WearableTransferResult
- ShinyWearableListenerService
- AddWearables
- shiny_wearable
- Shiny.Wearables
- shiny wearables
metadata: version: "1.0.0"
Shiny Wearables
When to Use This Skill
Use this skill when the user needs to:
- Send data or commands from a phone app to a companion Apple Watch or Wear OS app (or from a .NET Wear OS app to the phone)
- Ask the watch something and await its reply
- Share the latest state (settings, the current plan) with the watch
- Queue data or files for the watch that must arrive even if it is out of range
- Receive messages, context, transfers or files from the watch, including in the background
- Check whether a watch is paired, has the companion app installed, and is reachable
- Write the Swift (watchOS) or Kotlin (Wear OS) side so it speaks Shiny's wire format
Library Overview
| Item | Value | |
|---|---|---|
| NuGet | Shiny.Wearables | |
| Primary Namespace | Shiny.Wearables | |
| Config Namespace | Shiny (AddWearables extension on IServiceCollection) | |
| Platforms | iOS (WatchConnectivity), Android (Wear OS Data Layer - phone or a .NET Wear OS app). Other targets register nothing. |
The four shapes
| Shape | API | Needs reachable? | Semantics | |
|---|---|---|---|---|
| Message | SendMessage(path, data) → reply bytes | Yes - throws WearableException(NotReachable) otherwise | Live request/reply. ~64 KB on iOS, ~100 KB on Wear OS | |
| Context | UpdateContext(data), GetContext(), GetReceivedContext() | No | Only the newest value is kept, each direction | |
| Transfer | Transfer(path, data) → id | No | Queued, in order, delivered once | |
| File | TransferFile(path, filePath, metadata) → id | No | Queued; the platform reads the file during transfer - don't delete it until OnTransferCompleted |
Setup
// MauiProgram.csbuilder.Services.AddWearables<MyWearableDelegate>(); // manager + delegate// orbuilder.Services.AddWearables(); // manager only
AddWearables<TDelegate>() can be called once per delegate; every delegate runs. On targets other than iOS/Android no IWearableManager is registered - in shared code resolve it optionally (services.GetService<IWearableManager>()) or check GetStatus().IsSupported.
iOS
- The
WCSessionis activated at startup (anIShinyStartupTask) so deliveries that arrived while the app was closed reach the delegate. Register during app startup. - iPad has no WatchConnectivity:
IsSupportedis false. Mac Catalyst/macOS/tvOS have no target. - iOS has one active watch; its node id is always
watchandnodeIdarguments are ignored.
Android / Wear OS
- Phone and watch apps must share the application id and signing key.
- The companion is discovered by the `shiny_wearable` capability (
WearableProtocol.Capability). Shiny advertises it for this app at startup; a Kotlin companion declares it inres/values/wear.xml(android_wear_capabilities). A .NET Wear OS app using Shiny.Wearables advertises it automatically too. - Inbound traffic arrives through
ShinyWearableListenerService(declared by attributes; no manifest edits), bound by Play services forwear://*/shiny...- also when the app is not running. - No Wear OS app / Play services →
IsSupportedis false. IsPairedandIsAppInstalledsurvive the watch going away - a paired watch that is off or out of range is still listed (IsConnected = false), found through the capability rather than the connected-node list. The one thing Wear OS cannot see is a paired watch that has never had the companion app installed and is currently away - the Data Layer exposes nothing that reports it.- A Wear OS node can be connected through the cloud when Bluetooth is out of range:
IsConnected = true,IsNearby = false. Transfers and context still go through; it does not count towardIsReachable, because a live message would take seconds.
Showing watch status - paired vs. connected
Two different questions, and a UI needs both. Never use one flag for both.
| Question | Flag | Survives the watch being off / out of range? | |
|---|---|---|---|
| Does this user have a watch set up? (show the "send to watch" feature at all) | status.IsConfigured (= IsPaired && IsAppInstalled) | Yes | |
| Is the watch here right now? (a "connected" dot, live messages) | status.IsReachable | No | |
| Does this platform have a wearable API at all? | status.IsSupported | n/a - fixed per device |
`IsSupported` is not "a watch is set up". It is false only where there is no wearable API (not iOS/Android, an iPad, Android without Play services). A phone with no watch reports IsSupported = true. Gate features on IsConfigured.
Gating a feature on IsReachable makes it appear and disappear as the user walks around the house. Gating a "connected" indicator on IsConfigured claims the watch is receiving while it is charging in another room.
GetStatus() is async and there is no event on IWearableManager - changes arrive through IWearableDelegate.OnStatusChanged, raised on pair/unpair, app installed/removed, and reachability changes. Cache the last status there and bind to that:
public class WatchStatusDelegate(WatchStatusState state) : WearableDelegate{public override Task OnStatusChanged(WearableStatus status){state.Update(status); // the delegate runs off the UI thread - marshal inside Updatereturn Task.CompletedTask;}}public class WatchStatusState : INotifyPropertyChanged{public bool HasWatch { get; private set; } // IsConfigured - stays true while awaypublic bool IsConnected { get; private set; } // IsReachable - livepublic string? WatchName { get; private set; }public void Update(WearableStatus status) => MainThread.BeginInvokeOnMainThread(() =>{this.HasWatch = status.IsConfigured;this.IsConnected = status.IsReachable;// a paired-but-away watch is still listed, so it can still be namedthis.WatchName = status.Nodes.FirstOrDefault(x => x.HasApp)?.DisplayName;this.PropertyChanged?.Invoke(this, new(null));});public event PropertyChangedEventHandler? PropertyChanged;}// register bothservices.AddSingleton<WatchStatusState>();services.AddWearables<WatchStatusDelegate>();// seed once at startup - OnStatusChanged fires on change, and on Android nothing changes until a capability doesstate.Update(await wearables.GetStatus());
Per node, WearableNode carries IsConnected (reachable at all - Bluetooth or cloud), IsNearby (direct Bluetooth) and HasApp. On iOS IsConnected and IsNearby are the same flag - WatchConnectivity has no cloud route.
Code Generation Instructions and Conventions
Sending
public class WatchService(IWearableManager wearables){public async Task StartWorkout(){var status = await wearables.GetStatus();if (!status.IsSupported || !status.IsAppInstalled)return;if (status.IsReachable){try{// key/values go as a JSON object; the reply comes back as key/valuesvar reply = await wearables.SendMessage("workout/start", new Dictionary<string, object?> { ["kind"] = "run", ["km"] = 5 });var accepted = reply["accepted"].GetBoolean();}catch (WearableException ex) when (ex.Code == WearableErrorCode.NotReachable){// fall back to a queued transferawait wearables.Transfer("workout/start", new Dictionary<string, object?> { ["kind"] = "run", ["km"] = 5 });}}// plain text goes as UTF-8; the reply comes back as textstring pong = await wearables.SendMessage("ping", "hello");await wearables.UpdateContext(new Dictionary<string, object?> { ["plan"] = "5k" });var fileId = await wearables.TransferFile("maps/offline", localPath, new Dictionary<string, string> { ["zoom"] = "14" });IReadOnlyList<WearableTransferInfo> pending = await wearables.GetPendingTransfers();bool cancelled = await wearables.CancelTransfer(fileId);}}
- Never build a JSON string (raw string literals, string interpolation,
JsonSerializer.Serialize) to pass as a payload. Structured data is always key/values; thestringoverloads are for plain text only. - Paths are app-defined (
sync,workout/start); slashes are trimmed; whitespace,?,#throwArgumentException. - Never hand-encode payloads.
SendMessage,UpdateContextandTransferhave extension overloads (WearableManagerExtensions, namespaceShiny.Wearables) taking astring(plain text, sent as UTF-8) or anIReadOnlyDictionary<string, TValue>(sent as a JSON object;Dictionary<string, object?>andDictionary<string, string>both work). The stringSendMessagereturns the reply as astring; the dictionary one returnsIReadOnlyDictionary<string, JsonElement>(empty for an empty reply,JsonExceptionif the reply is not a JSON object). Thebyte[]members remain for binary payloads. - Dictionary values: null, string, bool, numbers,
DateTime/DateTimeOffset/TimeSpan/Guid/Uri, enums (by name),byte[](base64),JsonElement, nested string-keyed dictionaries and lists/arrays. Anything else throwsNotSupportedException- break it into a nested dictionary or list. No reflection, so it is trim/AOT safe.
Receiving
public class MyWearableDelegate : WearableDelegate{// return value = the reply the watch gets; null lets another delegate answer// the body reads as message.GetString() or message.GetValues(); WearableData.FromString / FromValues build a replypublic override Task<byte[]?> OnMessageReceived(WearableMessage message) => message.Path switch{"ping" => Task.FromResult<byte[]?>(WearableData.FromString("pong")),"workout/start" => Task.FromResult<byte[]?>(WearableData.FromValues(new Dictionary<string, object?>{["accepted"] = message.GetValues()["kind"].GetString() == "run"})),_ => Task.FromResult<byte[]?>(null)};public override Task OnContextReceived(WearableContext context){var plan = context.GetValues()["plan"].GetString(); // or context.GetString()return Task.CompletedTask;}public override Task OnTransferReceived(WearableTransfer transfer) => Task.CompletedTask; // transfer.GetString() / GetValues()// file already moved to {AppData}/Shiny.Wearables/{id}/{name}; the delegate owns itpublic override Task OnFileReceived(WearableFile file) => Task.CompletedTask;public override Task OnTransferCompleted(WearableTransferResult result) => Task.CompletedTask; // result.Succeeded / Errorpublic override Task OnStatusChanged(WearableStatus status) => Task.CompletedTask;}
- Reply semantics: delegates are asked in turn; the first non-null reply wins; a throwing delegate is logged and skipped; with no answer the watch gets an empty reply.
message.ExpectsReplyis false when the sender used a fire-and-forget send - the return value is discarded. - All other callbacks run on every registered delegate.
- On Wear OS,
OnTransferCompletedfires when the receiver deletes the data item (the delivery receipt), soresult.Pathis empty - track byId.
Companion apps (wire format - WearableProtocol)
watchOS (Swift, WCSession)
- Message:
sendMessage(["path": String, "data": Data], replyHandler:); reply["data": Data]. Messages from the phone always carry a reply handler - always call it. - Context:
updateApplicationContext(["data": Data]) - Transfer:
transferUserInfo(["id": String, "path": String, "data": Data]) - File:
transferFile(url, metadata: ["id": String, "path": String, "name": String, "metadata": [String: String]])
WCSession.default.sendMessage(["path": "sync", "data": json],replyHandler: { reply in let data = reply["data"] as? Data },errorHandler: { print($0) })func session(_ s: WCSession, didReceiveMessage m: [String: Any], replyHandler: @escaping ([String: Any]) -> Void) {replyHandler(["data": handle(m["path"] as? String ?? "", m["data"] as? Data ?? Data())])}
Wear OS (Kotlin, Data Layer)
- Message:
MessageClient.sendRequest(nodeId, "/shiny/message/{path}", bytes); answer the phone inWearableListenerService.onRequest(orMessageClient.addRpcService) withTasks.forResult(replyBytes). - Context: data item
/shiny/contextwith byte arraydata(each node writes its own). - Transfer: urgent data item
/shiny/transfer/{id}withid,path,data; the receiver deletes it after handling (that is the sender's receipt). - File: urgent data item
/shiny/file/{id}withid,path,name,metadata(DataMap of strings), assetfile; deleted by the receiver. - Listener service intent filters:
MESSAGE_RECEIVED,REQUEST_RECEIVED,DATA_CHANGED; schemewear, host*, path prefix/shiny.
val node = Wearable.getCapabilityClient(ctx).getCapability("shiny_wearable", CapabilityClient.FILTER_REACHABLE).await().nodes.first()val reply = Wearable.getMessageClient(ctx).sendRequest(node.id, "/shiny/message/sync", bytes).await()class PhoneListener : WearableListenerService() {override fun onRequest(nodeId: String, path: String, request: ByteArray): Task<ByteArray>? =Tasks.forResult(handle(path.removePrefix("/shiny/message/"), request))}
Namespace Ambiguities
WearableStatus,WearableMessage,WearableContextetc. are common names - if another library (e.g.Shiny.AppDeviceBridge.Wearables.Client) defines contracts with the same names, alias one namespace (using Native = Shiny.Wearables;).
Best Practices
- Check
GetStatus()before live messages; preferTransfer/UpdateContextfor anything that must arrive eventually. - Gate features on
IsConfiguredand a "connected" indicator onIsReachable- neverIsSupportedfor either. - Use
UpdateContextfor state, not events - intermediate values are dropped. - Keep message payloads small; use
TransferFilefor anything large. - Don't delete a file passed to
TransferFileuntilOnTransferCompletedreports its id. - Move or delete files in
OnFileReceived- they accumulate underAppData/Shiny.Wearablesotherwise. - Always return promptly from
OnMessageReceived: the watch is waiting and WatchConnectivity times out.