×
Namespaces

Variants
Actions

How to send descriptor over Bluetooth

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleArticle
Created: mahbub_s60 (23 Jul 2008)
Last edited: hamishwillee (26 Jul 2012)

Contents

Introduction

This tutorial introduces how to send a descriptor over Bluetooth link and this will also show how a server socket will work as receiver and sender of descriptor message. The example is modified based on Bluetooth chat example found in SDK. Here we just show how to send descriptor or binary data, for file sending you could see obex example: S60_Platform_Bluetooth_OBEX_Example_v1_0_en.zip.html

Two socket for server side and one socket for client side

A BT device can work as client or server at the same time. If it want to work as server (waiting for some other BT device to be connected to it) it should have a listening socket and an accepting socket. When a connection request is occurred then accepting socket is given the handle, it is then used to transfer data. After this listening socket may be used to monitor further connections.

    iAcceptedSocket.Close();    // close old connection - if any
User::LeaveIfError(iAcceptedSocket.Open(iSocketServer)); // Open abstract socket
 
iState = EConnected;
iListeningSocket.Accept(iAcceptedSocket, iStatus);
SetActive();

and in RunL()

        case EConnected:            
iLog.LogL(_L("Connected"));
// do not accept any more connections
iAdvertiser->UpdateAvailabilityL(EFalse);
RequestData();
break;

After establishing the connection, accepted socket goes to receive mode, so it will receive data. RequestData() method is like this.

void CMessageServer::RequestData()
{
iState = EWaitingForMessage;
iAcceptedSocket.RecvOneOrMore(iBuffer, 0, iStatus, iLen);
SetActive();
}

Sending data over BT by client or server

We can send data either by client side or by server side. If client is connected we use client side if not then we use server side.

        case EBTPointToPointSendMessage:
 
if(iClient->IsReadyToSendMessage())
{
iClient->SendMessageL(_L8("Hello Client"));
}
else if(iServer->IsConnected())
{
iServer->SendMessageL(_L8("Hello Server"));
}
break;

SendMessageL() of server side is like this.

void CMessageServer::SendMessageL(const TDesC8& aData)	
{
// cancel any read requests on socket
Cancel();
if(EDisconnected != iState)
{
// send message
iState=ESending;
iAcceptedSocket.Write(aData, iStatus);
SetActive();
}
}

When the data has been sent, we get our RunL() called with current state ESending. So we can go again to receive mode.

    switch (iState)
{
...
case ESending:
RequestData();
break;

Example Applications

The application is tested with E65 & N95 8GB, can be found here: File:BtChatown S60.zip

This page was last modified on 26 July 2012, at 02:04.
48 page views in the last 30 days.
×