简体   繁体   中英

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.

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:

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

QBluetoothDeviceInfo

but iOS doesn't get me the mac address from ble, but only the UUID. I tried to search online on doc, but i didn't find something about connect the ios and bluetooth service through uuid.

There is a mode to convert uuid in mac address or there is a library to connect service ble device and ios with UUID.

You should use QLowEnergyController If you use your device as peripheral look this method. I create class that contains instance of QLowEnergyController and works with it. 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. 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.

See this answer for more details: Get MAC address of bluetooth low energy peripheral

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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