Java Code Examples for javax.bluetooth.DataElement

The following examples show how to use javax.bluetooth.DataElement. 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 static synchronized byte[] serialize(ServiceRecord record) {
    DataElement seq = new DataElement(DataElement.DATSEQ);
    int[] attrIDs = record.getAttributeIDs();
    for (int i = 0; i < attrIDs.length; i++) {
        DataElement attrID = new DataElement(DataElement.U_INT_2,
                attrIDs[i]);
        DataElement attrValue = record.getAttributeValue(attrIDs[i]);
        if (attrValue != null) {
            seq.addElement(attrID);
            seq.addElement(attrValue);
        }
    }
    try {
        return des.serialize(seq);
    } catch (IOException e) {
        return null;
    }
}
 
Example 2
public synchronized ServiceRecordImpl copy() {
    int count = attributesTable.size();
    int[] attrIDs = new int[count];
    DataElement[] attrValues = new DataElement[count];

    Enumeration ids = attributesTable.keys();
    Enumeration values = attributesTable.elements();

    for (int i = 0; i < count; i++) {
        attrIDs[i] = ((Integer)ids.nextElement()).intValue();
        // no nedd to copy elements here; service record constructor
        // performs the copying
        attrValues[i] = (DataElement)values.nextElement();
    }

    ServiceRecordImpl servRec = new ServiceRecordImpl(notifier,
                  attrIDs, attrValues);
    servRec.serviceClasses = serviceClasses;
    return servRec;
}
 
Example 3
public synchronized boolean setAttributeValue(
        int attrID, DataElement attrValue) {

    if ((attrID & MASK_OVERFLOW) != 0) {
        throw new IllegalArgumentException(
                "attrID does not represent a 16-bit unsigned integer");
    }

    if (attrID == SERVICE_RECORD_HANDLE) {
        throw new IllegalArgumentException(
                "attrID is the value of ServiceRecordHandle (0x0000)");
    }

    if (remoteDevice != null) {
        throw new RuntimeException(
                "can't update ServiceRecord of the RemoteDevice");
    }
    Object key = new Integer(attrID);

    if (attrValue == null) {
        return attributesTable.remove(key) != null;
    } else {
        attributesTable.put(key, dataElementCopy(attrValue));
        return true;
    }
}
 
Example 4
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 5
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 6
Source Project: Ardulink-1   Source File: ArdulinkDiscoveryListener.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Find service for a device that is named: OBEX Object Push
 * @param serviceRecords
 * @return the service record
 */
private ServiceRecord findService(ServiceRecord[] serviceRecords) {
	ServiceRecord retvalue = null;
	if(serviceRecords.length == 1) {
		retvalue = serviceRecords[0];
	} else {
		for (int i = 0; i < serviceRecords.length; i++) {
            DataElement serviceName = serviceRecords[i].getAttributeValue(0x0100);
            if (serviceName != null && serviceName.getValue().equals("DevB")) {
            	retvalue = serviceRecords[i];
            }
		}
	}
	return retvalue;
}
 
Example 7
Source Project: pluotsorbet   Source File: ServiceSelector.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void serviceAttributeResponse(int[] attrIDs,
        DataElement[] attributeValues, int transactionID) {
    if (DEBUG) {
        System.out.println(cn +
                ".serviceAttributeResponse: unexpected call");
    }
    throw new RuntimeException("unexpected call");
}
 
Example 8
Source Project: pluotsorbet   Source File: ServiceSelector.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void serviceSearchAttributeResponse(int[] attrIDs,
        DataElement[] attributeValues, int transactionID) {
    if (DEBUG) {
        System.out.println(cn + ".serviceSearchAttributeResponse: called");
    }
    synchronized (this) {
        attrSet = attrIDs;
        attrValues = attributeValues;
        notify();
    }
}
 
Example 9
public void setHandle(int handle) {
    Integer attrID = new Integer(SERVICE_RECORD_HANDLE);
    attributesTable.remove(attrID);
    attributesTable.put(attrID, new DataElement(
            DataElement.U_INT_4, handle));
    recHandle = handle;
}
 
