Java Code Examples for javax.bluetooth.UUID

The following examples show how to use javax.bluetooth.UUID. 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
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 2
Source Project: pluotsorbet   Source File: ServiceSearcher.java    License: 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
public ClientServiceSearchAttributeTransaction(JavaSDPClient client, int transactionID,
        SDPResponseListener listener, int[] attrSet,
        UUID[] uuidSet) {
    super(client, SDPClientTransaction.SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST, transactionID,
            listener);
    attrData = new DataElement(DataElement.DATSEQ);
    uuidData = new DataElement(DataElement.DATSEQ);
    for (int i = 0; i < attrSet.length; i++) {
        attrData.addElement(new DataElement(DataElement.U_INT_2,
                attrSet[i]));
    }
    for (int i = 0; i < uuidSet.length; i++) {
        uuidData.addElement(new DataElement(DataElement.UUID,
                uuidSet[i]));
    }
    parameterLength = super.client.getConnection().getReaderWriter().getDataSize(attrData) +
            super.client.getConnection().getReaderWriter().getDataSize(uuidData) + 2;
}
 
Example 4
public void servicesDiscovered( int transID, ServiceRecord[] serviceRecords ) {
	DataElement e;
	ServiceRecord r;
	Enumeration< DataElement > en;
	boolean keepRun = true;
	for( int i = 0; i < serviceRecords.length && keepRun; i++ ) {
		r = serviceRecords[ i ];
		// Search for the desired UUID
		if( (e = r.getAttributeValue( 0x0001 )) != null ) {
			if( e.getDataType() == DataElement.DATSEQ ) {
				en = (Enumeration< DataElement >) e.getValue();
				Object o;
				while( en.hasMoreElements() ) {
					o = en.nextElement().getValue();
					if( o instanceof UUID ) {
						if( o.equals( uuid ) ) {
							serviceRecord = r;
							keepRun = false;
						}
					}
				}
			} else if( e.getDataType() == DataElement.UUID ) {
				if( e.getValue().equals( uuid ) ) {
					serviceRecord = r;
					keepRun = false;
				}
			}
		}
	}
}
 
Example 5
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 6
public static UUID[] removeDuplicatedUuids( UUID[] uuidSet ) 
    throws IllegalArgumentException, NullPointerException {
    Hashtable uniquies = new Hashtable();
    UUID[] uuids;
    if ((uuidSet != null) && (uuidSet.length > 0)) {
    /* uuid checking */
        for (int i = 0; i < uuidSet.length; i++) {

            if (uuidSet[i] == null) {
                throw new NullPointerException("Invalid UUID. Null");
            }

            /* check UUID duplication */
            if (uniquies.put(uuidSet[i], FAKE_VALUE) != null) {
                throw new IllegalArgumentException("Duplicated UUID: " + 
                        uuidSet[i]);
            }
        }

        uuids = new UUID[uniquies.size()];
        Enumeration keys = uniquies.keys();

        for (int i = 0; keys.hasMoreElements(); i++) {
            uuids[i] = (UUID) keys.nextElement();
        }
    } else {
        uuids = new UUID[0];
    }
    return uuids;
}
 
Example 7
public String selectService(UUID uuid, int security, boolean master)
            throws BluetoothStateException {

        // use the separated class to light this one
        return selectServiceHandler.selectService(uuid, security, master);
//        return ServiceDiscovererFactory.getServiceDiscoverer().
//                                 selectService(uuid, security, master, this);
    }
 
Example 8
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 9
public ClientServiceSearchTransaction(JavaSDPClient client, int transactionID,
        SDPResponseListener listener, UUID[] uuidSet) {
    super(client, SDPClientTransaction.SDP_SERVICE_SEARCH_REQUEST, transactionID, listener);
    serviceSearchPattern = new DataElement(DataElement.DATSEQ);
    for (int i = 0; i < uuidSet.length; i++) {
        serviceSearchPattern.addElement(new DataElement(
                DataElement.UUID, uuidSet[i]));
    }
    parameterLength = super.des.getDataSize(serviceSearchPattern) + 2;
}
 
