Java Code Examples for javax.bluetooth.LocalDevice

The following examples show how to use javax.bluetooth.LocalDevice. 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
Source Project: ShootOFF   Source File: BluetoothServer.java    License: GNU General Public License v3.0 6 votes vote down vote up
private Optional<String> getLocalAddress() {
	try {
		final LocalDevice localDevice = LocalDevice.getLocalDevice();

		// Insert colons into the address because android needs them
		final StringBuilder addressBuilder = new StringBuilder();
		final String originalAddress = localDevice.getBluetoothAddress();
		for (int i = 0; i < originalAddress.length(); i++) {
			addressBuilder.append(originalAddress.charAt(i));
			if (i > 0 && i < originalAddress.length() - 1 && i % 2 != 0) addressBuilder.append(':');
		}

		return Optional.of(addressBuilder.toString());
	} catch (BluetoothStateException e) {
		logger.error("Failed to access local bluetooth device to fetch its address. Ensure the "
				+ "system's bluetooth service is started with \"sudo systemctl start bluetooth\" "
				+ "and the bluetooth stack is on in the system settings", e);
		return Optional.empty();
	}
}
 
Example 2
Source Project: Ardulink-2   Source File: BluetoothDiscoveryUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static LocalDevice getLocalDevice() {
	try {
		return LocalDevice.getLocalDevice();
	} catch (BluetoothStateException e) {
		throw propagate(e);
	}
}
 
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
@SuppressWarnings( "PMD" )
public Boolean setDiscoverable( Integer i )
	throws FaultException {
	boolean b = false;
	try {
		b = LocalDevice.getLocalDevice().setDiscoverable( i );
	} catch( BluetoothStateException e ) {
		throw new FaultException( e );
	}
	return b;
}
 
Example 5
private void retrieveUrlCommonParams() {
    if (protocol != BluetoothUrl.UNKNOWN) {
        // already retrieved
        return;
    }

    if (remoteDevice != null) {
        btaddr = remoteDevice.getBluetoothAddress();
    } else {
        try {
            btaddr = LocalDevice.getLocalDevice().getBluetoothAddress();
        } catch (BluetoothStateException bse) {
            throw new IllegalArgumentException("cannot generate url");
        }
    }

    /*
     * There are three protocols supported -
     * they are obex or rfcomm or l2cap. So, if obex is
     * found in ProtocolDescriptorList, the protocol is btgoep,
     * if RFCOMM is found (and no obex) - the btspp, otherwise
     * the protocol is btl2cap.
     */
    DataElement protocolList = getAttributeValue(PROTOCOL_DESCRIPTOR_LIST);
    if (protocolList == null) {
    	return;
    }
    Enumeration val = (Enumeration) protocolList.getValue();
    int type = -1; // 0 = l2cap, 1 = spp, 2 = obex
    final UUID L2CAP_UUID = new UUID(0x0100);
    final UUID RFCOMM_UUID = new UUID(0x0003);
    final UUID OBEX_UUID = new UUID(0x0008);

    // go through all of the protocols in the protocols list
    while (val.hasMoreElements()) {
        DataElement protoDE = (DataElement) val.nextElement();

        // application adds a garbage in protocolList - ignore
        if (protoDE.getDataType() != DataElement.DATSEQ) {
            continue;
        }
        Enumeration protoEnum = (Enumeration) protoDE.getValue();
        int tmpPort = -1;
        int tmpType = -1;

        // look on protocol details
        while (protoEnum.hasMoreElements()) {
            DataElement de = (DataElement) protoEnum.nextElement();

            // may be PSM or channel id
            if (de.getDataType() == DataElement.U_INT_1 ||
                    de.getDataType() == DataElement.U_INT_2)  {
                tmpPort = (int) de.getLong();
            } else if (de.getDataType() == DataElement.UUID) {
                UUID protoUUID = (UUID) de.getValue();

                if (protoUUID.equals(L2CAP_UUID)) {
                    tmpType = 0;
                } else if (protoUUID.equals(RFCOMM_UUID)) {
                    tmpType = 1;
                } else if (protoUUID.equals(OBEX_UUID)) {
                    tmpType = 2;
                }
            }
        }

        /*
         * ok, new protocol has been parsed - let's check if it
         * is over the previous one or not.
         *
         * Note, that OBEX protocol may appear before the RFCOMM
         * one - in this case the port (channel id) is not set -
         * need to check this case separately.
         */
        if (tmpType > type) {
            type = tmpType;

            // no "port" for obex type (obex = 2)
            if (tmpType != 2) {
                port = tmpPort;
            }
        } else if (tmpType == 1) {
            port = tmpPort;
        }
    }

    switch (type) {
    case 0:
        protocol = BluetoothUrl.L2CAP;
        break;
    case 1:
        protocol = BluetoothUrl.RFCOMM;
        break;
    case 2:
        protocol = BluetoothUrl.OBEX;
        break;
    default:
        throw new IllegalArgumentException("wrong protocol list");
    }
}
 
Example 6
Source Project: blucat   Source File: BlucatServer.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void startServerRFCOMM() throws IOException{
	
	String url = "btspp://localhost:" + BluetoothConsts.RFCOMM_PROTOCOL_UUID + 
			";name=BlueCatPipe;authenticate=false;encrypt=false;master=true";
	
	PrintUtil.verbose("#" + "Creating RFCOMM server");
	
       StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open(url);

       ServiceRecord rec = LocalDevice.getLocalDevice().getRecord(service);
       
       //PrintUtil.out.println(rec.toString());
       String remoteUrl = rec.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
       remoteUrl = remoteUrl.substring(0, remoteUrl.indexOf(";"));

       PrintUtil.verbose("#" + new Date() + " - Listening at " + remoteUrl);
       
       BlucatConnection.handle(service);
}
 
Example 7
Source Project: blucat   Source File: BlucatServer.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void startServerUuid(String uuidValue) throws IOException{
	
	uuidValue = uuidValue.replace("-", "");
	
	UUID uuid = new UUID(uuidValue, false);
	
	String url = "btspp://localhost:" + uuid.toString() + 
			";name=BlueCatPipe"; //;authenticate=false;authorize=false;encrypt=false;master=false";
	
	PrintUtil.verbose("Creating server with UUID " + uuid);

       StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open(url);

       ServiceRecord rec = LocalDevice.getLocalDevice().getRecord(service);
       
       //PrintUtil.out.println(rec.toString());
       String remoteUrl = rec.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
       remoteUrl = remoteUrl.substring(0, remoteUrl.indexOf(";"));

       PrintUtil.verbose("#" + new Date() + " - Listening at " + remoteUrl);
       
       BlucatConnection.handle(service);

}
 
Example 8
Source Project: blucat   Source File: BlucatServer.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void startServerChannel(String port) throws IOException{
	
	String url = null;
	
	
	if (BlucatState.l2cap){
		url = "btl2cap://localhost:" + port + ";name=BlueCatL2CAP";
	}else{
		url = "btspp://localhost:" + port + ";name=BlueCatRFCOM";
	}
	
	PrintUtil.verbose("#" + "Creating server on channel " + port);
	
       Connection service = Connector.open(url);

       ServiceRecord rec = LocalDevice.getLocalDevice().getRecord(service);
       
       //PrintUtil.out.println(rec.toString());
       String remoteUrl = rec.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
       remoteUrl = remoteUrl.substring(0, remoteUrl.indexOf(";"));

       PrintUtil.verbose("#" + new Date() + " - Listening at " + remoteUrl);
       
       BlucatConnection.handle(service);
}
 
Example 9
Source Project: blucat   Source File: ListServices.java    License: 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;
	}