Example 10
private DataElement dataElementCopy(DataElement original) {
    if ((original.getDataType() == DataElement.DATSEQ)
            || (original.getDataType() == DataElement.DATALT)) {
        DataElement copy = new DataElement(original.getDataType());
        Enumeration elements = (Enumeration) original.getValue();

        while (elements.hasMoreElements()) {
            copy.addElement(dataElementCopy((DataElement)
                    elements.nextElement()));
        }
        return copy;
    } else {
        return original;
    }
}
 
Example 11
public DataElement getAttributeValue(int attrID) {
    if ((attrID & MASK_OVERFLOW) != 0) {
        throw new IllegalArgumentException(
                "attrID isn't a 16-bit unsigned integer");
    }
    DataElement attrValue = (DataElement) attributesTable.get(new
            Integer(attrID));

    if (attrValue == null) {
        return null;
    } else {
        return dataElementCopy(attrValue);
    }
}
 
Example 12
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 13
public void serviceAttributeResponse(int[] attributeIDs,
    DataElement[] attributeValues, int transactionID) {
    synchronized (this) {
        attrIDs = attributeIDs;
        attrValues = attributeValues;
        notify();
    }
}
 
Example 14
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 15
public synchronized byte[] serialize(DataElement data) throws IOException {
    writeBuffer = new byte[(int)getDataSize(data)];
    writePos = 0;
    writeDataElement(data);
    byte[] result = writeBuffer;
    writeBuffer = null;
    return result;
}
 
Example 16
public synchronized DataElement restore(byte[] data) throws IOException {
    readBuffer = data;
    readPos = 0;
    DataElement result = readDataElement();
    readBuffer = null;
    return result;
}
 
Example 17
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 18
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 19
ClientServiceAttributeTransaction(JavaSDPClient client, int transactionID,
        SDPResponseListener listener, int recordHandle,
        int[] attrSet) {
    super(client, SDPClientTransaction.SDP_SERVICE_ATTRIBUTE_REQUEST, transactionID, listener);
    serviceRecordHandle = recordHandle;
    attributeIDList = new DataElement(DataElement.DATSEQ);
    for (int i = 0; i < attrSet.length; i++) {
        attributeIDList.addElement(new DataElement(
                DataElement.U_INT_2, attrSet[i]));
    }
    parameterLength = super.client.getConnection().getReaderWriter().getDataSize(attributeIDList) + 6;
}
 
Example 20
public void complete() {
    DataElement attrList;
    DataElementSerializer des = new DataElementSerializer();
    try {
        attrList = des.restore(attributes);
    } catch (IOException e) {
        listener.serviceAttributeResponse(null, null,
                ssTransID);
        return;
    }
    int size = attrList.getSize() / 2;
    if (size == 0) {
        listener.serviceAttributeResponse(null, null,
                ssTransID);
        return;
    }
    Enumeration elements = (Enumeration)attrList.getValue();
    final int[] attrIDs = new int[size];
    final DataElement[] attrValues = new DataElement[size];
    for (int i = 0; elements.hasMoreElements(); i++) {
        attrIDs[i] = (int)((DataElement)
                elements.nextElement()).getLong();
        attrValues[i] = ((DataElement)
                elements.nextElement());
    }
    listener.serviceAttributeResponse(attrIDs, attrValues, ssTransID);
}
 
Example 21
Source Project: pluotsorbet   Source File: ServiceSearcher.java    License: GNU General Public License v2.0 5 votes vote down vote up
private ServiceRecordImpl getOneRecord(int[] attrIDs,
        DataElement[] attributeValues, int firstPos, int curPos) {

    int aLength = curPos - firstPos;
    int[] aIDs = new int[aLength];
    DataElement[] aValues = new DataElement[aLength];
    System.arraycopy(attrIDs, firstPos, aIDs, 0, aLength);
    System.arraycopy(attributeValues, firstPos, aValues, 0, aLength);
    ServiceRecordImpl record = 
                      new ServiceRecordImpl(btDev, aIDs, aValues);
    return record;
}
 
Example 22
abstract public void serviceAttributeResponse(int[] attrIDs,
DataElement[] attributeValues, int transactionID);
 
