pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • Examples
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

Guides

  • Table of Contents
  • App Resources
  • Appstore Publishing
  • Best Practices
  • Communication
    • Advanced Communication
    • Datalogging
    • PebbleKit Android
    • PebbleKit JS
    • PebbleKit iOS (Discontinued)
    • Sending and Receiving Data
    • Sports API
  • Debugging
  • Design and Interaction
  • Events and Services
  • Graphics and Animations
  • Migrating Older Apps
  • Pebble Packages
  • Pebble Timeline
  • Rocky.js
  • Smartstraps
  • Tools and Resources
  • User Interfaces

PebbleKit Android

PebbleKit Android は、Pebble SDK と連携し、任意の Android アプリケーションに組み込むことができる Javaライブラリです。このライブラリのクラスとメソッドを使用すると、Android コンパニオンアプリは Pebble ウォッチを見つけてデータを交換できます。

このセクションでは、読者が基本的な Android 開発と統合開発環境としての Android Studio に精通していることを前提としています。Android SDK のインストールの詳細については、Android Documentation を参照してください。

ほとんどの PebbleKit Android メソッドには Context パラメータが必要です。アプリは、任意の Activity 実装から利用可能な getApplicationContext() を使用できます。

PebbleKit Android のセットアップ

Android Studio プロジェクトに PebbleKit Android を追加するには、app/build.gradle ファイルで次のようにします:

dependencies {
  compile 'com.getpebble:pebblekit:4.0.1'
}

Android からのメッセージ送信

Android アプリはコンパニオン Pebble アプリとは別にビルドされるため、ビルドシステムが一致する appmessage キーを自動的に作成する方法はありません。したがって、package.json で手動で指定する必要があります:

{
  "ContactName": 0,
  "Age": 1
}

これらの数値は、Android アプリで appmessage キーとして使用できます。

メッセージは PebbleDictionary クラスで構築され、PebbleKit クラスを使用して C ウォッチアプリまたはウォッチフェイスに送信されます。最初のステップは、PebbleDictionary オブジェクトを作成することです:

// Create a new dictionary
PebbleDictionary dict = new PebbleDictionary();

データ項目は、addString() や addInt32() など、オブジェクトによって提供されるメソッドを使用して、キーと値のペアで PebbleDictionary に追加されます。以下に例を示します:

// The key representing a contact name is being transmitted
final int AppKeyContactName = 0;
final int AppKeyAge = 1;

// Get data from the app
final String contactName = getContact();
final int age = getAge();

// Add data to the dictionary
dict.addString(AppKeyContactName, contactName);
dict.addInt32(AppKeyAge, age);

最後に、データを受信する C アプリの UUID と一致する UUID で sendDataToPebble() を呼び出すことにより、辞書が C アプリに送信されます:

final UUID appUuid = UUID.fromString("EC7EE5C6-8DDF-4089-AA84-C3396A11CC95");

// Send the dictionary
PebbleKit.sendDataToPebble(getApplicationContext(), appUuid, dict);

配信されると、この辞書は、Sending and Receiving Data で詳述されているように、AppMessageInboxReceived コールバック経由で C アプリで利用可能になります。

Android でのメッセージ受信

PebbleKit Android アプリで Pebble からメッセージを受信するには、BroadcastReceiver を拡張する PebbleDataReceiver オブジェクトの形式でリスナーを登録する必要があります:

// Create a new receiver to get AppMessages from the C app
PebbleDataReceiver dataReceiver = new PebbleDataReceiver(appUuid) {

  @Override
  public void receiveData(Context context, int transaction_id,
                                                    PebbleDictionary dict) {
    // A new AppMessage was received, tell Pebble
    PebbleKit.sendAckToPebble(context, transaction_id);
  }

};

重要

PebbleKit アプリは、メッセージが正常に受信されたことを Pebble に通知するために、確認応答(Ack)を手動で Pebble に送信する必要があります。これを行わないと、タイムアウトが発生します。

作成したら、このレシーバーは Activity からオーバーライドされた onResume() で登録する必要があります:

@Override
public void onResume() {
  super.onResume();

  // Register the receiver
  PebbleKit.registerReceivedDataHandler(getApplicationContext(), dataReceiver);
}

注意: Activity または Service が終了した後にコールバックを受け取らないようにするには、アプリは onPause() で unregisterReceiver() を使用してレシーバーの登録を解除しようとする必要があります。

レシーバーが配置されると、getString() や getInteger() などの類似のメソッドを使用して、提供された PebbleDictionary からデータを読み取ることができます。キーの値を読み取る前に、アプリは最初に != null チェックを使用してキーが存在するかどうかを確認する必要があります。

以下の例は、ウォッチが Android コンパニオンアプリに年齢の値を送信しているシナリオで、メッセージから整数を読み取る方法を示しています:

@Override
public void receiveData(Context context, int transaction_id,
                                                      PebbleDictionary dict) {
  // If the tuple is present...
  Long ageValue = dict.getInteger(AppKeyAge);
  if(ageValue != null) {
    // Read the integer value
    int age = ageValue.intValue();
  }
}

その他の機能

メッセージの送受信に加えて、PebbleKit Android では、Pebble とのより複雑な対話も可能になります。利用可能なメソッドの完全なリストについては、PebbleKit Android Documentation を参照してください。以下に、可能なことのいくつかの例を示します:

  • ウォッチが接続されているかどうかの確認:

    boolean connected = PebbleKit.isWatchConnected(getApplicationContext());
    
  • registerPebbleConnectedReceiver() と registerPebbleDisconnectedReceiver()、および適切な BroadcastReceiver で接続イベントを登録します。

    PebbleKit.registerPebbleConnectedReceiver(getApplicationContext(),
                                                      new BroadcastReceiver() {
    
      @Override
      public void onReceive(Context context, Intent intent) { }
    
    });
    
  • registerReceivedAckHandler() と registerReceivedNackHandler() で Ack/Nack イベントを登録します。

    PebbleKit.registerReceivedAckHandler(getApplicationContext(),
                                  new PebbleKit.PebbleAckReceiver(appUuid) {
    
      @Override
      public void receiveAck(Context context, int i) { }
    
    });
    
  • startAppOnPebble() と closeAppOnPebble() でウォッチアプリの起動と終了を行います。

    PebbleKit.startAppOnPebble(getApplicationContext(), appUuid);
    
You need JavaScript enabled to read and post comments.

Overview

  • PebbleKit Android のセットアップ
  • Android からのメッセージ送信
  • Android でのメッセージ受信
  • その他の機能