-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.cpp
119 lines (115 loc) · 4.94 KB
/
device.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "device.h"
Device::Device(QBluetoothDeviceInfo &info,QObject *parent) : QObject(parent), m_info(info)
{
m_control = new QLowEnergyController(info.address());
m_name = info.name();
connect(m_control,SIGNAL(connected()),this,SLOT(deviceConnected()));
connect(m_control,SIGNAL(disconnected()),this,SLOT(deviceDisconnected()));
connect(m_control,SIGNAL(serviceDiscovered(QBluetoothUuid)),this,SLOT(addService(QBluetoothUuid)));
connect(m_control,SIGNAL(discoveryFinished()),this,SLOT(selectService()));
connect(m_control,SIGNAL(error(QLowEnergyController::Error)),this,SLOT(error(QLowEnergyController::Error)));
m_control->connectToDevice();
}
Device::~Device()
{
if(m_service != NULL) {delete m_service;}
m_control->disconnectFromDevice();
delete m_control;
}
void Device::deviceConnected()
{
m_control->discoverServices();
}
void Device::deviceDisconnected()
{
emit devDis(m_info.name());
}
void Device::addService(QBluetoothUuid serv)
{
services.append(serv);
}
void Device::write(QString data)
{
QByteArray tmp;
tmp.append(data);
tmp.append("\r\n");
if(!m_chars.isEmpty())
{
m_service->writeCharacteristic(m_chars.first(),tmp);
}
}
void Device::selectService()
{
m_service = m_control->createServiceObject(services.last());
connect(m_service,SIGNAL(stateChanged(QLowEnergyService::ServiceState)),this,SLOT(selectChar(QLowEnergyService::ServiceState)));
connect(m_service,SIGNAL(characteristicChanged(QLowEnergyCharacteristic,QByteArray)),this,SLOT(characteristicChanged(QLowEnergyCharacteristic,QByteArray)));
connect(m_service,SIGNAL(error(QLowEnergyService::ServiceError)),this,SLOT(error(QLowEnergyService::ServiceError)));
m_service->discoverDetails();
}
void Device::selectChar(QLowEnergyService::ServiceState a)
{
m_chars = m_service->characteristics();
if(a == QLowEnergyService::ServiceDiscovered)
{
emit devCon(m_info.name());
}
}
QString Device::read()
{
return m_chars.first().value();
}
void Device::characteristicChanged(QLowEnergyCharacteristic b, QByteArray a)
{
emit Packet(this->m_info.name(),a);
}
void Device::error(QLowEnergyController::Error er)
{
switch(er)
{
case QLowEnergyController::UnknownRemoteDeviceError:
emit errorMsg(QString("Error from Bluetooth Device(%1): The remote BLE device with the address is not found or no local bluetooth device").arg(m_info.name()));
break;
case QLowEnergyController::NetworkError:
emit errorMsg(QString("Error from Bluetooth Device(%1): the attempt to read from or write to the device failed").arg(m_info.name()));
break;
case QLowEnergyController::InvalidBluetoothAdapterError:
emit errorMsg(QString("Error from Bluetooth Device(%1): bluetooth adapter cannot be found or no local device").arg(m_info.name()));
break;
case QLowEnergyController::ConnectionError:
emit errorMsg(QString("Error from Bluetooth Device(%1): attempt to connect to this device failed").arg(m_info.name()));
break;
case QLowEnergyController::AdvertisingError:
emit errorMsg(QString("Error from Bluetooth Device(%1): attempt to start advertising failed").arg(m_info.name()));
break;
case QLowEnergyController::RemoteHostClosedError:
emit errorMsg(QString("Error from Bluetooth Device(%1): device closed connection").arg(m_info.name()));
break;
case QLowEnergyController::UnknownError:
emit errorMsg(QString("Error from Bluetooth Device(%1): Unknown error").arg(m_info.name()));
break;
}
}
void Device::error(QLowEnergyService::ServiceError er)
{
switch(er)
{
case QLowEnergyService::OperationError:
emit errorMsg(QString("Error from Bluetooth Device(%1): An operation was attempted while the service was not ready.").arg(m_info.name()));
break;
case QLowEnergyService::CharacteristicReadError:
emit errorMsg(QString("Error from Bluetooth Device(%1):An attempt to read a characteristic value failed.").arg(m_info.name()));
break;
case QLowEnergyService::CharacteristicWriteError:
emit errorMsg(QString("Error from Bluetooth Device(%1): An attempt to write a new value to a characteristic failed.").arg(m_info.name()));
break;
case QLowEnergyService::DescriptorReadError:
emit errorMsg(QString("Error from Bluetooth Device(%1): An attempt to write a new value to a descriptor failed. ").arg(m_info.name()));
break;
case QLowEnergyService::DescriptorWriteError:
emit errorMsg(QString("Error from Bluetooth Device(%1): attempt to start advertising failed").arg(m_info.name()));
break;
case QLowEnergyService::UnknownError:
emit errorMsg(QString("Error from Bluetooth Device(%1): An unknown error occurred when interacting with the service. ").arg(m_info.name()));
break;
}
}