Example 23
abstract public void serviceSearchAttributeResponse(int[] attrIDs,
DataElement[] attributeValues, int transactionID);
 
Example 24
public ServiceRecordImpl(RemoteDevice device, int[] attrIDs,
        DataElement[] attrValues) {
    init(attrIDs, attrValues);
    remoteDevice = device;
}
 
Example 25
public ServiceRecordImpl(BluetoothNotifier notifier, int[] attrIDs,
        DataElement[] attrValues) {
    init(attrIDs, attrValues);
    this.notifier = notifier;
}
 
Example 26
public int getHandle() {
    DataElement handle = getAttributeValue(SERVICE_RECORD_HANDLE);
    return handle != null ? (int)handle.getLong() : 0;
}
 
Example 27
private void init(int[] attrIDs, DataElement[] attrValues) {
    attributesTable = new Hashtable(attrIDs.length + 1);
    attrsInit(attrIDs, attrValues);
}
 
Example 28
private void attrsInit(int[] attrIDs, DataElement[] attrValues) {
    for (int i = 0; i < attrIDs.length; i++) {
        attributesTable.put(new Integer(attrIDs[i]),
                dataElementCopy(attrValues[i]));
    }
}
 
Example 29
public synchronized boolean populateRecord(int[] attrIDs)
    throws IOException {
    Hashtable dupChecker = new Hashtable();
    Object checkObj = new Object();

    if (remoteDevice == null) {
        throw new RuntimeException("local ServiceRecord");
    }

    if (attrIDs.length == 0) {
        throw new IllegalArgumentException("attrIDs size is zero");
    }

    if (attrIDs.length > RETRIEVABLE_MAX) {
        throw new IllegalArgumentException(
                "attrIDs size exceeds retrievable.max");
    }

    for (int i = 0; i < attrIDs.length; i++) {
        if ((attrIDs[i] & MASK_OVERFLOW) != 0) {
            throw new IllegalArgumentException("attrID does not represent "
                + "a 16-bit unsigned integer");
        }

        // check attribute ID duplication
        if (dupChecker.put(new Integer(attrIDs[i]), checkObj) != null) {
            throw new IllegalArgumentException(
                    "duplicated attribute ID");
        }
    }

    // obtains transaction ID for request
    short transactionID = SDPClientTransactionBase.newTransactionID();

    // SDP connection and listener. They are initialized in try blok.
    SDPClient sdp = null;
    SRSDPListener listener = null;

    try {
        // prepare data for request
        DataElement handleEl = (DataElement) attributesTable.get(
                new Integer(SERVICE_RECORD_HANDLE));
        int handle = (int) handleEl.getLong();

        // create and prepare SDP listner
        listener = new SRSDPListener();

        // create SDP connection and ..
        if (sdpClient == null) {
            sdp = ServiceDiscovererFactory.getServiceDiscoverer().
                    getSDPClient(remoteDevice.getBluetoothAddress());
        } else {
            sdp = sdpClient;
        }

        // ... and make request
        sdp.serviceAttributeRequest(handle, attrIDs, transactionID,
                listener);

        synchronized (listener) {
            if ((listener.ioExcpt == null)
                    && (listener.attrValues == null)) {
                try {
                    listener.wait();
                } catch (InterruptedException ie) {
                    // ignore (breake waiting)
                }
            }
        }
    } finally {

        // Closes SDP connection and frees transaction ID in any case
        SDPClientTransactionBase.freeTransactionID(transactionID);

        // if connection was created try to close it
        if (sdp != null) {
            try {
                sdp.close();
            } catch (IOException ioe) {
                // ignore
            }
        }
    }

    if (listener.ioExcpt != null) {
        throw listener.ioExcpt;
    } else if (listener.attrValues == null) {
        return false;
    } else if (listener.attrValues.length == 0) {
        return false;
    } else {
        attrsInit(listener.attrIDs, listener.attrValues);
        return true;
    }
}
 
Example 30
public void serviceSearchAttributeResponse(int[] attrIDs,
    DataElement[] attributeValues, int transactionID) {
    throw new RuntimeException("unexpected call");
}