void CMyService::ConstructL() { User::LeaveIfError(iSocketServ.Connect()); iSCOConnection = new(ELeave) CMySCOService(iSocketServ); iSCOConnection->ConstructL(); User::LeaveIfError(iSdpSession.Connect()); TBTServiceSecurity sec; sec.SetAuthentication(EMitmNotRequired); sec.SetAuthorisation(EFalse); sec.SetEncryption(EFalse); sec.SetDenied(EFalse); iBtService = CBtService::NewL(TUUID(KSerialPortUUID), //register as a serial port so we can be easily seen iSdpSession, iSocketServ, *this, KRFCOMM, &sec); iConnection = CBluetoothSocket::NewL(*this, iSocketServ); iBtService->AcceptConnection(*iConnection); }
bool QRfcommServer::listen(const QBluetoothAddress &address, quint16 port) { Q_D(QRfcommServer); // listen has already been called before if(d->socket) return true; d->socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket,this); if(!d->socket) { return false; } d->ds = d->socket->d_ptr; if(!d->ds) { delete d->socket; d->socket = 0; return false; } d->ds->ensureNativeSocket(QBluetoothSocket::RfcommSocket); TRfcommSockAddr addr; if(!address.isNull()) { // TBTDevAddr constructor may panic TRAPD(err,addr.SetBTAddr(TBTDevAddr(address.toUInt64()))); if(err != KErrNone) { delete d->socket; d->socket = 0; return false; } } if (port == 0) addr.SetPort(KRfcommPassiveAutoBind); else addr.SetPort(port); TBTServiceSecurity security; switch (d->securityFlags) { case QBluetooth::Authentication: security.SetAuthentication(EMitmDesired); break; case QBluetooth::Authorization: security.SetAuthorisation(ETrue); break; case QBluetooth::Encryption: // "Secure" is BlueZ specific we just make sure the code remain compatible case QBluetooth::Secure: // authentication required security.SetAuthentication(EMitmDesired); security.SetEncryption(ETrue); break; case QBluetooth::NoSecurity: default: break; } if(d->ds->iSocket->Bind(addr) == KErrNone) { d->socket->setSocketState(QBluetoothSocket::BoundState); } else { delete d->socket; d->socket = 0; return false; } if(d->ds->iSocket->Listen(d->maxPendingConnections) != KErrNone) { delete d->socket; d->socket = 0; return false; } QBluetoothSocket *pendingSocket = new QBluetoothSocket(QBluetoothSocket::UnknownSocketType, this); if(!pendingSocket) { delete d->socket; d->socket = 0; return false; } QBluetoothSocketPrivate *pd = pendingSocket->d_ptr; pd->ensureBlankNativeSocket(QBluetoothSocket::RfcommSocket); connect(d->socket, SIGNAL(disconnected()), this, SLOT(_q_disconnected())); connect(d->socket, SIGNAL(connected()), this, SLOT(_q_connected())); connect(d->socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(_q_socketError(QBluetoothSocket::SocketError))); if (d->ds->iSocket->Accept(*pd->iSocket) == KErrNone) { d->socket->setSocketState(QBluetoothSocket::ListeningState); d->activeSockets.append(pendingSocket); return true; } else { delete d->socket, pendingSocket; d->socket = 0; return false; } }
// ---------------------------------------------------------------------------- // CChatBt::SetSecurityWithChannelL( ) // Sets the security on the channel port and returns the available port. // ---------------------------------------------------------------------------- // void CChatBt::SetSecurityWithChannelL( TBool aAuthentication, TBool aEncryption, TBool aAuthorisation, TBool aDenied, TInt& aChannel ) { // Local variable to channel to listen to. TInt channel; User::LeaveIfError( iSocket.GetOpt( KRFCOMMGetAvailableServerChannel, KSolBtRFCOMM, channel ) ); TBTSockAddr listeningAddress; // Set the Port to listen to. listeningAddress.SetPort( channel ); // Write Log events HBufC* strGetPort = StringLoader::LoadLC ( R_CHAT_STR_GET_PORT ); iLog.LogL( *strGetPort, channel ); CleanupStack::PopAndDestroy( strGetPort ); User::LeaveIfError( iSocket.Bind( listeningAddress ) ); User::LeaveIfError( iSocket.Listen( KListeningQueSize ) ); // close old connection - if any iAcceptedSocket.Close(); // Open abstract socket User::LeaveIfError( iAcceptedSocket.Open( iSocketServer ) ); // Set the Active Object's State to Connecting indicated. SetState( EConnecting ); iSocket.Accept( iAcceptedSocket, iStatus ); iActiveSocket = &iAcceptedSocket; // Set the Active Object Active again, SetActive(); // Write Log events HBufC* acceptNextConn = StringLoader::LoadLC ( R_CHAT_ACCEPT_NEXT_CONN ); iLog.LogL( *acceptNextConn ); CleanupStack::PopAndDestroy( acceptNextConn ); // Set the security according to. TBTServiceSecurity serviceSecurity; serviceSecurity.SetUid ( KUidChatApp ); serviceSecurity.SetAuthentication ( aAuthentication ); serviceSecurity.SetEncryption ( aEncryption ); serviceSecurity.SetAuthorisation ( aAuthorisation ); serviceSecurity.SetDenied( aDenied ); // Attach the security settings. listeningAddress.SetSecurity(serviceSecurity); // Return the port to listen to. aChannel = channel; }