Messages….

DANS LE CODE C

Déclaration des couples de données à envoyer (code ; valeur) :

 enum {  
  KEY_PRESS=0,  
  KEY_LONGPRESS=4  
 };  

Envoi des messages :

 //-- Write message to buffer & send  
 static void send_appmessage(int key, int value) {  
   DictionaryIterator *iter;  
   app_message_outbox_begin(&iter);  
   Tuplet tuplet = TupletInteger(key, value);  
   dict_write_tuplet(iter, &tuplet);  
   app_message_outbox_send();  
 }   

Appel de la fonction :

 send_appmessage(KEY_LONGPRESS, 1);  

Réception de données :

 //-- Traitement des valeurs reçues
 static void in_received_handler(DictionaryIterator *received, void *context) {  
      Tuple *tuple;  
      char string_value[256];  
      tuple = dict_find(received, GARE_DEP);  
      if(tuple) {  
           // Numérique : APP_LOG(APP_LOG_LEVEL_DEBUG, "Received Status: %d", (int)tuple->value->uint32);   
           // String :    APP_LOG(APP_LOG_LEVEL_DEBUG, "Received Message: %s", tuple->value->cstring);  
           //APP_LOG(APP_LOG_LEVEL_DEBUG, "Gare Départ: %s", tuple->value->cstring);  
           strcpy(string_value, tuple->value->cstring);  
           //APP_LOG(APP_LOG_LEVEL_DEBUG, "Gare Départ: %s", string_value);  
           snprintf(dep_buffer, 32, "De %s", string_value);  
           //APP_LOG(APP_LOG_LEVEL_DEBUG, "Gare Départ: %s", dep_buffer);  
           text_layer_set_text(dep_layer, (char*) &dep_buffer);  
      }  
 }  

Déclaration dans la fonction init() des events d’envoi et de réception :

      //Register AppMessage events  
      app_message_register_inbox_received(in_received_handler);                            
      app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());          //Largest possible input and output buffer sizes  

Gestion des erreurs  :

 // Called when an incoming message from PebbleKitJS is dropped  
 static void in_dropped_handler(AppMessageResult reason, void *context) {  
  char buf[] = "1";  
  char cMsg[80];  
  int nRes=reason;  
  snprintf(buf, sizeof(buf), "%d", reason);  
  //strcpy (cMsg, "Drop: ");  
  //strcat (cMsg,buf);  
  switch ( nRes ) {  
  case APP_MSG_OK:  
    strcat (cMsg,"Actualisation ok");  
    break;  
  case APP_MSG_SEND_TIMEOUT:  
    strcat (cMsg,"TimeOut...");  
    break;  
  case APP_MSG_SEND_REJECTED:  
    strcat (cMsg,"Rejet...");  
    break;   
  case APP_MSG_NOT_CONNECTED:  
    strcat (cMsg,"Pas connecté...");  
    break;  
  case APP_MSG_APP_NOT_RUNNING:  
    strcat (cMsg,"Téléphone pb...");  
    break;     
  case APP_MSG_BUSY:  
    strcat (cMsg,"Busy...");  
    break;   
  case APP_MSG_BUFFER_OVERFLOW:  
    strcat (cMsg,"Overflow...");  
    break;    
  case APP_MSG_OUT_OF_MEMORY:  
    strcat (cMsg,"Err mémoire...");  
    break;      
  default:  
    strcat (cMsg,"Erreur ");  
    strcat (cMsg,buf);  
    /*  
     APP_MSG_OK //All good, operation was successful.  
     APP_MSG_SEND_TIMEOUT //The other end did not confirm receiving the sent data with an (n)ack in time.  
     APP_MSG_SEND_REJECTED      //The other end rejected the sent data, with a "nack" reply.  
     APP_MSG_NOT_CONNECTED      //The other end was not connected.  
     APP_MSG_APP_NOT_RUNNING      //The local application was not running.  
     APP_MSG_INVALID_ARGS      //The function was called with invalid arguments.  
     APP_MSG_BUSY      //There are pending (in or outbound) messages that need to be processed first before new ones can be received or sent.  
     APP_MSG_BUFFER_OVERFLOW      //The buffer was too small to contain the incoming message.  
     APP_MSG_ALREADY_RELEASED      //The resource had already been released.  
     APP_MSG_CALLBACK_ALREADY_REGISTERED      //The callback node was already registered, or its ListNode has not been initialized.  
     APP_MSG_CALLBACK_NOT_REGISTERED      //The callback could not be deregistered, because it had not been registered before.  
     APP_MSG_OUT_OF_MEMORY      //The support library did not have sufficient application memory to perform the requested operation.  
     APP_MSG_CLOSED      //App message was closed.  
     APP_MSG_INTERNAL_ERROR   
    */  
    break;  
  }   
  APP_LOG(APP_LOG_LEVEL_DEBUG, cMsg);  
  snprintf(err_buffer, 32, "Drop: %s", cMsg);  
  text_layer_set_text(update_layer, (char*) &err_buffer);   
 }  

Idem pour
// Called when PebbleKitJS does not acknowledge receipt of a message
static void out_failed_handler(DictionaryIterator *failed, AppMessageResult reason, void *context) {}

Dans init() : Initialisation des handlers :

   app_message_register_inbox_dropped(in_dropped_handler); //((AppMessageInboxDropped) in_dropped)  
   app_message_register_outbox_failed(out_failed_handler); //((appMessageOutboxFailed)failed_callback)  

….

 

Publicité

Votre commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l’aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l’aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s

Articles récents
Commentaires récents
Catégories
Archives
%d blogueurs aiment cette page :