简体   繁体   English

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

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

I am creating a Qt application, where I connect the iOS with BLE board. 我正在创建一个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);
}

after read the ble component in near, i connect the Android device like this: 读完附近的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();

}

so, on Android i get the mac address from 因此,在Android上,我从

QBluetoothDeviceInfo QBluetoothDeviceInfo

but iOS doesn't get me the mac address from ble, but only the UUID. 但是iOS不能从ble获得我的mac地址,而只能从UUID获得。 I tried to search online on doc, but i didn't find something about connect the ios and bluetooth service through uuid. 我尝试在线搜索doc,但没有找到有关通过uuid连接ios和蓝牙服务的信息。

There is a mode to convert uuid in mac address or there is a library to connect service ble device and ios with UUID. 有一种在Mac地址中转换uuid的模式,或者有一个库,用于通过UUID连接服务ble设备和ios。

You should use QLowEnergyController If you use your device as peripheral look this method. 您应该使用QLowEnergyController。如果将设备用作外围设备,请使用方法。 I create class that contains instance of QLowEnergyController and works with it. 我创建了包含QLowEnergyController实例的类并对其进行处理。 Here is part of code that i use in my application: 这是我在应用程序中使用的部分代码:

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);
}

There isn't a way to get the MAC address of BLE peripheral devices by scanning on iOS. 无法通过在iOS上进行扫描来获取BLE外围设备的MAC地址。 This means that you may need to find or add some additional data on your BLE peripheral board to be able to identify it during a BLE scan. 这意味着您可能需要在BLE外围板上找到或添加一些其他数据,以便能够在BLE扫描期间识别它。

See this answer for more details: Get MAC address of bluetooth low energy peripheral 有关更多详细信息,请参见此答案: 获取蓝牙低功耗外设的MAC地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM