0

我正在创建一个非常简单的 pebble 应用程序。

目标: 当我点击我的 Android 应用程序时,Pebble 应用程序会显示我从我的 Android 应用程序发送的消息。

问题: 文本不会更改/显示在鹅卵石上。

代码

Pebble .c 代码:

#include "pebble_os.h"
#include "pebble_app.h"
#include "pebble_fonts.h"


#define MY_UUID { 0x98, 0x15, 0xA8, 0xDA, 0x6C, 0xAC, 0x40, 0xB0, 0xAD, 0x87, 0xFC, 0xDF, 0x9E, 0x86, 0x3E, 0x91 }
PBL_APP_INFO(MY_UUID,
             "Phone App", "Me",
             1, 0, /* App version */
             DEFAULT_MENU_ICON,
             APP_INFO_STANDARD_APP);

Window window;
TextLayer hello_layer;
AppMessageCallbacksNode messageCallBacks;

void myMessageHandler(DictionaryIterator* received, void* context) {
    vibes_short_pulse();
    Tuple* tuple = dict_find(received, 0);
    text_layer_set_text(&hello_layer, tuple->value->cstring);
}

void handle_init(AppContextRef ctx) {
    messageCallBacks = (AppMessageCallbacksNode) {
        .callbacks = {
            .in_received = myMessageHandler
        },
        .context = NULL
    };
  window_init(&window, "Window Name");
  window_stack_push(&window, true /* Animated */);

  text_layer_init(&hello_layer, GRect(0, 65, 144, 30));
  text_layer_set_text(&hello_layer, "Hello World!");
  layer_add_child(window_get_root_layer(&window), (Layer*)&hello_layer);
}


void pbl_main(void *params) {
  PebbleAppHandlers handlers = {
    .init_handler = &handle_init,
      .messaging_info = {
          .buffer_sizes = {
              .inbound = 64,
              .outbound = 64
          }
      }
  };
  app_event_loop(params, &handlers);
}

安卓代码:

public class MainActivity extends Activity {
    private Button send;
    private Context context;
    UUID AppId = UUID
            .fromString("9815A8DA-6CAC-40B0-AD87-FCDF9E863E91");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send = (Button) findViewById(R.id.send);
        context = this;

        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {

                        PebbleDictionary data = new PebbleDictionary();
                        data.addString(0, "TEST");
                        PebbleKit.sendDataToPebble(getApplicationContext(), AppId, data);

                    }

                });
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

谢谢!

4

1 回答 1

1

看起来您的方法中缺少一个app_message_register_callbacks调用handle_init

添加这个应该可以解决问题:

app_message_register_callbacks(&messageCallBacks);

您仍在使用现已弃用的 SDK 1.x。我强烈建议您升级到 SDK 2.0。开发和调试工具要好得多。您还将有更多的 API 可供使用。

于 2014-03-04T03:28:35.360 回答