javax.bluetooth.RemoteDevice Java Examples

The following examples show how to use javax.bluetooth.RemoteDevice. 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
Source File: ArdulinkDiscoveryListener.java    From Ardulink-1 with Apache License 2.0 6 votes vote down vote up
@Override
public void serviceSearchCompleted(int arg0, int arg1) {
	
	Map<String, ServiceRecord> ports = new HashMap<String, ServiceRecord>();
	
	for (Entry<RemoteDevice, ServiceRecord[]> entry : services.entrySet()) {
		RemoteDevice remoteDevice = entry.getKey();
		ServiceRecord service = findService(entry.getValue());
		if (service != null) {
			String name = "noname";
			try {
				name = remoteDevice.getFriendlyName(false);
			} catch (Exception e) {
			}

			name += " " + remoteDevice.getBluetoothAddress();
			ports.put(name, service);
		}
	}
	
	bluetoothConnection.setPorts(ports);
	
       synchronized (lock) {
           lock.notify();
       }
}
 
Example #2
Source File: ServiceSearcher.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
public int searchService(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev,
        DiscoveryListener discListener) throws BluetoothStateException,
        IllegalArgumentException {

    if (DEBUG) {
        System.out.println("- serviceSearcher: initializing");
    }
    initialize(attrSet, uuidSet, btDev);

    if (discListener == null) {
        throw new NullPointerException("DiscoveryListener is null");
    }
    this.discListener = discListener;

    return start();
}
 
Example #3
Source File: SelectServiceHandler.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
private String selectFromDevicesList(RemoteDevice[] devs, UUID uuid,
        int security, boolean master, 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;
}
 
Example #4
Source File: BluetoothStack.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
void onInquiryResult(InquiryResult result) {
    if (discListener == null) {
        return;
    }
    String addr = result.getAddress();
    Enumeration e = inquiryHistory.elements();
    while (e.hasMoreElements()) {
        InquiryResult oldResult = (InquiryResult)e.nextElement();
        if (oldResult.getAddress().equals(addr)) {
            // inquiry result is already in our possession
            return;
        }
    }
    inquiryHistory.addElement(result);
    RemoteDevice dev
        = DiscoveryAgentImpl.getInstance().getRemoteDevice(addr);
    DiscoveryAgentImpl.getInstance().addCachedDevice(addr);
    discListener.deviceDiscovered(dev, result.getDeviceClass());
}
 
Example #5
Source File: DiscoveryAgentImpl.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
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 #6
Source File: BluetoothService.java    From jolie with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void deviceDiscovered( RemoteDevice btDevice, DeviceClass cod ) {
	Value dValue = Value.create();
	dValue.getFirstChild( "address" ).setValue( btDevice.getBluetoothAddress() );
	try {
		dValue.getFirstChild( "name" ).setValue( btDevice.getFriendlyName( true ) );
	} catch( IOException e ) {
	}
	value.getChildren( "device" ).add( dValue );
}
 
Example #7
Source File: BluetoothDiscovery.java    From fuchsia with Apache License 2.0 5 votes vote down vote up
@Unbind
public void unbindRemoteNamedDevice(RemoteDevice device) {
    String ba = device.getBluetoothAddress();
    LOG.debug("Remove declaration for the device " + ba);

    unregisterImportDeclaration(bluetoothDevices.remove(ba));
}
 
Example #8
Source File: BluetoothDiscovery.java    From fuchsia with Apache License 2.0 5 votes vote down vote up
@Bind(aggregate = true, optional = true)
public void bindRemoteNamedDevice(RemoteDevice device) {
    ImportDeclaration iDec = null;
    String ba = null;
    String deviceName = null;
    try {
        ba = device.getBluetoothAddress();
        deviceName = device.getFriendlyName(false);
        LOG.debug("Building declaration for device " + ba);

        iDec = ImportDeclarationBuilder.empty()
                .key(ID).value(ba)
                .key(PROTOCOL_NAME).value("bluetooth")
                .key(BLUETOOTH_DEVICE_ADDRESS).value(ba)
                .key(BLUETOOTH_DEVICE_FRIENDLYNAME).value(deviceName)
                .key("scope").value("generic")
                .build();

    } catch (IOException e) {
        LOG.error("Can't get description from the device, maybe is already gone.", e);
        return;
    }

    LOG.debug("Add declaration for the device " + ba + "(" + deviceName + ")");

    registerImportDeclaration(iDec);
    bluetoothDevices.put(ba, iDec);
}
 
Example #9
Source File: BTDeviceDiscoveryService.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * transforms a {@link RemoteDevice} object from the bluecove API to our own datastructure for bluetooth devices
 * 
 * @param btDevice the device coming from the bluecove API
 * @return an instance of our own bluetooth device data structure
 */
private static BluetoothDevice toBluetoothDevice(RemoteDevice btDevice) {
    String address = btDevice.getBluetoothAddress();
    String friendlyName = "";
    try {
        friendlyName = btDevice.getFriendlyName(false);
    } catch (IOException e) {
        // no friendly name accessible, let's ignore that
    }
    boolean paired = btDevice.isTrustedDevice();
    return new BluetoothDevice(address, friendlyName, paired);
}
 
Example #10
Source File: SelectServiceHandler.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
private String selectService(RemoteDevice btDev, 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);
    }
}
 
Example #11
Source File: SelectServiceHandler.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
public void deviceDiscovered(RemoteDevice btDevice, 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);
        }
    }
 
Example #12
Source File: DiscoveryAgentImpl.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
private 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;
    }
}
 
