Pebble.Companion.Phone

Watch AppMessage protocol wiring and per-API platform incoming ports.

Use the typed Pebble.Companion.* modules for phone platform APIs such as weather, storage, and battery. Each platform API registers its own incoming port so apps can compose listeners with plain Sub.batch.

This module is for typed watch ↔ phone AppMessage traffic and platform bridge ports only.

Message constructors such as WatchToPhone and PhoneToWatch come from your project's protocol definition in protocol/src/Companion/Types.elm.

import Companion.Types exposing (PhoneToWatch(..), WatchToPhone(..))
import Pebble.Companion.Battery as Battery
import Pebble.Companion.Locale as Locale
import Pebble.Companion.Phone as Phone

type Msg
    = FromWatch (Result String WatchToPhone)
    | GotBattery (Result String Battery.BatteryInfo)
    | GotLocale (Result String Locale.LocaleInfo)

subscriptions _ =
    Sub.batch
        [ Phone.onWatchToPhone FromWatch
        , Battery.onBattery GotBattery
        , Locale.onLocale GotLocale
        ]

Watch protocol

decodeWatchToPhone

decodeWatchToPhone : Decode.Value -> Result String WatchToPhone

Decode a watch-originated request payload into a typed message.

onWatchToPhone

onWatchToPhone : (Result String WatchToPhone -> msg) -> Sub msg

Subscribe to watch-originated AppMessage payloads as typed protocol messages.

sendPhoneToWatch

sendPhoneToWatch : PhoneToWatch -> Cmd msg

Encode and send a typed phone-to-watch response.

Platform bridge

request

request : String -> String -> String -> (Decode.Value -> Result String a) -> Request a

Build a typed platform bridge request from an API id, operation, and response decoder.

requestWithPayload

requestWithPayload : String
-> String
-> String
-> Encode.Value
-> (Decode.Value -> Result String a)
-> Request a

Build a platform bridge request that includes a JSON payload.

send

send : (Result String a -> msg) -> Request a -> Cmd msg

Send a typed platform bridge request and decode the response into msg.

sendBridgeCommand

sendBridgeCommand : CommandEnvelope -> Cmd msg

Send a raw platform bridge command envelope to the companion runtime.

registerHandler

registerHandler : String -> Encode.Value -> Cmd msg

Register interest in a platform handler with the companion bridge.

Platform ports

platformIncomingFor

platformIncomingFor : String -> (Decode.Value -> msg) -> Sub msg

Route a platform handler id to its dedicated incoming port.