Java Reference
In-Depth Information
Discovering Devices
The Bluetooth API has the concept of a discovery agent. You can think of it as a helper process
that assists you in the discovery of other devices or services in a PAN.
It is called an agent because it works independently. Once you tell it what to do, it goes
about its task merrily on your behalf. Sometime later, you can come back to the agent and
request the status. Or you can tell the agent to call you back when it makes a new discovery.
To get an instance of a DiscoveryAgent , you need to go through the Bluetooth manager
(the LocalDevice class). Basically, you need to make a call similar to the following:
DiscoveryAgent myDa = LocalDevice.getInstance().getDiscoveryAgent();
You can perform discovery on a device level or a service level. Device-level discovery
allows for more control over the discovery process, but requires significantly more complex
coding. When you perform discovery on a service level, the DiscoveryAgent handles the device-
level manipulations for you.
Starting and Stopping Device Discovery
When you have a DiscoveryAgent instance, you can tell it explicitly to start discovery for devices
via this method:
boolean startInquiry(int accessCode, DiscoveryListener listener) ;
The access code determines the type of inquiry and can be either DiscoveryAgent.GIAC
(General Inquiry Access Code) or DiscoveryAgent.LIAC (Limited Inquiry Access Code).
These codes are specified by the Bluetooth “Assigned Numbers” document (see http://
www.bluetooth.org/assigned-numbers/ ). Almost all Bluetooth devices you encounter can be
discovered using GIAC .
startInquiry() will start device discovery. For each device that is discovered, the listener
that you register will receive a notification. This listener must implement the DiscoveryListener
interface. The interface has four methods, but only two are important for device discovery:
void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) ;
void inquiryCompleted(int discType) ;
The deviceDiscovered() notification method is called when a device is discovered during
the inquiry. The inquiryCompleted() method is called when the inquiry process is completed.
This can be due to a timeout period (determined by the Bluetooth implementation) or when
the DiscoveryAgent is being told to stop the discovery.
The inquiryCompleted() notification method is called when the discovery process completes.
A discType value of DiscoveryListener.INQUIRY_COMPLETED indicates a normal completion,
while a DiscoveryListener.INQUIRY_TERMINATED indicates a manual termination. Error-caused
termination will have a discType value of DiscoveryListener.INQUIRY_ERROR .
You can tell the DiscoveryAgent to stop the discovery, before its natural termination, by
calling its cancelInquiry() method:
public boolean cancelInquiry(DiscoveryListener listener);
Note that you must supply the same listener as the one that you've called startInquiry()
with; otherwise, the discovery will not be stopped, and the method returns false.
Search WWH ::




Custom Search