Example #13
Source File: ServiceSearcherBase.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
protected void initialize(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev)
    throws IllegalArgumentException, NullPointerException {

    this.btDev = btDev;
    this.attrSet = ServiceSearcherBase.extendByStandardAttrs(attrSet);
    this.uuidSet = ServiceSearcherBase.removeDuplicatedUuids(uuidSet);
}
 
Example #14
Source File: ArdulinkDiscoveryListener.java    From Ardulink-1 with Apache License 2.0 5 votes vote down vote up
@Override
public void servicesDiscovered(int transID, ServiceRecord[] serviceRecords) {
	for(ServiceRecord serviceRecord : serviceRecords) {
		RemoteDevice currentDevice = serviceRecord.getHostDevice();
		services.put(currentDevice, serviceRecords);
	}
}
 
Example #15
Source File: ServiceSelector.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
ServiceSelector(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev) {
    super(attrSet, uuidSet, btDev);
}
 
Example #16
Source File: ArdulinkDiscoveryListener.java    From Ardulink-1 with Apache License 2.0 4 votes vote down vote up
@Override
public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {
       devices.add(remoteDevice);
}
 
Example #17
Source File: ListServices.java    From blucat with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1) {
	//PrintUtil.out.println("deviceDiscovered");
}
 
Example #18
Source File: ServiceDiscoverer.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public int searchService(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev,
DiscoveryListener discListener) throws BluetoothStateException ;
 
Example #19
Source File: ArdulinkDiscoveryListener.java    From Ardulink-1 with Apache License 2.0 4 votes vote down vote up
public List<RemoteDevice> getDevices() {
	return devices;
}
 
Example #20
Source File: ArdulinkDiscoveryListener.java    From Ardulink-1 with Apache License 2.0 4 votes vote down vote up
public Map<RemoteDevice, ServiceRecord[]> getServices() {
	return services;
}
 
Example #21
Source File: DiscoveryAgentImpl.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public void addCachedDevice(String addr) {
    RemoteDevice rd = getRemoteDevice(addr);
    synchronized (cachedDevices) {
        cachedDevices.put(addr, rd);
    }
}
 
Example #22
Source File: SelectServiceHandler.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
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;
}
 
Example #23
Source File: ServiceSearcherBase.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
ServiceSearcherBase(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev) {
    super();
    initialize(attrSet, uuidSet, btDev);
}
 
Example #24
Source File: BluetoothConnection.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public RemoteDevice getRemoteDevice() throws IOException {
    checkOpen();
    return remoteDevice;
}
 
Example #25
Source File: ServiceRecordImpl.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public RemoteDevice getHostDevice() {
    return remoteDevice;
}
 
Example #26
Source File: ServiceRecordImpl.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public ServiceRecordImpl(RemoteDevice device, int[] attrIDs,
        DataElement[] attrValues) {
    init(attrIDs, attrValues);
    remoteDevice = device;
}
 
Example #27
Source File: DiscoveryAgentImpl.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public int searchServices(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev,
        DiscoveryListener discListener) throws BluetoothStateException {

    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());
    }
    if (uuidSet == null) {
        throw new NullPointerException("UUID set is null");
    }

    if (uuidSet.length == 0 || uuidSet.length > MAX_ALLOWED_UUIDS ) {
        throw new IllegalArgumentException("Invalid UUID set length");
    }

    if (btDev == null) {
        throw new NullPointerException("null instance of RemoteDevice");
    }
    
    /* the 'transID' is assigned by service discoverer */
    int transID = ServiceDiscovererFactory.getServiceDiscoverer().
            searchService(
                    ServiceSearcherBase.extendByStandardAttrs(attrSet), 
                    ServiceSearcherBase.removeDuplicatedUuids(uuidSet), 
                    btDev, discListener);

    if (DEBUG) {
        System.out.println("\ttransID=" + transID);
    }
    return transID;
}
 
Example #28
Source File: ListServices.java    From blucat with GNU General Public License v2.0 2 votes vote down vote up
static Set<ServiceRecord> findViaSDP() throws Exception{
		
		Set<ServiceRecord> toReturn = new HashSet<ServiceRecord>();
		
		UUID[] uuidSet ={
				//new UUID(0x1002),
			//BluetoothConsts.RFCOMM_PROTOCOL_UUID,
			BluetoothConsts.L2CAP_PROTOCOL_UUID
//			BluetoothConsts.OBEX_PROTOCOL_UUID,
//			new UUID(0x0003)

			
			
		};
		
        int[] attrIDs =  new int[] {
                0x0100 // Service name
                ,0x0003
        };
		
        
        RemoteDeviceDiscovery.findDevices();
		Set<RemoteDevice> devices  = RemoteDeviceDiscovery.getDevices();
		
		
		for (RemoteDevice remote : devices){
        
	        synchronized(serviceSearchCompletedEvent) {
	        	
	        	PrintUtil.verbose("#" + "Searching for services on ");
	            PrintUtil.out.println("+," + RemoteDeviceDiscovery.deviceName(remote));
	        
	            LocalDevice.getLocalDevice().getDiscoveryAgent()
	            .searchServices(attrIDs, uuidSet, remote, new ServiceDiscoveryListener(toReturn));
	            
	            serviceSearchCompletedEvent.wait();
	        }
		
		}
		return toReturn;
	}
 
Example #29
Source File: BTServiceDiscoveryListener.java    From jolie with GNU Lesser General Public License v2.1 votes vote down vote up
public void deviceDiscovered( RemoteDevice btDevice, DeviceClass cod ) {}