Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / wearlink / README.md
| 1 | # WearLink — first-party Wear Data Layer replacement |
| 2 | |
| 3 | The Google Wearable Data Layer is proprietary Play services. WearLink |
| 4 | reimplements the parts Modern-Apps actually used, over platform Bluetooth, |
| 5 | with no Play services anywhere. |
| 6 | |
| 7 | ## Transport choice |
| 8 | |
| 9 | **Bluetooth Classic RFCOMM for everything, over the existing bond.** |
| 10 | |
| 11 | - Wear OS watches are already BT-bonded to the phone by the system |
| 12 | companion, but third-party apps cannot ride that system link — we open |
| 13 | our own RFCOMM service (UUID `4d41574c-0001-…`, name |
| 14 | `ModernApps WearLink`) on top of the same bond. |
| 15 | - Candidates come from `BluetoothManager.adapter.bondedDevices` |
| 16 | (`getBondedDevices()`); RFCOMM connect is |
| 17 | `createRfcommSocketToServiceRecord`, accept is |
| 18 | `listenUsingRfcommWithServiceRecord` (both verified against the local |
| 19 | android-37 SDK). Secure/bonded sockets only. |
| 20 | - **Both sides listen and both sides dial**, so either side can initiate. |
| 21 | When both dial at once, the handshake's per-process instance ids break |
| 22 | the tie: the pair initiated by the smaller instance id survives, on both |
| 23 | ends, deterministically. |
| 24 | - Why not BLE GATT as primary: a GATT design (watch = server advertising, |
| 25 | phone = central) would need BLUETOOTH_ADVERTISE/SCAN, MTU negotiation |
| 26 | and characteristic chunking — workable for messages, but APK-sized |
| 27 | streams become slow, fiddly MTU plumbing. RFCOMM is an ordered stream |
| 28 | socket with socket flow control: bulk is just bytes. BLE's only win |
| 29 | would be lower idle power for discovery, and we do not discover at all |
| 30 | (bonded lookup instead), so BLE is not used. |
| 31 | |
| 32 | ## Layer mapping (vs Data Layer) |
| 33 | |
| 34 | | Data Layer | WearLink | |
| 35 | |---|---| |
| 36 | | CapabilityClient | Bonded-set candidates + HELLO handshake for `appId` (the old capability string, reused verbatim). `WearLinkState` replaces node reachability. | |
| 37 | | MessageClient | `MESSAGE` frames: fire-and-forget, optional `MESSAGE_ACK`, or `RESPONSE`-correlated requests (ids + timeout + duplicate suppression). Same paths/payloads as before. | |
| 38 | | DataClient (DataItem) | `DATA` frames: path-keyed document, generation + chunk index/count + total size + SHA-256; applied only when complete and verified. No 100 KB cap; replaces contacts' hand-rolled chunk paths and appstore/calendar's DataMap JSON. | |
| 39 | | ChannelClient | `STREAM_OPEN/CHUNK/CLOSE` on the same session: total size + SHA-256 header, receiver `STREAM_ACK` carries its durable offset, sender seeks — **resumable** across reconnects when the sink kept its partial file. | |
| 40 | | WearableListenerService | *Nothing.* See degradations. | |
| 41 | |
| 42 | Golden-JSON payloads inside messages/data items are unchanged — only the |
| 43 | transport underneath them changed. |
| 44 | |
| 45 | ## Framing & reliability |
| 46 | |
| 47 | `[u32 length][u8 type][u64 id][u16 path len][path][body]`, capped at |
| 48 | 64 KiB per frame, 256 KiB per message, 4 MiB per data item, 200 MiB per |
| 49 | stream. Corrupt length/type fails the session closed. All IO runs on |
| 50 | `Dispatchers.IO` inside `WearLinkNode`; reconnects back off 1 s → 30 s. |
| 51 | Failure is surfaced through `WearLinkState` into each app's existing |
| 52 | phone/watch sync states (no fake "synced"). |
| 53 | |
| 54 | ## Trust |
| 55 | |
| 56 | Bonding proves the paired device and encrypts the link; it does not prove |
| 57 | the peer *app*. The handshake also requires: equal `appId`, and a token |
| 58 | `SHA-256(signingCertHash ‖ peerNonce ‖ appId)` where each side derives the |
| 59 | expected hash from its **own** PackageManager signing certificate |
| 60 | (`GET_SIGNING_CERTIFICATES`). A differently-signed peer fails; the nonce |
| 61 | binding stops replay. Honest limitation: an APK certificate is public, so |
| 62 | a malicious app already on the already-bonded watch could compute a token |
| 63 | — WearLink proves *same-signed app on the paired device*, not more, and |
| 64 | no more is available to third-party apps (the signing private key is not |
| 65 | accessible to app code). |
| 66 | |
| 67 | ## Permissions |
| 68 | |
| 69 | Library manifest declares only `BLUETOOTH_CONNECT` (API 31+) plus |
| 70 | `BLUETOOTH`/`BLUETOOTH_ADMIN` capped at API 30. No scan/advertise (we |
| 71 | never scan or advertise), and no location permission or |
| 72 | `neverForLocation` workaround. The hosting activity requests |
| 73 | `BLUETOOTH_CONNECT` at runtime; until granted the node reports |
| 74 | `PermissionMissing` instead of crashing. |
| 75 | |
| 76 | ## What degrades vs GMS (honest) |
| 77 | |
| 78 | - **No background system wakeups.** `WearableListenerService` woke a dead |
| 79 | app on every message/data change; WearLink listeners live and die with |
| 80 | the app process. A watch request that arrives while the phone app is |
| 81 | dead is not delivered until the phone app next runs and reconnects |
| 82 | (then state documents republish/reconcile as before). |
| 83 | - **No store-and-forward.** DataItems were persisted by Play services and |
| 84 | reconciled whenever devices met; WearLink data items are delivered on |
| 85 | a live session and each app persists what it receives (all three wear |
| 86 | apps already did). Publishing while disconnected is a no-op reported |
| 87 | through state, not queued by the system. |
| 88 | - **Presence is session presence**, not system reachability: "watch |
| 89 | connected" now means "our handshake completed", which is stricter and |
| 90 | slower to become true (a connect attempt), not instantaneous. |
| 91 | - Same-package + same-signing is no longer a *transport* requirement |
| 92 | (any of our app pairs could share WearLink), but all three pairs keep |
| 93 | it: the handshake token still requires the same signing certificate. |