Example 10
public long getDataSize(DataElement data) {
    int type = data.getDataType();
    long size = getPureDataSize(data);
    if ((type == DataElement.NULL) || (type == DataElement.BOOL)
            || (type == DataElement.INT_1) || (type == DataElement.U_INT_1)
            || (type == DataElement.INT_2) || (type == DataElement.U_INT_2)
            || (type == DataElement.INT_4) || (type == DataElement.U_INT_4)
            || (type == DataElement.INT_8) || (type == DataElement.U_INT_8)
            || (type == DataElement.INT_16)
            || (type == DataElement.U_INT_16)
            || (type == DataElement.UUID)) {
        return size + 1;
    } else if ((type == DataElement.DATSEQ)
            || (type == DataElement.DATALT) || (type == DataElement.STRING)
            || (type == DataElement.URL)) {
        if (size <= 0xffL) {
            return size + 2;
        } else if (size <= 0xffffL) {
            return size + 3;
        } else if (size <= 0xffffffffL) {
            return size + 5;
        } else {
            throw new RuntimeException("Data size is too large.");
        }
    } else {
        throw new RuntimeException("Unexpected data type.");
    }
}
 
Example 11
public long getPureDataSize(DataElement data) {
    switch (data.getDataType()) {
        case DataElement.NULL:
            return 0;
        case DataElement.BOOL:
        case DataElement.INT_1:
        case DataElement.U_INT_1:
            return 1;
        case DataElement.INT_2:
        case DataElement.U_INT_2:
            return 2;
        case DataElement.INT_4:
        case DataElement.U_INT_4:
            return 4;
        case DataElement.INT_8:
        case DataElement.U_INT_8:
            return 8;
        case DataElement.INT_16:
        case DataElement.U_INT_16:
            return 16;
        case DataElement.DATSEQ:
        case DataElement.DATALT:
            long size = 0;
            Enumeration elements = (Enumeration)data.getValue();
            while (elements.hasMoreElements()) {
                size += getDataSize((DataElement)elements.nextElement());
            }
            return size;
        case DataElement.STRING:
        case DataElement.URL:
            return ((String)data.getValue()).length();
        case DataElement.UUID:
            return 16;
        default:
            throw new RuntimeException("Unknown data type.");
    }
}
 
Example 12
public UUID readUUID(int len) throws IOException {
    String uuid = "";
    for (int i = 0; i < len; i++) {
        String digit = Integer.toHexString(readByte() & 0xff);
        if (digit.length() == 1) digit = '0' + digit;
        uuid += digit;
    }
    return new UUID(uuid, len < 16);
}
 
Example 13
Source Project: pluotsorbet   Source File: JavaSDPClient.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void serviceSearchRequest(UUID[] uuidSet, int transactionID,
      SDPResponseListener listener) throws IOException {
  	SDPClientTransaction tr = null;
      tr = new ClientServiceSearchTransaction(this, transactionID, listener, uuidSet);
      synchronized (ssTrnas) {
      	ssTrnas.put(tr.getID(), tr);
}
      tr.start();
  }
 
Example 14
Source Project: pluotsorbet   Source File: JavaSDPClient.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void serviceSearchAttributeRequest(int[] attrSet, UUID[] uuidSet,
      int transactionID, SDPResponseListener listener) throws IOException {
  	SDPClientTransaction tr = new ClientServiceSearchAttributeTransaction( this, transactionID, listener, attrSet, uuidSet );
      synchronized (ssTrnas) {
      	ssTrnas.put(tr.getID(), tr);
}
      tr.start();
  }
 
Example 15
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 16
Source Project: Ardulink-2   Source File: BluetoothDiscoveryUtil.java    License: Apache License 2.0 4 votes vote down vote up
private static UUID[] serialPortService() {
	return new UUID[] { new UUID(0x1101) };
}
 
Example 17
public BTServiceDiscoveryListener( UUID uuid ) {
	this.uuid = uuid;
}
 
Example 18
Source Project: Ardulink-1   Source File: BluetoothConnection.java    License: Apache License 2.0 4 votes vote down vote up
private static UUID[] serialPortService() {
	return new UUID[] { new UUID(0x1101) }; // Serial Port Service
}
 
Example 19
Source Project: pluotsorbet   Source File: ServiceSelector.java    License: GNU General Public License v2.0 4 votes vote down vote up
ServiceSelector(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev) {
    super(attrSet, uuidSet, btDev);
}
 
Example 20
ServiceSearcherBase(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev) {
    super();
    initialize(attrSet, uuidSet, btDev);
}
 
Example 21
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 22
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 Project: pluotsorbet   Source File: SDPClient.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void serviceSearchAttributeRequest(int[] attrSet, UUID[] uuidSet,
int transactionID, SDPResponseListener listener) throws IOException;
 
Example 24
public int searchService(int[] attrSet, UUID[] uuidSet, RemoteDevice btDev,
DiscoveryListener discListener) throws BluetoothStateException ;
 
Example 25
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 26
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;
	}