Java Code Examples for javax.bluetooth.DataElement#getLong()

The following examples show how to use javax.bluetooth.DataElement#getLong() . 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: ServiceRecordImpl.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
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 2
Source File: ServiceRecordImpl.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
public int getHandle() {
    DataElement handle = getAttributeValue(SERVICE_RECORD_HANDLE);
    return handle != null ? (int)handle.getLong() : 0;
}
 
Example 3
Source File: ServiceRecordImpl.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
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 4
Source File: BluetoothNotifier.java    From pluotsorbet with GNU General Public License v2.0 4 votes vote down vote up
protected boolean compareDataElements(DataElement first,
        DataElement second) {
    boolean ret = false;
    int valueType = first.getDataType();
    if (ret = (valueType == second.getDataType())) {
        switch (valueType) {
        case DataElement.BOOL:
            ret = first.getBoolean() == second.getBoolean();
            break;
        case DataElement.U_INT_1:
        case DataElement.U_INT_2:
        case DataElement.U_INT_4:
        case DataElement.INT_1:
        case DataElement.INT_2:
        case DataElement.INT_4:
        case DataElement.INT_8:
            ret = first.getLong() == second.getLong();
            break;
        default:
            Object v1 = first.getValue();
            Object v2 = second.getValue();
            if (v1 instanceof Enumeration && v2 instanceof Enumeration) {
                Enumeration e1 = (Enumeration)v1;
                Enumeration e2 = (Enumeration)v2;
                ret = true;
                while (e1.hasMoreElements() &&
                       e2.hasMoreElements() && ret) {
                    ret &= e1.nextElement().equals(e2.nextElement());
                }
                ret = ret &&
                    !(e1.hasMoreElements() ||
                      e2.hasMoreElements());
            } else if (v1 instanceof byte[] && v2 instanceof byte[]) {
                byte[] a1 = (byte[])v1;
                byte[] a2 = (byte[])v2;
                ret = a1.length == a2.length;
                for (int i = a1.length; --i >= 0 && ret; ) {
                    ret &= (a1[i] == a2[i]);
                }
            } else {
                ret = v1.equals(v2);
            }
            break;
        }
    }
    return ret;
}