在Qt应用程序中自动连接并读取来自可调体重秤的数据

在Qt应用程序中自动连接并读取来自可调体重秤的数据,qt,bluetooth,bluetooth-lowenergy,Qt,Bluetooth,Bluetooth Lowenergy,我正在开发一个Qt应用程序,它需要从体重秤模型中读取数据,但不能完全理解蓝牙低能量是如何工作的,以及如何在Qt中实现它。 我有一个UC-352BLE体重秤,它使用BLE发送数据。我想要实现的是: 在将电子秤与运行我的应用程序的Raspberry Pi进行初始配对后,当电子秤发送数据时(测量时它会自动执行),我的应用程序将接收数据。例如,我有一个血压监护仪,它使用普通蓝牙,在这里很容易。在我的应用程序中,我创建了一个QBluetoothServer并调用它的listen()方法。然后,当(已经配对

我正在开发一个Qt应用程序,它需要从体重秤模型中读取数据,但不能完全理解蓝牙低能量是如何工作的,以及如何在Qt中实现它。 我有一个UC-352BLE体重秤,它使用BLE发送数据。我想要实现的是: 在将电子秤与运行我的应用程序的Raspberry Pi进行初始配对后,当电子秤发送数据时(测量时它会自动执行),我的应用程序将接收数据。例如,我有一个血压监护仪,它使用普通蓝牙,在这里很容易。在我的应用程序中,我创建了一个QBluetoothServer并调用它的listen()方法。然后,当(已经配对的)设备发送测量值时,它会自动连接到我的应用程序,然后我创建QBluetoothSocket并读取数据。但有了天平,它是不可能的,看来你不能这样做。我试着按照Qt文档中的说明来做,现在我只需要在秤发送数据时手动按下连接到秤的按钮。每次它连接到设备时,它都会发现它的特征和服务等。我不知道我是否可以这样做,所以当电子秤发送测量值时,应用程序会自动接收连接并读取数据。甚至当我尝试这样连接时,它有时连接,有时不连接(我从QLowEnergyController::error连接时收到未知错误)。以下是我已经拥有的:

Bletest::Bletest(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Bletest)
{
    ui->setupUi(this);

    if (localDevice.isValid()) {
        localDevice.powerOn();
        localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
        connect(&localDevice, &QBluetoothLocalDevice::deviceConnected, this, &Bletest::deviceConnected);
        connect(&localDevice, &QBluetoothLocalDevice::deviceDisconnected, this, &Bletest::deviceDisconnected);
        connect(&localDevice, &QBluetoothLocalDevice::pairingFinished, this, &Bletest::pairingFinished);
    }

    discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &Bletest::addDevice);
    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Bletest::scanFinished);
    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &Bletest::scanFinished);
    discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}

Bletest::~Bletest()
{
    delete ui;
}

// Local device slots
void Bletest::deviceConnected(const QBluetoothAddress &address)
{
    qDebug() << address.toString() << " connected";
}

void Bletest::deviceDisconnected(const QBluetoothAddress &address)
{
    qDebug() << address.toString() << " disconnected";
}

void Bletest::pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
{

}

// Agent slots

void Bletest::addDevice(const QBluetoothDeviceInfo &device)
{
    if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
        if (device.name().contains("352")) {
            bleDevice = device;
            qDebug() << "Found: " + device.name() + "\t" + device.address().toString();
        }
    }
}

void Bletest::scanFinished()
{
    qDebug() << "Devices scan finished";
}

///////////





void Bletest::on_connectButton_clicked()
{
    controller = QLowEnergyController::createCentral(bleDevice, this);
    connect(controller, &QLowEnergyController::serviceDiscovered, this, &Bletest::serviceDiscovered);
    connect(controller, &QLowEnergyController::discoveryFinished, this, &Bletest::serviceScanFinished);
    connect(controller, static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
            this, [this](QLowEnergyController::Error error) {
        qDebug() << "Cannot connect to device: " + QString::number(error);
    });
    connect(controller, &QLowEnergyController::connected, this, [this]() {
        qDebug() << "Connected to device";
        controller->discoverServices();
    });
    connect(controller, &QLowEnergyController::disconnected, this, [this]() {
        qDebug() << "Disconnected";
    });
    controller->connectToDevice();
}

