void QDeclarativeBluetoothService::setRegistered(bool registered)
{
    if (!d->m_componentComplete) {
        return;
    }

    delete d->m_server;
    d->m_server = 0;

    if (!registered) {
        d->m_service->unregisterService();
        emit registeredChanged();
        return;
    }

    d->listen();
    connect(d->m_server, SIGNAL(newConnection()), this, SLOT(new_connection()));


    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010);

    QBluetoothServiceInfo::Sequence classId;
    classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);

    //qDebug() << "name/uuid" << d->m_name << d->m_uuid << d->m_port;

    d->m_service->setAttribute(QBluetoothServiceInfo::BrowseGroupList,
                             QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));

    QBluetoothServiceInfo::Sequence protocolDescriptorList;
    QBluetoothServiceInfo::Sequence protocol;

    if (d->m_protocol == L2CapProtocol) {
        protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap))
                 << QVariant::fromValue(quint16(d->m_server->serverPort()));
        protocolDescriptorList.append(QVariant::fromValue(protocol));
    }
    else if (d->m_protocol == RfcommProtocol) {
        protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
                 << QVariant::fromValue(quint8(d->m_server->serverPort()));
        protocolDescriptorList.append(QVariant::fromValue(protocol));
    }
    else {
        qWarning() << "No protocol specified for bluetooth service";
    }
    d->m_service->setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                             protocolDescriptorList);

    if (d->m_service->registerService()) {
        emit registeredChanged();
    }
    else {
        qWarning() << "Register service failed";
        //TODO propaget this error to the user
    }
}
void RegistrationHandler::registrationFailed()
{
  // A generic error message is provided here.
  // You could provide a different error for each failure code to instruct the
  // user on how to continue.
  setStatusText("BBM registration failed. Registration is required connect with BlackBerry Messenger. Please restart the application to try again.");

  mRegistered = false;
  emit registeredChanged(mRegistered);
}
void QDeclarativeBluetoothService::setRegistered(bool registered)
{

    d->m_needsRegistration = registered;

    if(!d->m_componentComplete){
        return;
    }

    if(!registered) {
        if(!d->m_service)
            return;
        d->m_service->unregisterService();
        emit registeredChanged();
    }

    if(!d->m_service){
        d->m_service = new QBluetoothServiceInfo();
    }


    delete d->m_listen;
    d->m_listen = 0;

    d->listen();
    connect(d->m_listen, SIGNAL(newConnection()), this, SLOT(new_connection()));


    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceRecordHandle, (uint)0x00010010);

//    QBluetoothServiceInfo::Sequence classId;
////    classId << QVariant::fromVhttp://theunderstatement.com/alue(QBluetoothUuid(serviceUuid));
//    classId << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::SerialPort));
//    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceClassIds, classId);

    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceName, d->m_name);
    d->m_service->setAttribute(QBluetoothServiceInfo::ServiceDescription,
                             d->m_description);

    d->m_service->setServiceUuid(QBluetoothUuid(d->m_uuid));

    qDebug() << "name/uuid" << d->m_name << d->m_uuid << d->m_port;

    d->m_service->setAttribute(QBluetoothServiceInfo::BrowseGroupList,
                             QBluetoothUuid(QBluetoothUuid::PublicBrowseGroup));

    QBluetoothServiceInfo::Sequence protocolDescriptorList;
    QBluetoothServiceInfo::Sequence protocol;

    qDebug() << "Port" << d->m_port;

    if(d->m_protocol == "l2cap"){
        protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap))
                 << QVariant::fromValue(quint16(d->m_port));
        protocolDescriptorList.append(QVariant::fromValue(protocol));
    }
    else if(d->m_protocol == "rfcomm"){
        protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::Rfcomm))
                 << QVariant::fromValue(quint8(d->m_port));
        protocolDescriptorList.append(QVariant::fromValue(protocol));
    }
    else {
        qWarning() << "No protocol specified for bluetooth service";
    }
    d->m_service->setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
                             protocolDescriptorList);

    if(d->m_service->registerService()) {
        qDebug() << "registered";
        emit registeredChanged();
    }
    else {
        qDebug() << "Failed";
    }
}
void RegistrationHandler::registrationFinished()
{
  // Finish registration and use the state to decide which message to show
  // the user.
  mProgress = BbmRegistrationProgress::Finished;
  switch (mContext->registrationState())
  {
  case RegistrationState::Allowed:
    qDebug() << "Application connected to BBM. Press Continue.";
    break;

  case RegistrationState::BlockedByRIM:
    qDebug() << "application from connecting to BBM.";

    break;

  case RegistrationState::BlockedByUser:
    qDebug()
        << "Disconnected. Go to Settings -> Security and  Privacy -> Application Permissions and  connect this application to BBM.";

    break;

  case RegistrationState::InvalidUuid:
    // You should be resolving this error at development time.
    qDebug() << "Invalid UUID. Report this error to the vendor.";
    break;

  case RegistrationState::MaxAppsReached:
    qDebug()  << "Too many applications are connected to BBM.Uninstall one or more applications and try again.";
    break;

  case RegistrationState::Expired:
  case RegistrationState::MaxDownloadsReached:
    qDebug()  << "Cannot connect to BBM. Download this application from AppWorld to keep using it.";
    break;

  case RegistrationState::NoDataConnection:
    qDebug() << "Check your Internet connection and try again.";

    break;

  case RegistrationState::Pending:
    // The user will never see this. The BBM Social Platform already
    // displays a "Connecting" dialog.
    qDebug() << "Connecting to BBM. Please wait.";
    break;

  case RegistrationState::Unknown:
    qDebug() << "Determining the status. Please wait.";
    break;

  case RegistrationState::Unregistered:
  case RegistrationState::UnexpectedError:
  case RegistrationState::TemporaryError:
  case RegistrationState::CancelledByUser:
  default:

    break;
  }

  if (mContext->isAccessAllowed())
  {
    mRegistered = true;
  }
  else
  {
    mRegistered = false;
  }

  qDebug() << "Finished BBM Social Platform registration=" << mRegistered;

  emit registeredChanged(mRegistered);
}