1°) Préparation Eclipse :
Importez le projet pebblekit en téléchargeant le zip ici
Décompressez le zip dans votre « workspace » puis importez le projet dans Eclipse (click droit sur l’arborescence des projets Android puis Import puis Existing Android Code Into Workspace)
Ajoutez pebblekit comme une bibrairie via clic droit sur votre projet « Pebble Android » puis choisissez ‘Properties’ puis cliquez sur ‘add’ dans la section ‘Android’ et choisissz ‘PEBBLE_KIT’ puis OK, et encore OK pour fermer la fenêtre ‘Properties’.
Inclure les 3 lignes d’include spécifique à Pebble dans votre code :
import com.getpebble.android.kit.PebbleKit; import com.getpebble.android.kit.PebbleKit.PebbleDataReceiver; import com.getpebble.android.kit.util.PebbleDictionary;
2°) Principes
La jonction entre l’application chargée dans la montre et le Smartphone se fait à l’aide de l’App UUID, identifiant unique de l’application (figurant dans le cloud dans le menu Settings). Bien entendu les couples (Tuples) de données devront être identiques sur la montre et dans l’application Android (cf. page Dialoguer…).
Vous trouverez plus d’information sur le site de Pebble
3°) Exemple :
Nous souhaitons écrire une application qui récupère sur un Smartphone l’état des boutons appuyés sur la Pebble. En retour d’information le smartphone fera sonner la Pebble. Le lancement et l’arrêt de l’application android provoquant le lancement ou l’arrêt de l’application Pebble.
Le code main.c de l’application Pebble :
#include <pebble.h>
enum {
KEY_BUTTON_EVENT = 0,
BUTTON_EVENT_UP = 1,
BUTTON_EVENT_DOWN = 2,
BUTTON_EVENT_SELECT = 3,
KEY_VIBRATION = 4
};
static Window *window;
static TextLayer *text_layer;
void send_int(uint8_t key, uint8_t cmd)
{
DictionaryIterator *iter;
app_message_outbox_begin(&iter);
Tuplet value = TupletInteger(key, cmd);
dict_write_tuplet(iter, &value);
app_message_outbox_send();
}
static void select_click_handler(ClickRecognizerRef recognizer, void *context) {
text_layer_set_text(text_layer, "Select");
send_int(KEY_BUTTON_EVENT, BUTTON_EVENT_SELECT);
}
static void up_click_handler(ClickRecognizerRef recognizer, void *context) {
text_layer_set_text(text_layer, "Up");
send_int(KEY_BUTTON_EVENT, BUTTON_EVENT_UP);
}
static void down_click_handler(ClickRecognizerRef recognizer, void *context) {
text_layer_set_text(text_layer, "Down");
send_int(KEY_BUTTON_EVENT, BUTTON_EVENT_DOWN);
}
static void click_config_provider(void *context) {
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
}
static void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
text_layer = text_layer_create((GRect) { .origin = { 0, 72 }, .size = { bounds.size.w, 20 } });
text_layer_set_text(text_layer, "Press a button");
text_layer_set_text_alignment(text_layer, GTextAlignmentCenter);
layer_add_child(window_layer, text_layer_get_layer(text_layer));
}
static void window_unload(Window *window) {
text_layer_destroy(text_layer);
}
static void in_received_handler(DictionaryIterator *iter, void *context)
{
Tuple *t = dict_read_first(iter);
if(t)
{
vibes_short_pulse();
}
}
static void init(void) {
window = window_create();
window_set_click_config_provider(window, click_config_provider);
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
//Register AppMessage events
app_message_register_inbox_received(in_received_handler);
app_message_open(512, 512); //Large input and output buffer sizes
const bool animated = true;
window_stack_push(window, animated);
}
static void deinit(void) {
window_destroy(window);
}
int main(void) {
init();
APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window);
app_event_loop();
deinit();
}
Le code Android :
package com.andrologiciels.andropebble;
import java.util.UUID;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import com.getpebble.android.kit.PebbleKit;
import com.getpebble.android.kit.PebbleKit.PebbleDataReceiver;
import com.getpebble.android.kit.util.PebbleDictionary;
public class MainActivity extends Activity {
private PebbleDataReceiver mReceiver; //-- Pour recevoir les données
private TextView mButtonView; //-- Affichage des infos recues
//-- Identification de l'application Pebble à récupérer dans le menu Settings du Cloud
private final static UUID PEBBLE_APP_UUID = UUID.fromString("2672b126-0c95-4454-8c90-93237ea097fd");
private static final int //-- Définition des événements en provenance de Pebble
KEY_BUTTON_EVENT = 0,
BUTTON_EVENT_UP = 1,
BUTTON_EVENT_DOWN = 2,
BUTTON_EVENT_SELECT = 3,
KEY_VIBRATION = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//-- Initialisation de l'écran
mButtonView = new TextView(this);
mButtonView.setText("No button yet!");
setContentView(mButtonView);
//-- Lancement de l'application Pebble
PebbleKit.startAppOnPebble(getApplicationContext(), PEBBLE_APP_UUID);
}
@Override
protected void onResume() {
super.onResume();
//-- Réception des données en provenance de la Pebble
mReceiver = new PebbleDataReceiver(PEBBLE_APP_UUID) {
@Override
public void receiveData(Context context, int transactionId, PebbleDictionary data) {
//-- Accusé de réception du message (ACK)
PebbleKit.sendAckToPebble(context, transactionId);
//-- Contrôle de l'existance de la clef
if(data.getUnsignedInteger(KEY_BUTTON_EVENT) != null) {
int button = data.getUnsignedInteger(KEY_BUTTON_EVENT).intValue();
switch(button) {
case BUTTON_EVENT_UP:
//-- Le boutton UP a été appuyé
mButtonView.setText("UP button pressed!");
break;
case BUTTON_EVENT_DOWN:
//-- DOWN
mButtonView.setText("DOWN button pressed!");
break;
case BUTTON_EVENT_SELECT:
//-- SELECT
mButtonView.setText("SELECT button pressed!");
break;
}
}
//-- Action à effectuer en retour (du téléphone vers la montre)
//-- Par exemple vibration de la montre
PebbleDictionary dict = new PebbleDictionary();
dict.addInt32(KEY_VIBRATION, 0);
PebbleKit.sendDataToPebble(context, PEBBLE_APP_UUID, dict);
}
};
//-- Enregistrement de l'Handler
PebbleKit.registerReceivedDataHandler(this, mReceiver);
}
@Override
protected void onPause() {
super.onPause();
if (mReceiver != null) {
unregisterReceiver(mReceiver);
mReceiver = null;
}
}
@Override
protected void onDestroy() {
//-- A la fermeture de l'application on demande la fermeture de l'appli sur la montre
super.onDestroy();
if (mReceiver != null) {
unregisterReceiver(mReceiver);
mReceiver = null;
}
PebbleKit.closeAppOnPebble(getApplicationContext(), PEBBLE_APP_UUID);
}
}
Le code source Pebble est ici https://app.box.com/s/ac4to24446lumeny4hha et le code Android là https://app.box.com/s/78wcxloy94u74fxj6kj0
Votre commentaire