×
Namespaces

Variants
Actions

How to configure Bluetooth L2CAP connection

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

Description

L2CAP takes data from application or higher protocol layers and sends it over the lower layers. L2CAP passes packets either to HCI or in a host-less system, directly to LM. L2CAP utilizes ACL connections. L2CAP transfers data, not audio (voice or IP regard as data). When L2CAP connection is established between two devices then they negotiates about the inbound and outbound transmission unit (TU) and then they know also the maximum transmission unit (MTU). Mostly application developers does not need to care about this since these negotiation is done automatically. But if the developers need to control or affect on the MTU then he/she can do it in the following way. In this article, we shall show how we can configure the link with specified transmit unit and ask the transmit unit from the connection by using socket and L2CAP protocol.


Solution

L2CAP transmits unit is configured before connection. Once the connection has been established then the transmit unit will be used. Inbound transmission unit of local device is the same as out bound transmission unit of remote devices and outbound transmission unit is same as inbound transmission unit of remote device.

Code for configuring the L2CAP connection.

    iLog.LogL(_L("Connecting to service"));
User::LeaveIfError(iSendingSocket.Open(iSocketServer, _L("L2CAP")));
TBTSockAddr address;
address.SetBTAddr(iServiceSearcher->BTDevAddr());
address.SetPort(iServiceSearcher->Port());
TBuf<100> buf(0);
TL2CapConfigPkg ConfigPkg = iL2CapAPIConfig;
ConfigPkg().SetMaxTransmitUnitSize(iMaxTransmitUnit); // has been set or use some default value specified in constructor
ConfigPkg().SetMaxReceiveUnitSize(iMaxReceivedUnit); // has been set or use some default value specified in constructor
TInt rerr = iSendingSocket.SetOpt(KL2CAPUpdateChannelConfig, KSolBtL2CAP, ConfigPkg);
buf.Format(_L("SetOpt ret= %d"), rerr);
iSendingSocket.Connect(address, iStatus);
 
#ifdef __WINS__
User::After(1); // Fix to allow emulator client to connect to server
#endif
 
SetActive();


Code for getting the configuration value from L2CAP link.
This will return us the inbound and outbound transmission unit as well as inbound and outbound MTU.

    TInt rerr = KErrNone;
TPckgBuf<TInt> getOptBuf;
TInt getOptValue = 0;
TBuf<100> buf(0);
 
 
 
 
if(rerr == KErrNone)
{
rerr = iSendingSocket.GetOpt(KL2CAPGetOutboundMTU, KSolBtL2CAP, getOptBuf);
if(rerr == KErrNone)
{
getOptValue = getOptBuf();
buf.Format(_L("Out MTU %d"), getOptValue);
iLog.LogL(buf);
}
}
 
 
if(rerr == KErrNone)
{
rerr = iSendingSocket.GetOpt(KL2CAPInboundMTU, KSolBtL2CAP, getOptBuf);
if(rerr == KErrNone)
{
getOptValue = getOptBuf();
buf.Format(_L("In MTU %d"), getOptValue);
iLog.LogL(buf);
}
}
 
if(rerr == KErrNone)
{
rerr = iSendingSocket.GetOpt(KL2CAPGetMaxOutboundMTU, KSolBtL2CAP, getOptBuf);
if(rerr == KErrNone)
{
getOptValue = getOptBuf();
buf.Format(_L("Max Out MTU %d"), getOptValue);
iLog.LogL(buf);
}
}
 
if(rerr == KErrNone)
{
rerr = iSendingSocket.GetOpt(KL2CAPGetMaxInboundMTU, KSolBtL2CAP, getOptBuf);
if(rerr == KErrNone)
{
getOptValue = getOptBuf();
buf.Format(_L("Max In MTU %d"), getOptValue);
iLog.LogL(buf);
}
}

Required Capability and Example

LocalServices capability is necessary to use this software.. File:L2cap Config.zip

This page was last modified on 31 May 2013, at 02:49.
55 page views in the last 30 days.
×