Java Reference
In-Depth Information
void servicesDiscovered(int transID, ServiceRecord[] servRecord) ;
void serviceSearchCompleted(int transID, int respCode) ;
servicesDiscovered() is called when services are found during a search. The transID gives
the transaction ID, while the servRecord array contains an entry for each matching service.
serviceSearchCompleted() is called when the search is ending, either because of successful
completion, forced termination, or error. You can terminate a search by calling the DiscoveryAgent
method:
public boolean cancelServiceSearch(int transID);
A Simpler Way to Discover Services
Combining device discovery with service search, the pseudo-code to discover a Bluetooth
service, given its UUID, may be as follows:
obtain the DiscoveryAgent
tell DiscoveryAgent to startInquiry()
save every discovered RemoteDevice discovered in a Vector
wait until device discovery finishes or terminate inquiry with cancelInquiry()
for each RemoteDevice discovered {
tell DiscoveryAgent to searchServices() specifying the UUID seeked
save every service record returned
}
The preceding pseudo-code translates to a lot of Java code. Because the discovery process
is centered around asynchronous listener callbacks, the code will also necessarily involve
multiple concurrent threads and tricky synchronization.
Unfortunately, no other way exists if you require fine control over service selection between
devices. If you don't really care which device a service comes from, however, there is a simpler
way to perform service discovery. The selectService() method on DiscoveryAgent can be used
to find a service when you don't care which device it may come from:
String selectService(UUID uuid, int security, boolean master);
The returned value, if not null, is a connection string that you can use with GCF's
Connector.open() to start talking to the service. security indicates whether authentication
and encryption should be used; security can be one of the following:
ServiceRecord.NOAUTHENTICATE_NOENCRYPT
ServiceRecord.AUTHENTICATE_NOENCRYPT
ServiceRecord.AUTHENTICATE_ENCRYPT
Bluetooth authentication, if implemented, is based on a PIN-generated challenge/response
sequence. Encryption also requires this PIN; therefore it is not possible to perform encryption
without first authenticating. Note that authentication and encryption may not be implemented by
all Bluetooth implementations. The master flag indicates whether the client must be a master
in the underlying Bluetooth connection.
Search WWH ::




Custom Search