如何使用Qt从iOS连接蓝牙低功耗设备?

时间:2018-01-16 16:24:15

标签: android ios qt bluetooth uuid

我正在创建一个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上我从

获取mac地址
  

QBluetoothDeviceInfo

但是iOS并没有从ble获取mac地址,只有UUID。 我尝试在线搜索doc,但我没有找到关于通过uuid连接ios和蓝牙服务的事情。

有一种模式可以在mac地址中转换uuid,或者有一个库可以连接服务设备和带有UUID的ios。

2 个答案:

答案 0 :(得分:0)

你应该使用QLowEnergyController 如果您将设备用作外设外观this方法。 我创建包含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);
}

答案 1 :(得分:0)

无法通过在iOS上进行扫描来获取BLE外围设备的MAC地址。这意味着您可能需要在BLE外围板上找到或添加一些其他数据,以便能够在BLE扫描期间对其进行识别。

有关更多详细信息,请参见以下答案:Get MAC address of bluetooth low energy peripheral