Java Code Examples for javax.bluetooth.DiscoveryAgent

The following examples show how to use javax.bluetooth.DiscoveryAgent. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public 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);
    }
}
 
Example 2
Source Project: pluotsorbet   Source File: BluetoothPush.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void registerUrl(String url) throws IOException {
    ServiceRecordImpl record = null;
    String protocol = url.substring(0, url.indexOf(':')).toUpperCase();
    if (protocol.equals("BTL2CAP")) {
        record = L2CAPNotifierImpl.createServiceRecord(url);
    } else if (protocol.equals("BTSPP")) {
        record = BTSPPNotifierImpl.createServiceRecord(url);
    } else if (protocol.equals("BTGOEP")) {
        record = BTSPPNotifierImpl.createServiceRecord(url);
    } else {
        throw new RuntimeException("Unsupported Bluetooth protocol.");
    }
    record.setHandle(0);
    if (!BCC.getInstance().isBluetoothEnabled() &&
            !BCC.getInstance().enableBluetooth()) {
        throw new IOException("Bluetooth radio is not enabled.");
    }
    if (!registerUrl(url, srs.serialize(record))) {
        throw new IOException("Error registering Bluetooth URL.");
    }
    if (BCC.getInstance().getAccessCode() != DiscoveryAgent.GIAC) {
        BCC.getInstance().setAccessCode(DiscoveryAgent.GIAC);
    }
    // get the emulation services up and running
    SDDB.getInstance();
}
 
Example 3
public Value inquire()
	throws FaultException {
	Value retValue = null;
	try {
		DiscoveryListenerImpl listener = new DiscoveryListenerImpl();
		LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(
			DiscoveryAgent.GIAC,
			listener );
		retValue = listener.getResult();
	} catch( BluetoothStateException e ) {
		throw new FaultException( e );
	}
	return retValue;
}
 
Example 4
public boolean startInquiry(int accessCode, DiscoveryListener listener)
        throws BluetoothStateException {

    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);
    }
}
 
Example 5
Source Project: pluotsorbet   Source File: LocalDeviceImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
synchronized void notifyNewAccessCode(int oldCode, int newCode) {
    if (newCode == oldCode) {
        return;
    }
    savedCode = oldCode;

    if (newCode == DiscoveryAgent.LIAC) {
        // the currentCode was not LIAC - start a killer
        startTime = System.currentTimeMillis();
        new Thread(this).start();
    } else {
        /*
         * startTime != -1 if the killer is running, but
         * this method may be called by the killer itself -
         * then there is no need to stop it.
         */
        boolean stopKiller = startTime != -1 && isCanceledFromOutside;
        startTime = -1;
        isCanceledFromOutside = false;

        if (stopKiller) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
    }
}
 
Example 6
Source Project: pluotsorbet   Source File: LocalDeviceImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
public boolean setDiscoverable(int accessCode)
        throws BluetoothStateException {
    // Check if the specified mode has a valid value
    if (accessCode != DiscoveryAgent.GIAC &&
            accessCode != DiscoveryAgent.LIAC &&
            accessCode != DiscoveryAgent.NOT_DISCOVERABLE &&
            (accessCode < 0x9E8B00 || accessCode > 0x9E8B3F)) {
        throw new IllegalArgumentException("Access code is out of range: "
                + "0x" + Integer.toHexString(accessCode).toUpperCase());
    }
    synchronized (cancelerOfLIAC) {
        /*
         * Accroding to the spec, the device should only be limited
         * discoverable (DiscoveryAgent.LIAC) for 1 minute -
         * then back to the PREVIOUS discoverable mode.
         */
        int oldAccessCode = BCC.getInstance().getAccessCode();
        if (BCC.getInstance().setAccessCode(accessCode)) {
            cancelerOfLIAC.notifyNewAccessCode(oldAccessCode, accessCode);
            if (accessCode != DiscoveryAgent.NOT_DISCOVERABLE) {
                // Start SDDB if discoverable mode was set successfully
                // IMPL_NOTE: Do we really need this step?
                SDDB.getInstance();
            }
            return true;
        }
    }
    return false;
}
 
Example 7
String selectService(UUID uuid, int security, boolean master)
        throws BluetoothStateException {
    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;
}