我正在创建一个非常简单的卵石应用程序。
目标: 当我点击我的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);
}
Android代码:
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;
}
}
谢谢!
答案 0 :(得分:1)
您的app_message_register_callbacks
方法似乎错过了handle_init
的来电。
添加这个应该可以解决问题:
app_message_register_callbacks(&messageCallBacks);
您仍在使用SDK 1.x,现已弃用。我强烈建议您升级到SDK 2.0。开发和调试工具更好。您还可以使用更多API。