Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions BBPebbleLib/Pebble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Author: James
*/

#include <QtEndian>

#include "Pebble.h"

const quint8 Pebble::PEBBLE_CLIENT_VERSION = 2;
Expand Down Expand Up @@ -38,31 +40,48 @@ void Pebble::onDisconnect()

void Pebble::onDataReceived(QBluetoothSocket &bt_socket)
{
QDataStream in(&bt_socket);

if (this->payloadSize == 0) {
if (bt_socket.bytesAvailable() < (int)2*sizeof(quint16)) {
static const int header_length = (int)2*sizeof(quint16);

while (bt_socket.bytesAvailable() >= header_length) {
// Take a look at the header, but do not remove it from the socket input buffer.
// We will only remove it once we're sure the entire packet is in the buffer.
uchar header[header_length];
bt_socket.peek(reinterpret_cast<char*>(header), header_length);

quint16 message_length = qFromBigEndian<quint16>(&header[0]);
quint16 endpoint = qFromBigEndian<quint16>(&header[2]);

// Sanity checks on the message_length
if (message_length == 0) {
qDebug() << "received empty message";
bt_socket.read(header_length); // skip this header
continue; // check if there are additional headers.
} else if (message_length > 8 * 1024) {
// Protocol does not allow messages more than 8K long, seemingly.
qDebug() << "received message size too long: " << message_length;
bt_socket.readAll(); // drop entire input buffer
return;
}
in >> this->payloadSize;
in >> this->endPoint;
}

if(bt_socket.bytesAvailable() < this->payloadSize){
return;
}
char buffer[this->payloadSize];
in.readRawData(buffer, this->payloadSize);
QByteArray payload(buffer);
// Now wait for the entire message
if (bt_socket.bytesAvailable() < header_length + message_length) {
qDebug() << "incomplete msg body in read buffer";
return; // try again once more data comes in
}

qDebug() << "Received data. Endpoint : " << endPoint << " Payload Size : " << payloadSize << " HEX : " << payload.toHex() << "STR : " << QString(payload);
// We can now safely remove the header from the input buffer,
// as we know the entire message is in the input buffer.
bt_socket.read(header_length);

emit onDataReadFinished(endPoint, payload);
// Now read the rest of the message
QByteArray data = bt_socket.read(message_length);

this->payloadSize = 0;
this->endPoint = 0;
qDebug() << "<<<< received data. Endpoint : " << endpoint << " Payload Size : " << data.size() << " HEX : " << data.toHex() << "STR : " << QString(data);

emit onDataReadFinished(endpoint, data);

onDataReceived(bt_socket);
onDataReceived(bt_socket);
}
}

void Pebble::onDataReadFinished(quint16 endPoint, const QByteArray &payload){
Expand All @@ -88,6 +107,9 @@ void Pebble::sendDataToPebble(quint16 endPoint, const QByteArray &payload) const
in << (quint16)endPoint;
finalData.append(payload);
in.setByteOrder(QDataStream::BigEndian);

qDebug() << ">>>> sent data. Endpoint : " << endPoint << " Payload Size : " << payload.size() << " HEX : " << payload.toHex() << "STR : " << QString(payload);

this->bt_device->sendData(finalData);
}

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ I am expecting (hopefully) to not be the only one to contribute to this project
* Get information of an application
* Displaying Weather on a watchface
* Much much much more

### Related Work
Thanks to all the other projects which achieved communication with the Pebble watch and inspired/helped this project, namely:
* https://github.com/smokku/pebble
* https://github.com/Hexxeh/libpebble
* https://github.com/pebble/libpebble2
* https://github.com/Keboo/PebbleSharp
* https://github.com/DouweM/pebblewatch
* https://github.com/barometz/flint