簡體   English   中英

如何使用Qt從iOS連接到藍牙低功耗設備?

[英]How to connect to a Bluetooth low energy device from iOS with Qt?

我正在創建一個Qt應用程序,將iOS與BLE板連接在一起。

void EasyConnect::startDiscovery()
{
    discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
    discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &EasyConnect::addDevice);
//    connect(discoveryAgent, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error), this, &Device::deviceScanError);
//    connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &Device::deviceScanFinished);

    discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}

讀完附近的ble組件后,我像這樣連接Android設備:

void EasyConnect::connectToService(QBluetoothAddress address)
{
    m_control = new QLowEnergyController(address);
    connect(m_control, &QLowEnergyController::serviceDiscovered, this, &EasyConnect::serviceDiscovered);
//    connect(m_control, &QLowEnergyController::discoveryFinished,
//            this, &DeviceHandler::serviceScanDone);

    connect(m_control, static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
            this, [this](QLowEnergyController::Error error) {
        Q_UNUSED(error);
        qDebug() << "Cannot connect to remote device.";
    });
    connect(m_control, &QLowEnergyController::connected, this, [this]() {

        qDebug() << "controller connect.";
        m_control->discoverServices();
    });
    connect(m_control, &QLowEnergyController::disconnected, this, [this]() {
        qDebug() << "controller disconneted.";
    });

    // Connect
    m_control->connectToDevice();

}

因此,在Android上,我從

QBluetoothDeviceInfo

但是iOS不能從ble獲得我的mac地址,而只能從UUID獲得。 我嘗試在線搜索doc,但沒有找到有關通過uuid連接ios和藍牙服務的信息。

有一種在Mac地址中轉換uuid的模式,或者有一個庫,用於通過UUID連接服務ble設備和ios。

您應該使用QLowEnergyController。如果將設備用作外圍設備,請使用方法。 我創建了包含QLowEnergyController實例的類並對其進行處理。 這是我在應用程序中使用的部分代碼:

void BLEAdvertisementCommunicator::startAdvertisingService() {
    mController = QLowEnergyController::createPeripheral(this);
    mAdvertisingData.setDiscoverability(QLowEnergyAdvertisingData::DiscoverabilityGeneral);
    mAdvertisingData.setIncludePowerLevel(false);
    mAdvertisingData.setLocalName("MyApplication");
    QLowEnergyServiceData serviceData;
    serviceData.setType(QLowEnergyServiceData::ServiceTypePrimary);
    serviceData.setUuid(QBluetoothUuid(serviceUuid));

    auto service = mController->addService(serviceData, mController);

    connect(mController, SIGNAL(connected()), this, SLOT(onDeviceConnected()));
    connect(mController, SIGNAL(disconnected()), this, SLOT(onDeviceDisconnected()));
    connect(mController, SIGNAL(error(QLowEnergyController::Error)), this, SLOT(onError(QLowEnergyController::Error)));
    mResponseData.setServices({QBluetoothUuid(serviceUuid)});
    mController->startAdvertising(QLowEnergyAdvertisingParameters(), mAdvertisingData,
                                  mResponseData);
}

無法通過在iOS上進行掃描來獲取BLE外圍設備的MAC地址。 這意味着您可能需要在BLE外圍板上找到或添加一些其他數據,以便能夠在BLE掃描期間識別它。

有關更多詳細信息,請參見此答案: 獲取藍牙低功耗外設的MAC地址

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM