Pebble WatchApp の設定をスマホから変更可能にする

公開日: : 最終更新日:2015/08/22 gadget, Pebble

invert-notinvert

このページは Pebble SDK 2.0 Tutorial #9: App Configuration | try { work(); } finally { code(); } の記事をおおまかに翻訳したものである。(Chris氏から許可いただきました)

Pebble アプリにはWatchface と WatchAppの2種類があり、WatchfaceはPebbleのハードウェアボタンを使用することができないのでPebble上で設定を変更することができない。

このため、Android/iOS 側で設定ページを用意する必要がある。

ここでは例として、Watchface や WatchApp の背景色をPebbleKit JSを使ってスマホ側で変更できるようにしてみる。

参考ページ Developer Guides // Pebble Developers

作業の流れ

  1. PebbleKit JS からデータを送れるように WatchAppでAppMessageを使えるようにする
  2. appinfo.json か、CloudPebble の設定ページで、AppMessageで使うKeyを設定する
  3. PebbleKit JS のコードを変更して設定ページをロードして watchappに送るようにする
  4. HTMLページを用意してユーザが設定を変更できるようにする

PebbleKit JS からデータを送るために WatchAppにAppMessage用コードを追加する

AppMessageInboxReceived ハンドラを用意し、メッセージを受けられるようにする。
ハンドラ中で persist date に値を書き込む。

static void in_recv_handler(DictionaryIterator *iterator, void *context)
{
  //Get Tuple
  Tuple *t = dict_read_first(iterator);
  if(t)
  {
    switch(t->key)
    {
    case KEY_INVERT:
      //It's the KEY_INVERT key
      if(strcmp(t->value->cstring, "on") == 0)
      {
        //Set and save as inverted
        text_layer_set_text_color(text_layer, GColorWhite);
        text_layer_set_background_color(text_layer, GColorBlack);
        text_layer_set_text(text_layer, "Inverted!");
 
        persist_write_bool(KEY_INVERT, true);
      }
      else if(strcmp(t->value->cstring, "off") == 0)
      {
        //Set and save as not inverted
        text_layer_set_text_color(text_layer, GColorBlack);
        text_layer_set_background_color(text_layer, GColorWhite);
        text_layer_set_text(text_layer, "Not inverted!");
 
        persist_write_bool(KEY_INVERT, false);
      }
      break;
    }
  }
}

下記を init などで実行する。

  • app_message_register_inbox_received((AppMessageInboxReceived) in_recv_handler);
  • app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());

書き込んだ persist data はwindow_load などで読み込む

appinfo.json に appkeys を追加する

App Metadata (SDK only) // Pebble Developers を参考に appinfo.json に appKeys を追加する。

今回の場合は  KEY_INVERT を追加する。

{
  "uuid": "153ff30e-0288-4411-871c-fce3dcc99e5f",
  "shortName": "sdktut9",
  "longName": "sdktut9",
  "capabilities": [ "configurable" ],
  "companyName": "Chris Lewis",
  "versionCode": 1,
  "versionLabel": "1.0.0",
  "watchapp": {
    "watchface": false
  },
  "appKeys": {
    "KEY_INVERT": 0
  },
  "resources": {
    "media": []
  }
}

PebbleKit JS から設定ページをロードして watchappに送る

PebbleKit JS は、スマートフォンから configuration page を読み込んでwatchappに結果を送るためのもので、src/js/pebble-js-app.js にある。 “showConfiguration” と “webviewclosed” のイベントで設定を行う。 デフォルトの状態

Pebble.addEventListener("ready",
  function(e) {
    console.log("PebbleKit JS ready!");
  }
);

showConfiguration イベントでは、ページを開く

Pebble.addEventListener("showConfiguration",
  function(e) {
    //Load the remote config page
    Pebble.openURL("https://dl.dropboxusercontent.com/u/10824180/pebble%20config%20pages/sdktut9-config.html");
  }

);

webviewclosed では、設定を読み込む

Pebble.addEventListener("webviewclosed",
  function(e) {
    //Get JSON dictionary
    var configuration = JSON.parse(decodeURIComponent(e.response));
    console.log("Configuration window returned: " + JSON.stringify(configuration));
 
    //Send to Pebble, persist there
    Pebble.sendAppMessage(
      {"KEY_INVERT": configuration.invert},
      function(e) {
        console.log("Sending settings data...");
      },
      function(e) {
        console.log("Settings feedback failed!");
      }
    );
  }
);



上記の URLにHTMLを用意して、ユーザが設定を変更できるようにする。


  
    SDKTut9 Configuration
  
  
    

Pebble Config Tutorial

Choose watchapp settings

Invert watchapp:

また、こちらのスクリプトも追加する。

HTML置き場として上記参考ページでは Dropbox の Public folder がおすすめされている。こちらを使うと自前でWebサーバを用意しなくてもすむ。

関連記事

Thank you, Pebble

Pebble will no longer produce or sell new hardware

記事を読む

AirMac Extreme (第5世代) MD031J/A 購入。15400円。

自宅の無線LAN環境に関して不快なことが多いので半年くらい悩んだ後にAirMac Ext

記事を読む

Topeak Ridecase for iPhone 7 Plus は2017年1月発売

(画像はiPhone 6 Plus 用 Ridecase)7月にTopeak Ridecase fo

記事を読む

Arduino Uno で電子工作

昨日 Arduino エントリーキットが届いたので、 電流、電圧?抵抗器?オームの法則って? | D

記事を読む

GPS付きPebbleベルトPalStrap

Kickstarter で出資した、GPS付き Pebble 用ベルト PalStrap がようやく

記事を読む

Mint60のFirmwareをOSX用に書き換えてみる

Mint60のFirmwareを書き換えてみる前回、自作キーボードキット Mint60のFirmwa

記事を読む

no image

Apple TV 第2世代の感想

先日届いた Apple TV 第2世代をようやく開梱。 うーむ、これはよくできてる。

記事を読む

[Apple Event]最高峰を解禁。

2022/03/09 3:00AM JST にApple Eventが行われる。今回は「最高峰を解

記事を読む

MacBook へ給電出来る外部バッテリー HyperJuice MBP-060を買ってみた

この前のブログでHyperJuice MBP-060の購入を検討していたが、品薄のた

記事を読む

[iOS SDK] Olympus Camera Kit で Pebble連携アプリを作ってみた

3月25日にOLYMPUS AIR A01が発売されたが、このカメラはOPC(Open Pl

記事を読む

Message

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ember Mug 2のACアダプタをUSB Type-Cに変えてみた

冬になると活躍する Ember Mug 2 の充電器は付属のACアダ

Wi-Fi6Eルータ TP-Link AXE5400購入

Wi-Fi6E を試してみたくなり、TP-Link AXE5

児童手当 認定請求書申請 2024 「請求者が養育をする18歳に達する日以降の最初の3月31日までの子の数」とは?

2024年に受給していない人には手紙が届くらしい。 電子申請も

Vision Proアプリ開発本 8/24、8/26に発売

Vision Proアプリ開発入門 P400が 8/24 に発売、V

Developer Strap が日本でも購入可能に

USアカウントでしか購入できなかった Vision Pro 用 De

→もっと見る

  • 2015年8月
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
PAGE TOP ↑