Adding a device to the PAN

In the last tutorial we looked at the steps involved in creating a Personal Area Network. Now we will look at adding devices to that PAN.

Using the Tutorial

The code fragments in this tutorial are derived from the Bluetooth Example application available in /src/COMMON/DEVELOPERLIBRARY/examples/Bluetooth/BTExample1. Although the code has been reformatted to the requirements of the tutorial there is always enough information included with the code to find the actual example code.

Intended Audience:

This tutorial is designed for Symbian licensees only. Several APIs mentioned ad used throughout this tutorial series are only available in a device creators kit environment.

Basic procedure:

The high level steps to add a device to a PAN are:

  • Connect to the listening device.

    (See previous tutorial)

  • Check if there are any open slots on the PAN.

  • Add the device to an open slot.

    This will start the new device to the PAN connection created in the previous tutorial.

Connecting to a PAN Host

The user will launch the application (BTExample1) and be presented with a list of options. One of the options is to invite another user to join the PAN. This will cause an RNotifier::StartNotifierAndGetResponse() to prompt the user to select a device. The result of this operation is that the Bluetooth device address is known. See "Finding the Bluetooth Device Address " for more.

Checking for open slots

The PAN host will need to check to see if there are any open connections. You may recall from the PAN Overview that there are currently two possible roles a member of a PAN may take, PANU or PAN-GN. If the PAN will only require two members the role of both can be PANU, the host will simply check that there are not any current connections before going on to open a connection for the new member. If however the PAN supports two or more connections the host will be operating in the PAN-GN role, which allows up to seven connections.

Check that there is a slot available.

if(iActiveConnections.Count() == 7)
{
 err = KErrOverflow;
}

If all the slots are currently used return KErrOverflow and end the connection attempt. If there are open slots add the device to one as explained in the next section.

Adding your device to the PAN

To recap, we have a configured PAN and it has at least one open slot.

Add the device address to one of the open connections of the PAN.

else if(aDevAddr)
{
 // Add the BT address of the device to the existing RConnection
 iControlAddr = *aDevAddr;
 TPtr8 ptr = iControlAddr.Des();
 err = iConnection.Control(KCOLAgent, KCOAgentPanConnectDevice, ptr);
 ...
}
return err;

aDevAddr will contain the Bluetooth device address if the notifier was used as discussed above. The device address is packaged into the ptr pointer, which is passed into the RConnection::Control(). The control function adds the device address to the PAN.

The device is now a member of the PAN.

What's next?

Having created a PAN and added a device you will need to remove a device and close the PAN. The following will help: