The Service Discovery Agent is used to perform queries about the Bluetooth services that are available on a specified remote device. It is typically used after the suitable devices that are in range have been identified through the Bluetooth Sockets API.
A service search returns the record handles of services that are of a specified class or classes (identified by UUID numbers). If the search is for more than one UUID, then all the UUIDs must exist in a service record for it to be considered a match. See "Service Discovery Protocol Assigned Numbers " for the common UUIDs.
Service search results are returned through asynchronous callbacks to an MSdpAgentNotifier
interface, which the querier must implement, as discussed later.
Basic Procedure
The steps to perform a service search are as follows:
Create a CSdpAgent
object, supplying it with an MSdpAgentNotifier
and the device address of the remote Bluetooth device.
Create a CSdpSearchPattern
object to specify the service classes to search for. Classes can be added through CSdpSearchPattern::AddL()
.
Set the search pattern on the agent object using CSdpAgent::SetRecordFilterL()
.
Call CSdpAgent::NextRecordRequestL()
to get search results until results are exhausted, or sufficient results have been obtained.
Querying for a service
The following code fragements may be used, in an appropriate context, to query a remote device's SDP database for Obex file transfer support (for example). The steps are given here:
Create agent. (Assume rcvr implements MSdpAgentNotifier
and devAddr
is the address of the remote device.)
CSdpAgent* agent = CSdpAgent::NewLC(rcvr, devAddr);
Create a search pattern and add a service classes to it.
CSdpSearchPattern* list = CSdpSearchPattern::NewL(); list->AddL(0x1106);
The UUID 0x1106 represents the OBEXFileTransfer service class.
Set the search pattern on the agent.
agent->SetRecordFilterL(*list);
Get first search result: results in call back to rcvr
.
agent->NextRecordRequestL();
A positive match indicates a device has been found that supports OBEX file transfer. The above code may serve as a template to search for any service class for which you have the UUID value.
The complete set of Service Discovery Agent tutorials are shown below:
Also refer to the Bluetooth Service Discovery Agent Overview and the Bluetooth SDP Overview for additional background information.