lunedì 29 novembre 1999

Discover Bluetooth devices from a J2ME application

Using the optional package JSR-82 (where is available of course) you can implement clients using the Bluetooth feature of your device.

Let's start discovering the devices around our phone. First of all you have to get a DiscoverAgent object from the object LocalDevice that represent your device:

LocalDevice m_locDevice = LocalDevice.getLocalDevice();
DiscoveryAgent m_discAgent = m_locDevice.getDiscoveryAgent();


After that you can call the startInquiry method:

try {
   m_discAgent.startInquiry( DiscoveryAgent.LIAC | DiscoveryAgent.GIAC, this );
}
catch ( BluetoothStateException e ) {
  e.printStackTrace();
}


in the previous piece of code, the startInquiry method has been called with two arguments:

- the first says we want to discover both devices temporarily discoverable (LIAC) and permanently discoverable (GIAC)
- the second argument is a DiscoveryListener object that will receive notification each time a device has been discovered and when the discover action has been completed.

The method called in the DiscoveryListener each time a device is discovered is

public void deviceDiscovered ( RemoteDevice remoteDevice, DeviceClass deviceClass )

The remoteDevice object contains information about the discovered device like the bluetooth address and the friendly name you can use to show it to the user.

The deviceClass object contains information about the type of device has been discovered.

The method called when the discover action is finished is (always in the class implementing the DiscoveryListener):

public void inquiryCompleted ( int i )

The argument i will be an integer saying if the action has been completed or if it has been interrupted (it's possible stop the action calling cancelInquiry( this ) in the m_discAgent instance).

Nessun commento:

Posta un commento