Each Bluetooth device has a 48-bit unique address built into its hardware. A basic inquiry for devices in range returns zero or more of these addresses.
As well as an address, a Bluetooth device has a text name suitable for display to users. If you want to display a list of available devices to the user, you will also need to obtain these names.
The address and the name inquiries can occur simultaneously, if the underlying hardware supports this. Otherwise, the address inquiry must finish before the name request can be issued over the air.
Address and name inquiries are performed through the generic EPOC sockets class RHostResolver
. A specialist Bluetooth sockets address class, TInquirySockAddr
, which encapsulates Bluetooth address, Inquiry Access Code, and service and device classes, is provided for use with such inquiries.
To inquire for the addresses of remote devices, take the following steps:
Connect to the Sockets Server (RSocketServ
), and then select the protocol to be used using RSocketServ::FindProtocol()
. Address and name queries are supplied by the stack's BTLinkManager protocol layer, so select this.
Create and initialise an RHostResolver
object.
Set the TInquirySockAddr
parameter for the inquiry: for address inquiries, the KHostResInquiry
flag must be set through TInquirySockAddr::SetAction()
.
The query can then be started with RHostResolver::GetByAddress()
.
When GetByAddress()
completes, it fills in a TNameEntry
object with the address and class of the first device found (or is undefined if no device was found).
To get all the devices discovered, call RHostResolver::Next()
repeatedly until KErrHostResNoMoreResults
is returned.
The following example shows how to start a remote device address inquiry.
// 1. Connect to the socket server
RSocketServ socketServ;
socketServ.Connect();
TProtocolDesc pInfo;
_LIT(KL2Cap, "BTLinkManager");
User::LeaveIfError(socketServ.FindProtocol(KL2Cap,pInfo));
// 2. Create and initialise an RHostResolver
RHostResolver hr;
User::LeaveIfError(hr.Open(socketServ,pInfo.iAddrFamily,pInfo.iProtocol));
// 3. Set up a discovery query and start it
TInquirySockAddr addr;
TNameEntry name;
addr.SetIAC(KGIAC);
addr.SetAction(KHostResInquiry);
TRequestStatus status;
hr.GetByAddress(addr, entry, status);
User::WaitForRequest(status);
// 4. Process the information returned in entry
...
TInquirySockAddr::SetIAC()
sets the Bluetooth Inquiry Access Code. For more information, see [BS1 Bluetooth Assigned Numbers 1.1].
The host resolver caches the results of inquiries so that devices that are no longer present may appear in the list of results. This does not cause any additional complications, as it is always possible for a device to go out of range between when it is discovered and when a connection to it is made.
Communications API calls are typically asynchronous (indicated by a TRequestStatus
parameter in the call). It is recommended that such calls are encapsulated in active objects, as explained in Using Asynchronous Programming.
The name of a remote device can be queried for by taking the same steps as for an address query, but setting the action flag of a TInquirySockAddr
to KHostResName
. The name is returned in the iName
member accessed through the TNameEntry
.
// Now do name inquiry
addr.SetAction(KHostResName);
hr.GetByAddress(addr, entry, stat);
User::WaitForRequest(stat);
TPtrC deviceName;
if (stat == KErrNone)
deviceName.Set(entry().iName);
To do a simultaneous address and name inquiry, use SetAction(KHostResName|KHostResInquiry)
.
RHostResolver::GetByName()
is not supported