bluetoothmanager.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**********************************************************************
  2. ** Copyright (C) 2009 Nokia Corporation.
  3. ** All rights reserved.
  4. **
  5. ** $LICENSE_TEXT$
  6. **********************************************************************/
  7. #include "bluetoothmanager.h"
  8. #include "btsingledeviceselector.h"
  9. #include "btserialportclient.h"
  10. #include "serialCommHandler/qserialport.h"
  11. #include <QDebug>
  12. static const QString defaultAddress("00:01:95:0a:1f:74");
  13. BluetoothManager::BluetoothManager(QObject *parent) :
  14. QObject(parent)
  15. {
  16. m_isTryingDefault = true;
  17. mp_ioDevice = NULL;
  18. mp_deviceSelector = new BtSingleDeviceSelector(this);
  19. mp_serialClient = new BtSerialPortClient(this);
  20. setupConnections();
  21. }
  22. BluetoothManager::~BluetoothManager()
  23. {
  24. if (mp_ioDevice != NULL) {
  25. if (mp_ioDevice->isOpen())
  26. mp_ioDevice->close();
  27. delete mp_ioDevice;
  28. }
  29. }
  30. void BluetoothManager::setupConnections()
  31. {
  32. QObject::connect(mp_deviceSelector, SIGNAL(deviceSelected(QString)), this, SLOT(
  33. setupSerialConnection(QString)));
  34. QObject::connect(mp_serialClient, SIGNAL(interfaceBounded(QIODevice*)), this, SLOT(setupIODevice(QIODevice*)));
  35. QObject::connect(mp_serialClient, SIGNAL(error()), this, SLOT(processError()));
  36. QObject::connect(mp_serialClient, SIGNAL(disconnected()), this, SLOT(processDisconnected()));
  37. QObject::connect(this, SIGNAL(deviceDefined(QString)), mp_serialClient, SLOT(setupInterface(
  38. QString)));
  39. }
  40. void BluetoothManager::setupSerialConnection(QString serialDevice)
  41. {
  42. if (!serialDevice.isEmpty())
  43. emit deviceDefined(serialDevice);
  44. else
  45. emit disconnected();
  46. }
  47. void BluetoothManager::setupIODevice(QIODevice* device)
  48. {
  49. mp_ioDevice = device;
  50. if (mp_ioDevice != NULL) {
  51. QObject::connect(mp_ioDevice, SIGNAL(readyRead()), this, SLOT(processNewData()));
  52. mp_ioDevice->open(QIODevice::ReadWrite);
  53. emit connected();
  54. return;
  55. }
  56. emit disconnected();
  57. }
  58. void BluetoothManager::connect()
  59. {
  60. m_isTryingDefault = true;
  61. setupSerialConnection(defaultAddress);
  62. }
  63. void BluetoothManager::disconnect()
  64. {
  65. mp_serialClient->disconnect();
  66. }
  67. void BluetoothManager::sendData(QString data)
  68. {
  69. qDebug() << "send data: " << data;
  70. if (mp_ioDevice != NULL) {
  71. mp_ioDevice->write(data.toLatin1());
  72. }
  73. }
  74. void BluetoothManager::processNewData()
  75. {
  76. if (mp_ioDevice != NULL) {
  77. qDebug() << "available bytes: " << mp_ioDevice->bytesAvailable();
  78. QByteArray data = mp_ioDevice->readAll();
  79. qDebug() << "manager data: " << data;
  80. emit dataReceived(data);
  81. }
  82. }
  83. void BluetoothManager::processError()
  84. {
  85. if (m_isTryingDefault) {
  86. m_isTryingDefault = false;
  87. mp_deviceSelector->show();
  88. return;
  89. }
  90. emit disconnected();
  91. }
  92. void BluetoothManager::processDisconnected()
  93. {
  94. qDebug() << "disconnected";
  95. if (mp_ioDevice != NULL) {
  96. mp_ioDevice->close();
  97. }
  98. emit disconnected();
  99. }