// Controller slots
void Bletest::serviceDiscovered(const QBluetoothUuid &gatt)
{
    qDebug() << "Service discovered: " << gatt.toString();
    if (gatt.toString().contains("0000181d-0000-1000-8000-00805f9b34fb")) {
        service = controller->createServiceObject(QBluetoothUuid(QBluetoothUuid::WeightScale));
        if (service) {
            qDebug() << "Found weight scale service";
            connect(service, &QLowEnergyService::stateChanged, this, &Bletest::serviceStateChanged);
            connect(service, &QLowEnergyService::characteristicChanged, this, &Bletest::updateWeight);
            connect(service, &QLowEnergyService::characteristicRead, this, &Bletest::updateWeight);
            service->discoverDetails();
        }
    }
}

void Bletest::serviceScanFinished()
{
    qDebug() << "Service scan finished";
}
////////////////////////////

// Service slots
void Bletest::serviceStateChanged(QLowEnergyService::ServiceState newState)
{
    if (controller->state() == QLowEnergyController::UnconnectedState)
        return;

    if (newState == QLowEnergyService::DiscoveringServices) {
        qDebug() << "Discovering services state";
    } else if (QLowEnergyService::ServiceDiscovered) {
        qDebug() << "Service discovered.";

        const QLowEnergyCharacteristic weightChar = service->characteristic(QBluetoothUuid(QBluetoothUuid::WeightMeasurement));
        if (!weightChar.isValid())  {
            qDebug() << "Weight data not found";
            return;
        }

        qDebug() << "Weight data found";
        //service->readCharacteristic(weightChar);
        desc = weightChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
        if (desc.isValid()) {
            qDebug() << "Writing descriptor";
            service->writeDescriptor(desc, QByteArray::fromHex("0100"));
        }
    }
}

void Bletest::updateWeight(const QLowEnergyCharacteristic &c, const QByteArray &value)
{
    qDebug() << "Updating weight";

    if (c.uuid() != QBluetoothUuid(QBluetoothUuid::WeightMeasurement))
        return;

    double weight = qFromLittleEndian<qint16>(value.mid(1, 2).data()) * RESOLUTION;

    qDebug() << "New weight: " << value.mid(1, 2);
    qDebug() << "New weight: " + QString::number(weight, 'f', 2);
}
///////////////////////////
Bletest::Bletest(QWidget*parent)
:QMainWindow(父级)
,ui(新ui::Bletest)
{
用户界面->设置用户界面(此);
if(localDevice.isValid()){
localDevice.powerOn();
setHostMode(QBluetoothLocalDevice::HostDiscoverable);
连接(&localDevice,&QBluetoothLocalDevice::deviceConnected,this,&Bletest::deviceConnected);
连接(&localDevice,&QBluetoothLocalDevice::deviceDisconnected,this,&Bletest::deviceDisconnected);
连接(&localDevice,&QBluetoothLocalDevice::pairingFinished,this,&Bletest::pairingFinished);
}
discoveryAgent=新的QBluetooth设备discoveryAgent(本);
连接(discoveryAgent、&QBluetoothDeviceDiscoveryAgent::deviceDiscovered、this、&Bletest::addDevice);
连接(discoveryAgent、&QBluetoothDeviceDiscoveryAgent::finished、this、&Bletest::scanFinished);
连接(discoveryAgent、&QBluetoothDeviceDiscoveryAgent::Cancelled、this、&Bletest::scanFinished);
发现->启动(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
Bletest::~Bletest()
{
删除用户界面;
}
//本地设备插槽
void Bletest::设备已连接(const QBluetooth地址和地址)
{
qDebug()