FileDocCategorySizeDatePackage
DiscoveryAgentImpl.javaAPI DocphoneME MR2 API (J2ME)11073Wed May 02 18:00:32 BST 2007com.sun.kvem.jsr082.bluetooth

DiscoveryAgentImpl

public final class DiscoveryAgentImpl extends Object
The DiscoveryAgentImpl class is a DiscoveryAgent API class implementation which does not extend this API class.

Fields Summary
private static final boolean
DEBUG
private static DiscoveryAgentImpl
instance
Keeps an instance to the object of this class to be accessible from the implementation.
private Hashtable
knownDevices
Keeps the RemoteDeviceImpl references of known devices.
private Hashtable
cachedDevices
Keeps the RemoteDeviceImpl references of cached devices.
private javax.bluetooth.DiscoveryListener
d_listener
Keeps the listener of the device discovery inquire. Also, it is used as flag that a device is in inquire mode.
private Object
d_lock
Keeps the lock object for device discovery synchronization.
private SelectServiceHandler
selectServiceHandler
Keeps the reference to module responsible for selecting services.
Constructors Summary
private DiscoveryAgentImpl()
Constructs the single instance.


         
      
Methods Summary
public voidaddCachedDevice(java.lang.String addr)
Adds address of remote device found during inquiry request to internal inquiry cache. The method does nothing if the RemoteDevice is already in the cache.

        RemoteDevice rd = getRemoteDevice(addr);
        synchronized (cachedDevices) {
            cachedDevices.put(addr, rd);
        }
    
public booleancancelInquiry(javax.bluetooth.DiscoveryListener listener)

        if (listener == null) {
            throw new NullPointerException("null listener");
        }
        synchronized (d_lock) {

            // no inquiry was started
            if (d_listener == null) {
                return false;
            }

            // not valid listener
            if (d_listener != listener) {
                return false;
            }
            
            // process the inquiry in the device specific way
            cancelInquiry();
        }
        
        inquiryCompleted(DiscoveryListener.INQUIRY_TERMINATED);
        return true;
    
private voidcancelInquiry()
Cancels inquiry in device specific way.

        BluetoothStack.getInstance().cancelInquiry(d_listener);
    
public booleancancelServiceSearch(int transID)

        if (DEBUG) {
            System.out.println("cancelServiceSearch: transID=" + transID);
        }
        return ServiceSearcher.cancel(transID);
    
private javax.bluetooth.RemoteDevice[]getCachedDevices()

        synchronized (cachedDevices) {
            int len = cachedDevices.size();
            if (len == 0) {
                return null;
            }
            RemoteDevice[] res = new RemoteDevice[len];
            Enumeration e = cachedDevices.elements();
            for (int i = 0; e.hasMoreElements(); i++) {
                res[i] = (RemoteDevice)e.nextElement();
            }
            return res;
        }
    
public static synchronized com.sun.kvem.jsr082.bluetooth.DiscoveryAgentImplgetInstance()
Returns the instance of this singleton constructing it if needed.

return
the only instance of DiscoveryAgentImpl.

        if (instance == null) {
            instance = new DiscoveryAgentImpl();
        }

        return instance;
    
public RemoteDeviceImplgetRemoteDevice(java.lang.String addr)
Porting interface: this method is used by the device specific implementation to create the RemoteDevice object by address. Also, this method puts the new remote devices into cache of known devices.

param
addr address of remote device to be created
return
new RemoteDeviceImplinstance if device with address given is unknown, the known one otherwise.

        synchronized (knownDevices) {
            addr = addr.toUpperCase();
            RemoteDeviceImpl rd = (RemoteDeviceImpl) knownDevices.get(addr);

            if (rd == null) {
                rd = new RemoteDeviceImpl(addr);
                knownDevices.put(addr, rd);
            }
            return rd;
        }
    
public voidinquiryCompleted(int discType)
Porting interface: this method is used by the device specific implementation to notify this class, that the current inquire has been completed.

param
discType type of completion: DiscoveryListener.INQUIRY_COMPLETED, or DiscoveryListener.INQUIRY_TERMINATED, or DiscoveryListener.INQUIRY_ERROR

        DiscoveryListener listener;
        synchronized (d_lock) {
            listener = d_listener;
            d_listener = null;
        }
        
        new Completed(listener, discType);
    
public javax.bluetooth.RemoteDevice[]retrieveDevices(int option)

        switch (option) {
            case DiscoveryAgent.CACHED:
                // IMPL_NOTE: use native cache keeping addresses of found devices
                // to share the cache between multiple isolates
                return getCachedDevices();
            case DiscoveryAgent.PREKNOWN:
                Vector pk = BCC.getInstance().getPreknownDevices();
                if (pk == null || pk.size() == 0) {
                    return null;
                }
                RemoteDevice[] res = new RemoteDevice[pk.size()];
                for (int i = 0; i < pk.size(); i++) {
                    String addr = (String)pk.elementAt(i);
                    res[i] = getRemoteDevice(addr);
                }
                return res;
            default:
                throw new IllegalArgumentException("Invalid option value: "
                        + option);
        }
    
public intsearchServices(int[] attrSet, javax.bluetooth.UUID[] uuidSet, javax.bluetooth.RemoteDevice btDev, javax.bluetooth.DiscoveryListener discListener)


        if (DEBUG) {
            System.out.println("searchServices: ");
            System.out.println("\tattrSet=" + attrSet);

            if (attrSet != null) {
                for (int i = 0; i < attrSet.length; i++) {
                    System.out.println("\tattrSet[" + i + "]=0x" + attrSet[i]);
                }
            }
            System.out.println("\tuuidSet=" + uuidSet);

            if (uuidSet != null) {
                for (int i = 0; i < uuidSet.length; i++) {
                    System.out.println("\tuuidSet[" + i + "]=" + uuidSet[i]);
                }
            }
            System.out.println("\tadderess=" + btDev.getBluetoothAddress());
        }
        ServiceSearcher searcher = new ServiceSearcher(attrSet, uuidSet,
                btDev, discListener);

        // the 'transID' is assigned by service discoverer (searcher)
        int transID = searcher.start();

        if (DEBUG) {
            System.out.println("\ttransID=" + transID);
        }
        return transID;
    
public java.lang.StringselectService(javax.bluetooth.UUID uuid, int security, boolean master)


        // use the separated class to light this one
        return selectServiceHandler.selectService(uuid, security, master);
    
public booleanstartInquiry(int accessCode, javax.bluetooth.DiscoveryListener listener)


        if (accessCode != DiscoveryAgent.GIAC &&
                accessCode != DiscoveryAgent.LIAC &&
                (accessCode < 0x9E8B00 || accessCode > 0x9E8B3F)) {
            throw new IllegalArgumentException("Access code is out of range: "
                    + accessCode);
        }

        if (listener == null) {
            throw new NullPointerException("null listener");
        }

        // IMPL_NOTE see
        // kvem/classes/com/sun/kvem/jsr082/impl/bluetooth/
        //         BTDeviceDiscoverer.java
        // heck what access codes should be supported.
        // Return false if access code is not supported.

        synchronized (d_lock) {
            if (d_listener != null) {
                throw new BluetoothStateException(
                        "The previous device discovery is running...");
            }
            d_listener = listener;

            // process the inquiry in the device specific way
            return startInquiry(accessCode);
        }
    
private booleanstartInquiry(int accessCode)

        return BluetoothStack.getEnabledInstance().startInquiry(
            accessCode, d_listener);