1

在我的 iOS 应用程序中,我想像这样向手表发送消息:

NSMutableDictionary *message = @{@(1): @(1),
                                 @(2): @"The String"}
[_watch appMessagesPushUpdate:message onSent:^(PBWatch *watch, NSDictionary *update, NSError *error) {
  if (error != nil)
    NSLog(@"Error sending message: %@", error);
}];

如果我像这样发送它,它可以工作。但是,如果我的字符串更长,或者我在字典中添加了 3 或 4 个以上的键,则消息将不会被传递。错误是“应用程序没有及时确认消息”。

在我的卵石应用程序中,我执行以下操作:

static void message_handler(DictionaryIterator *iter, void *context) {
  APP_LOG(APP_LOG_LEVEL_DEBUG, "Received message.");
  Tuple *msg_type_tuple = dict_find(iter, PebbleMessageKeyType);
  Tuple *msg_value_tuple = dict_find(iter, PebbleMessageKeyValue);
  write_line_on_screen(msg_value_tuple->value->cstring);
}
...
// Set sniff interval.
app_comm_set_sniff_interval(SNIFF_INTERVAL_NORMAL); 

// Register message handlers
app_message_register_inbox_received(message_handler);
app_message_register_inbox_dropped(message_dropped);

// Init buffers
app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
APP_LOG(APP_LOG_LEVEL_DEBUG, "App Message set up.");

我的想法是它与消息大小有关。但我无法想象消息只能这么小?在ToDo 列表示例中,我看到他们使用 app_message_open,收件箱参数值为 64,发件箱参数值为 16。这意味着什么单位?64个字符?在 iOS 端,我如何知道我的消息到达鹅卵石时会有多大?

4

1 回答 1

1

调试此类问题时,您应该做的第一件事是添加 message_dropped 处理程序并打印失败原因:

void init() {
   // ...
   app_message_register_inbox_dropped(appmsg_in_dropped);
   app_message_open(...);
   // ... 
}

static void appmsg_in_dropped(AppMessageResult reason, void *context) {
  APP_LOG(APP_LOG_LEVEL_DEBUG, "In dropped: %i", reason);
}

您将在文档中找到原因代码列表。

最常见的两个问题是:

  1. APP_MSG_BUFFER_OVERFLOW: 消息太大(见下文)
  2. APP_MSG_BUSY: 你发消息太快了。在前一条消息得到确认之前,您无法发送新消息。

消息的大小等于字典的大小。dict_calc_buffer_size()的文档解释了如何计算它:

1 byte + 7 bytes for each key + the sum of the sizes of the values

最后,传递给app_message_open()的值是以字节为单位的缓冲区大小。

于 2014-01-15T23:12:00.963 回答