QDeclarativeBluetoothDiscoveryModel::QDeclarativeBluetoothDiscoveryModel(QObject *parent) :
    QAbstractListModel(parent),
    d(new QDeclarativeBluetoothDiscoveryModelPrivate)
{

    QHash<int, QByteArray> roleNames;
    roleNames = QAbstractItemModel::roleNames();
    roleNames.insert(Qt::DisplayRole, "name");
    roleNames.insert(Qt::DecorationRole, "icon");
    roleNames.insert(ServiceRole, "service");
    setRoleNames(roleNames);

    d->m_agent = new QBluetoothServiceDiscoveryAgent(this);
    connect(d->m_agent, SIGNAL(serviceDiscovered(const QBluetoothServiceInfo&)), this, SLOT(serviceDiscovered(const QBluetoothServiceInfo&)));
    connect(d->m_agent, SIGNAL(finished()), this, SLOT(finishedDiscovery()));
    connect(d->m_agent, SIGNAL(canceled()), this, SLOT(finishedDiscovery()));
    connect(d->m_agent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(errorDiscovery(QBluetoothServiceDiscoveryAgent::Error)));

}
void QDeclarativeBluetoothDiscoveryModel::setRunning(bool running)
{
    if (!d->m_componentCompleted) {
        d->m_runningRequested = running;
        return;
    }

    if (d->m_running == running)
        return;

    d->m_running = running;

    if (!running) {
        if (d->m_deviceAgent)
            d->m_deviceAgent->stop();
        if (d->m_serviceAgent)
            d->m_serviceAgent->stop();
    } else {
        clearModel();
        d->m_error = NoError;
        if (d->m_discoveryMode == DeviceDiscovery) {
            if (!d->m_deviceAgent) {
                d->m_deviceAgent = new QBluetoothDeviceDiscoveryAgent(this);
                connect(d->m_deviceAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
                connect(d->m_deviceAgent, SIGNAL(finished()), this, SLOT(finishedDiscovery()));
                connect(d->m_deviceAgent, SIGNAL(canceled()), this, SLOT(finishedDiscovery()));
                connect(d->m_deviceAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(errorDeviceDiscovery(QBluetoothDeviceDiscoveryAgent::Error)));
            }
            d->m_deviceAgent->start();
        } else {
            if (!d->m_serviceAgent) {
                d->m_serviceAgent = new QBluetoothServiceDiscoveryAgent(this);
                connect(d->m_serviceAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
                connect(d->m_serviceAgent, SIGNAL(finished()), this, SLOT(finishedDiscovery()));
                connect(d->m_serviceAgent, SIGNAL(canceled()), this, SLOT(finishedDiscovery()));
                connect(d->m_serviceAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(errorDiscovery(QBluetoothServiceDiscoveryAgent::Error)));
            }

            d->m_serviceAgent->setRemoteAddress(QBluetoothAddress(d->m_remoteAddress));
            d->m_serviceAgent->clear();

            if (!d->m_uuid.isEmpty())
                d->m_serviceAgent->setUuidFilter(QBluetoothUuid(d->m_uuid));

            if (discoveryMode() == FullServiceDiscovery)  {
                //qDebug() << "Full Discovery";
                d->m_serviceAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
            } else {
                //qDebug() << "Minimal Discovery";
                d->m_serviceAgent->start(QBluetoothServiceDiscoveryAgent::MinimalDiscovery);
            }

            // we could not start service discovery
            if (!d->m_serviceAgent->isActive()) {
                d->m_running = false;
                errorDiscovery(d->m_serviceAgent->error());
                return;
            }
        }
    }

    emit runningChanged();
}