×
Namespaces

Variants
Actions

Remote Folder Browsing over Bluetooth

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleArticle
Created: mahbub_s60 (30 Jun 2009)
Last edited: hamishwillee (31 May 2013)

Contents

Introduction

The purpose of this document is to show how we can use Obex protocol to browse folder over Bluetooth link. Due to security reason we can't browse all folders (like any file browser in local devices). We connect the remote device to FTP service with a particular port number. So before using obex, we should discover the remote FTP services and it's port number. We can use normal service discovery protocol for these purposes.

How to Connect

First we need to connect to FTP service.

void CMyBTBrowserObexClient::ConstructL( const TBTDevAddr& aRemoteDevice,
const TUint aRemotePort)
{
TRACE_FUNC_ENTRY
 
// Create Obex Client
//
TObexBluetoothProtocolInfo info;
info.iTransport = KBTSProtocol;
info.iAddr.SetBTAddr( aRemoteDevice );
info.iAddr.SetPort( aRemotePort );
 
TObexProtocolPolicy obexProtocolPolicy;
obexProtocolPolicy.SetReceiveMtu( KMtuSizeReceiv );
obexProtocolPolicy.SetTransmitMtu( KMtuSizeTrans );
 
CObexHeader* header = CObexHeader::NewL();
CleanupStack::PushL( header );
header->SetByteSeqL( KBTSUTargetHeader, KFTPBrowserID );
 
RArray<CObexHeader*> headerList;
CleanupClosePushL( headerList );
headerList.Append( header );
 
iConnectObject = CObexNullObject::NewL();
 
 
if ( headerList.Count() > 0 )
{
for ( TInt index = 0; index < headerList.Count(); index++ )
{
iConnectObject->AddHeaderL( *headerList[index] );
}
}
 
iClient = CObexClient::NewL( info, obexProtocolPolicy );
iClient->SetCallBack( *this );
 
// Create Connect-object
//
iClient->Connect( *iConnectObject, iStatus );
SetActive();
iClientState = EMyBTConnecting;
 
CleanupStack::Pop(2); // headerList, typeHeader
headerList.Close();


When we get connection complete indication in our RunL() then we can instruct obex that we are interested to get folder listing. Folder listing request is done in GetObjectL() method.

void CMyBTBrowserObexClient::RunL()
{
 
TRACE_FUNC_ENTRY
 
switch ( iClientState )
{
case EMyBTSettingPath:
GetObjectL();
break;
 
case EMyBTConnecting:
{
GetObjectL();
iConnectObject->Reset();
break;
}
 
case EMyBTGettingFolderList:
{
iObserver->GetCompleted( iStatus.Int(), iGetObject );
break;
}
 
case EBTSCliDisconnecting:
{
iObserver->ClientConnectionClosed();
break;
}
 
case EBTSCliIdle:
default:
{
 
 
break;
}
}
}



How to request for folder listing

We need to set the path of the folder which one we are going to browse.

void CMyBTBrowserObexClient::SetPath( const TDesC& aPathName )
{
 
if(IsActive())
{
return;
}
 
 
CObex::TSetPathInfo info;
info.iName.Copy(aPathName);
 
info.iNamePresent = ETrue;
info.iFlags |= 2; // Set "Don't Create" flag as default.
 
// Parent
if (info.iName.Length() >= 2 && info.iName[0] == '.' && info.iName[1] == '.')
{
info.iName.Delete(0, 2);
info.iFlags |= 1;
}
 
if (info.iName.Length() <= 0)
{
info.iNamePresent = EFalse;
}
 
// Send object
//
iClient->SetPath( info, iStatus );
SetActive();
iClientState = EMyBTSettingPath;
}


How to request for folder listing

We need to set right header information for CObexBufObject.

void CMyBTBrowserObexClient::GetObjectL( )
{
 
if ( iGetObject )
{
iGetObject->Reset();
delete iGetObject;
iGetObject = NULL;
}
 
iObjectBuffer = CBufFlat::NewL( KBTSUDataBufferExpandSize );
iGetObject = CObexBufObject::NewL( iObjectBuffer );
RArray<CObexHeader*> headerList;
CleanupClosePushL(headerList);
CObexHeader* typeHeader = CObexHeader::NewL();
CleanupStack::PushL( typeHeader );
typeHeader->SetByteSeqL( KBTSUTypeHeader, KMyFolderListing );
headerList.Append( typeHeader );
 
// Set headers
//
if ( headerList.Count() > 0 )
{
for ( TInt index = 0; index < headerList.Count(); index++ )
{
iGetObject->AddHeaderL( *headerList[index] );
}
}
 
// Send get request
//
iClient->Get( *iGetObject, iStatus );
SetActive();
iClientState = EMyBTGettingFolderList;
CleanupStack::Pop(2); // headerList, typeHeader
headerList.Close();
 
}


How to handle folder listing

Remote device sends a XML file contains the list of folders and files in from the target folder of the remote device. We should handle the xml file and display the contents to user. Also we can implement left arrow, right arrow to browse folder to backward and forward direction. The contents of the XML file can be like this according to Obex specification.

<folder-listing>
<folder name = ”System” created = ”19961103T141500Z”/>
<file name = ”Jumar.txt” created = ”19971209T090300Z” size = ”6672”/>
<file name = “Obex.doc” created = ”19970122T102300Z” size = ”41042”/>
</folder-listing>

/*
* Do analysis of the xml file
* @parma aStatus status of the operation
* @parma aGetResponse CObexBufObject that contains the folder listing
*/

void CMyBTBrowserFolderContainer::GetCompleted( TInt aStatus, CObexBufObject* aGetResponse )
{
if(aStatus != KErrNone)
{
return;
}
 
_LIT(KAllItemFileName, "templist.dat");
TFileName tempfile;
RFs& fsSession = CCoeEnv::Static()->FsSession();
User::LeaveIfError(fsSession.CreatePrivatePath( EDriveC ) );
User::LeaveIfError(fsSession.PrivatePath(tempfile));
tempfile += KAllItemFileName;
// Create a temp XML folder listing file
aGetResponse->WriteToFile( tempfile );
aGetResponse->Reset();
 
CBTSUXmlParser* xmlParser = CBTSUXmlParser::NewL();
CleanupStack::PushL( xmlParser );
ClearFilesList(); // Clear old file list
iRemoteFolderList = xmlParser->GetFolderAndFileListL( tempfile );
fsSession.Delete( tempfile );
 
for(TInt i = 0; i <iMessageList->Count(); i++ )
{
MDesCArray* textArray = iFolderListBox->Model()->ItemTextArray();
CDesCArray* itemList = static_cast<CDesCArray*>(textArray);
itemList->Delete( i ); //remove the item
iFolderListBox->HandleItemRemovalL();
}
 
TInt capindex = 0;
iMessageList->Reset();
_LIT(KFormat, "%d\t%S\t\t");
TBuf<100> buf(0);
for (capindex=0; capindex < iRemoteFolderList->Count(); capindex++)
{
buf.Zero();
buf.Format(KFormat, (*iRemoteFolderList)[capindex].iFolderorFile, (*iRemoteFolderList)[capindex].iFolder );
iMessageList->AppendL(buf);
iFolderListBox->HandleItemAdditionL();
}
iFolderListBox->DrawNow();
CleanupStack::PopAndDestroy( 1 ); // xmlParser
}


Example Applications


Here we can find some implementation: File:MyBTBrowserObexClient.zip

This page was last modified on 31 May 2013, at 02:27.
61 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×