bool JTBLEObject::connectToDevice()
{
bool ret = false;
if(controller){
delete controller;
}
controller = new QLowEnergyController(*info,this);
if(controller)
{
connect(controller, SIGNAL(connected()),
this, SLOT(deviceConnected()));
connect(controller, SIGNAL(error(QLowEnergyController::Error)),
this, SLOT(errorReceived(QLowEnergyController::Error)));
connect(controller, SIGNAL(disconnected()),
this, SLOT(deviceDisconnected()));
connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)),
this, SLOT(addLowEnergyService(QBluetoothUuid)));
connect(controller, SIGNAL(discoveryFinished()),
this, SLOT(serviceScanDone()));
toTimer.setInterval(CONTROLLER_CONNECT_TO);
toTimer.start();
controller->connectToDevice();
set_status(Connecting);
ret = true;
}
return ret;
}
void JTBLEObject::deviceConnected()
{
toTimer.start();
discoverServiceTimer.start();
controller->discoverServices();
set_status(ServDiscovering);
}
void JTBLEObject::deviceDisconnected(){
// Status could be in "Detected" as a micro connection can happen. So remove only if the status is "Connected"
if(get_status() == Connected){
set_running(false);
set_expanded(false);
qDebug() << "notification.isValid()" << notification.isValid();
if(serviceList.
value(QBluetoothUuid
(QString(THERAPY_SERVICE_UUID
)))){ QLowEnergyService
* m_service
= serviceList.
value(QBluetoothUuid
(QString(THERAPY_SERVICE_UUID
)));
qDebug() << "service contains" << m_service->contains(notification);
qDebug() << "service error" << m_service->error();
m_service
->writeDescriptor
(notification,
QByteArray::fromHex("0000"));
}
batteryReadTimer.stop();
set_status(Removing);
emit bleControllerEvent();
}
}
void JTBLEObject::connectToService(QLowEnergyService* service)
{
discoverServiceTimer.stop();
if(service->state() == QLowEnergyService::DiscoveryRequired)
{
connect(service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)),
this, SLOT(serviceDetailsDiscovered(QLowEnergyService::ServiceState)));
connect(service, SIGNAL(characteristicRead(const QLowEnergyCharacteristic&, const QByteArray&)),
this, SLOT(readCharacteristicsFromBLE(const QLowEnergyCharacteristic&, const QByteArray&)));
connect(service, SIGNAL(characteristicChanged(const QLowEnergyCharacteristic&, const QByteArray&)),
this, SLOT(readCharacteristicsFromBLE(const QLowEnergyCharacteristic&, const QByteArray&)));
service->discoverDetails();
set_status(CharDiscovering);
}
}
void JTBLEObject::readCharacteristicsFromBLE(const QLowEnergyCharacteristic& carac, const QByteArray& data)
{
if(carac.
uuid() == QBluetoothUuid
(QString(PROGRAM_MSB_UUID
))){ .
.
.
}
else if(carac.
uuid() == QBluetoothUuid
(QString(THERAPY_PROGRAM_LSB_UUID
))) {
char tempData[16];
int max = data.size() > 16 ? 16 : data.size();
for(int i=0; i < max; i++)
{
tempData[i] = data.at(i);
}
set_timeValue(((quint8)tempData[1]) << 8 | ((quint8)tempData[2]));
set_running((tempData[3] & 0x01) == 0x01);
if(!notification.isValid()){
notification = carac.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
if (notification.isValid())
{
QLowEnergyService *service = qobject_cast<QLowEnergyService *>(sender());
service
->writeDescriptor
(notification,
QByteArray::fromHex("0100"));
qDebug() << "Enable Notification on Status - OK";
}
}
}
else if(carac.uuid() == QBluetoothUuid::BatteryLevel)
{
this->set_batteryValue(data.at(0) & 0x7F);
this->set_batteryCharging((data.at(0) & 0x80) == 0x80);
}
batteryReadTimer.start();
}
bool JTBLEObject::connectToDevice()
{
bool ret = false;
if(controller){
delete controller;
}
controller = new QLowEnergyController(*info,this);
if(controller)
{
connect(controller, SIGNAL(connected()),
this, SLOT(deviceConnected()));
connect(controller, SIGNAL(error(QLowEnergyController::Error)),
this, SLOT(errorReceived(QLowEnergyController::Error)));
connect(controller, SIGNAL(disconnected()),
this, SLOT(deviceDisconnected()));
connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)),
this, SLOT(addLowEnergyService(QBluetoothUuid)));
connect(controller, SIGNAL(discoveryFinished()),
this, SLOT(serviceScanDone()));
toTimer.setInterval(CONTROLLER_CONNECT_TO);
toTimer.start();
controller->connectToDevice();
set_status(Connecting);
ret = true;
}
return ret;
}
void JTBLEObject::deviceConnected()
{
toTimer.start();
discoverServiceTimer.start();
controller->discoverServices();
set_status(ServDiscovering);
}
void JTBLEObject::deviceDisconnected(){
// Status could be in "Detected" as a micro connection can happen. So remove only if the status is "Connected"
if(get_status() == Connected){
set_running(false);
set_expanded(false);
qDebug() << "notification.isValid()" << notification.isValid();
if(serviceList.value(QBluetoothUuid(QString(THERAPY_SERVICE_UUID)))){
QLowEnergyService* m_service = serviceList.value(QBluetoothUuid(QString(THERAPY_SERVICE_UUID)));
qDebug() << "service contains" << m_service->contains(notification);
qDebug() << "service error" << m_service->error();
m_service->writeDescriptor(notification, QByteArray::fromHex("0000"));
}
batteryReadTimer.stop();
set_status(Removing);
emit bleControllerEvent();
}
}
void JTBLEObject::connectToService(QLowEnergyService* service)
{
discoverServiceTimer.stop();
if(service->state() == QLowEnergyService::DiscoveryRequired)
{
connect(service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)),
this, SLOT(serviceDetailsDiscovered(QLowEnergyService::ServiceState)));
connect(service, SIGNAL(characteristicRead(const QLowEnergyCharacteristic&, const QByteArray&)),
this, SLOT(readCharacteristicsFromBLE(const QLowEnergyCharacteristic&, const QByteArray&)));
connect(service, SIGNAL(characteristicChanged(const QLowEnergyCharacteristic&, const QByteArray&)),
this, SLOT(readCharacteristicsFromBLE(const QLowEnergyCharacteristic&, const QByteArray&)));
service->discoverDetails();
set_status(CharDiscovering);
}
}
void JTBLEObject::readCharacteristicsFromBLE(const QLowEnergyCharacteristic& carac, const QByteArray& data)
{
if(carac.uuid() == QBluetoothUuid(QString(PROGRAM_MSB_UUID))){
.
.
.
}
else if(carac.uuid() == QBluetoothUuid(QString(THERAPY_PROGRAM_LSB_UUID)))
{
char tempData[16];
int max = data.size() > 16 ? 16 : data.size();
for(int i=0; i < max; i++)
{
tempData[i] = data.at(i);
}
set_timeValue(((quint8)tempData[1]) << 8 | ((quint8)tempData[2]));
set_running((tempData[3] & 0x01) == 0x01);
if(!notification.isValid()){
notification = carac.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
if (notification.isValid())
{
QLowEnergyService *service = qobject_cast<QLowEnergyService *>(sender());
service->writeDescriptor(notification, QByteArray::fromHex("0100"));
qDebug() << "Enable Notification on Status - OK";
}
}
}
else if(carac.uuid() == QBluetoothUuid::BatteryLevel)
{
this->set_batteryValue(data.at(0) & 0x7F);
this->set_batteryCharging((data.at(0) & 0x80) == 0x80);
}
batteryReadTimer.start();
}
To copy to clipboard, switch view to plain text mode
Bookmarks