Search code examples
androidiosqtbluetoothuuid

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.


Solution

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