5 #include <QtConnectivity/QBluetoothAddress>
6 #include <QtConnectivity/QBluetoothSocket>
9 class BtConnector : public QObject
13 explicit BtConnector(QObject *parent = 0){qDebug("BtConnector Constructor");}
14 ~BtConnector(){if(socket != NULL) delete socket;}
16 Q_INVOKABLE void connect(QString address, int port){
17 qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
21 socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
22 QObject::connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
23 QObject::connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
24 QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SIGNAL(error(QBluetoothSocket::SocketError)));
26 qDebug("Connecting...");
27 socket->connectToService(QBluetoothAddress(address), port);
30 QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
47 void error(QBluetoothSocket::SocketError errorCode);
49 void stickMoved(int x, int y);
50 void buttonsChanged(bool a, bool b, bool c, bool d);
54 // qDebug("readData...");
55 QByteArray data = socket->readAll();
56 // qDebug("read %d bytes.", data.size());
59 for(int i=0; i < data.size(); i++){
60 qDebug("%d: %d", i, ((signed char)data.at(i)));
65 * Actually it seems like that the first three bytes are used for
66 * identifying the "type" of data sent. However, for now using the
67 * first seems to suffice.
71 emit(stickMoved((int)(signed char) data.at(4), (int)(signed char) data.at(5)));
72 }else if(data.at(0) == 8){
75 * A -> 0, B -> 1, C -> 2, D ->3
76 * At index 3 to 6 (inclusive)
81 for(int i = 3; i <= 6; i++){
82 for(int b = 0; b <= 3; b++){
84 buttonMap ^= (1 << b);
89 qDebug("Button map: %d", buttonMap);
90 emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
95 QBluetoothSocket *socket;
99 #endif // BTCONNECTOR_H