Exemple #1
0
 public void SendEndpointMsg(PebbleEndpointType type, byte[] data, EndpointMsgCallback callback)
 {
     byte[] buf = new byte[data.Length + 5];
     //The first byte is the message type, which is PEBBLE_PROTOCOL_PHONE_TO_WATCH
     buf[0] = (byte)CloudPebbleCode.PEBBLE_PROTOCOL_PHONE_TO_WATCH;
     byte[] length    = FromShort((short)data.Length);
     byte[] typeBytes = FromShort((short)type);
     //Next two bytes are length of the message.
     length.CopyTo(buf, 1);
     //Next two bytes are the type.
     typeBytes.CopyTo(buf, 3);
     //The remainder is the message itself
     data.CopyTo(buf, 5);
     //Send this message and wait for a reply.
     SendDataGetReplyType(buf, CloudPebbleCode.PEBBLE_PROTOCOL_WATCH_TO_PHONE, (byte[] reply, object user) =>
     {
         //Read this in and see if the reply type matches the one we requested.
         using (MemoryStream ms = new MemoryStream(reply))
         {
             ms.Position += 1;
             PebbleProtocolMessage msg = new PebbleProtocolMessage(ms, CloudPebbleCode.PEBBLE_PROTOCOL_WATCH_TO_PHONE);
             if (msg.id == type)
             {
                 //This is what we wanted. Prevent the default and call callback.
                 return(callback(msg));
             }
             else
             {
                 //Not what we wanted.
                 return(AfterInterruptAction.NoPreventDefault_ContinueInterrupt);
             }
         }
     }, null);
 }
        public AfterInterruptAction OnGotData(PebbleProtocolMessage msg)
        {
            Log("Got screenshot chunk of size " + msg.data.Length);
            if (chunkCount == 0)
            {
                //This is a header chunk. Open it.
                using (MemoryStream ms = new MemoryStream(msg.data))
                {
                    ms.Position   += 1;
                    version        = ReadInt32(ms);
                    bits_per_pixel = 8;
                    if (version == 1)
                    {
                        bits_per_pixel = 1;
                    }

                    width              = ReadInt32(ms);
                    height             = ReadInt32(ms);
                    target_buffer_size = (width * height);
                    if (version == 1)
                    {
                        target_buffer_size = target_buffer_size / 8; //Version 1 images are 1bpp
                    }
                    //Create the buffer based on the width and height.
                    bmp_buffer = new byte[width * height];
                    Log("(Debug) Created image of size " + width.ToString() + "x" + height.ToString());
                    buffer_pos = 0;
                    //Copy the remainder of this content to the buffer.
                    byte[] buf = new byte[ms.Length - ms.Position];
                    ms.Read(buf, 0, buf.Length);
                    buf.CopyTo(bmp_buffer, buffer_pos);
                    buffer_pos += buf.Length;
                }
            }
            else
            {
                //Copy this content to the buffer.
                msg.data.CopyTo(bmp_buffer, buffer_pos);
                buffer_pos += msg.data.Length;
                Log("(Debug) Buffer completeness: " + buffer_pos.ToString() + "/" + target_buffer_size.ToString());
            }
            //If the buffer is complete, convert this into an actual image.
            if (buffer_pos == target_buffer_size)
            {
                Log("Buffer full. Creating image!");
                FinalizeImage();
            }
            chunkCount++;
            return(AfterInterruptAction.PreventDefault_ContinueInterrupt);
        }
        private void OnPPMMessage(PebbleProtocolMessage ppm)
        {
            //Send this on to the web client.
            Dictionary <string, object> data = new Dictionary <string, object>();

            data["msg"]       = ppm.stringData;
            data["direction"] = ((int)ppm.direction).ToString();
            data["type"]      = ppm.id.ToString();
            data["binary"]    = "";
            //Send it to the web client if they are connected.
            if (pair.connected)
            {
                try
                {
                    pair.web.QuickReply(-1, WebPebbleClient.WebPebbleRequestType.PebbleProtocolMsg, data);
                } catch
                {
                }
            }
        }