Java Reference
In-Depth Information
The following code snippet shows how to start a search for nearby
devices using JSR-82 classes:
// this throws a BluetoothStateException if Bluetooth is not enabled,
// which is a good way to check that it is before proceeding
LocalDevice localDevice = LocalDevice.getLocalDevice();
// start an inquiry for other devices in GIAC mode
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, listener);
Notice the listener variable passed on the last line above. This
is a reference to an object that implements the DiscoveryListener
interface which defines a handler interface for a series of callbacks that
occur during the inquiry and discovery phases. To stop an inquiry in
progress, you can also pass this handler to the cancelInquiry()
method of the DiscoveryAgent class. For reference, this interface is
shown below:
public interface DiscoveryListener
...
// called once for each device found during the inquiry phase
void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod);
void inquiryCompleted(int discType);
// called during service discovery
void servicesDiscovered(int transID, ServiceRecord[] servRecord);
void serviceSearchCompleted(int transID, int respCode);
Once a set of devices matching the desired CoD has been located,
they need to be queried in order to find out what services (identified using
UUIDs) they offer. In the case of a Bluetooth game, we are interested
in the game 'service' that we have built. Each device is queried in turn
and the servicesDiscovered() callback method is called on the
supplied listener, passing an array of ServiceRecord objects. These
represent the various attributes of services found on the remote device.
You can specify what attributes (such as, service name) that you want
returned in this process as part of the discovery setup:
// an array of service ids we want to find
UUID[] uuid = new UUID[1];
RemoteDevice remoteDevice;
...
// include service name attribute in returned service records
int attribs[] = new int[] { 0x0100 } ;
...
discoveryAgent.searchServices(attribs, uuid, remoteDevice, listener);
Search WWH ::




Custom Search