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

SelectServiceHandler

public final class SelectServiceHandler extends Object implements javax.bluetooth.DiscoveryListener
This class represents the module which is used by DiscoveryAgent#selectService method implementation.

Fields Summary
private static final boolean
DEBUG
Set to false in RR version - then the javac skip the code.
private Vector
btDevs
private Hashtable
btDevsHash
private Object
btDevsLock
private boolean
selectDevDisStarted
private boolean
selectDevDisStopped
private DiscoveryAgentImpl
agent
Constructors Summary
SelectServiceHandler(DiscoveryAgentImpl agent)
Constructs SelectServiceHandler for DiscoveryAgentImpl given.

param
agent the discovery agent to create instance for.


                        
      
        this.agent = agent;
    
Methods Summary
public voiddeviceDiscovered(javax.bluetooth.RemoteDevice btDevice, javax.bluetooth.DeviceClass cod)


        // if this bloototh device was found in preknown or
        // cached devices skips it now.
        if (btDevsHash.put(btDevice, btDevice) == null) {
            btDevs.addElement(btDevice);
        }
    
public voidinquiryCompleted(int discType)

        synchronized (btDevsLock) {
            selectDevDisStopped = true;
            btDevsLock.notify();
        }
    
private java.lang.StringselectFromDevicesList(javax.bluetooth.RemoteDevice[] devs, javax.bluetooth.UUID uuid, int security, boolean master, java.util.Hashtable disDevsHash)

        if (devs == null) {
            return null;
        }

        for (int i = 0; i < devs.length; i++) {
            if (disDevsHash.put(devs[i], devs[i]) != null) {
                continue;
            }
            String url = selectService(devs[i], uuid, security, master);

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

        if (DEBUG) {
            System.out.println("selectService:");
            System.out.println("\tuuid=" + uuid);
        }
        Vector disDevsVector = null;
        Hashtable disDevsHash = new Hashtable();

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

        // check in CACHED and PREKNOWN devices
        String url = selectFromDevicesList(agent.retrieveDevices(
                DiscoveryAgent.PREKNOWN), uuid, security, master, disDevsHash);

        if (url != null) {
            return url;
        }
        url = selectFromDevicesList(agent.retrieveDevices(
                DiscoveryAgent.CACHED), uuid, security, master, disDevsHash);

        if (url != null) {
            return url;
        }

        // start own device discovery now
        synchronized (btDevsLock) {
            if (selectDevDisStarted) {
                throw new BluetoothStateException(
                        "The previous device discovery is running...");
            }
            selectDevDisStarted = true;
            btDevs = new Vector();
            btDevsHash = disDevsHash;
        }

        try {
            agent.startInquiry(DiscoveryAgent.GIAC, this);
        } catch (BluetoothStateException btse) {
            synchronized (btDevsLock) {
                selectDevDisStarted = false;
                btDevs = null;
                btDevsHash = null;
            }
            throw btse;
        }

        synchronized (btDevsLock) {
            if (!selectDevDisStopped) {
                try {
                    btDevsLock.wait();
                } catch (InterruptedException ie) {
                    // ignore (breake waiting)
                }
                disDevsVector = btDevs;
                btDevs = null;
                btDevsHash = null;
                selectDevDisStarted = false;
                selectDevDisStopped = false;
            }
        }

        for (int i = 0; i < disDevsVector.size(); i++) {
            RemoteDevice btDev = (RemoteDevice) disDevsVector.elementAt(i);
            url = selectService(btDev, uuid, security, master);

            if (url != null) {
                if (DEBUG) {
                    System.out.println("\turl=" + url);
                }
                return url;
            }
        }
        if (DEBUG) {
            System.out.println("\turl=null");
        }
        return null;
    
private java.lang.StringselectService(javax.bluetooth.RemoteDevice btDev, javax.bluetooth.UUID uuid, int security, boolean master)

        UUID[] uuidSet = new UUID[] {uuid};
        ServiceSelector selector = new ServiceSelector(null, uuidSet, btDev);
        ServiceRecord serRec = selector.getServiceRecord();

        if (serRec == null) {
            return null;
        } else {
            return serRec.getConnectionURL(security, master);
        }
    
public voidserviceSearchCompleted(int transID, int respCode)

        throw new RuntimeException("unexpected call");
    
public voidservicesDiscovered(int transID, javax.bluetooth.ServiceRecord[] servRecord)

        throw new RuntimeException("unexpected call");