Java Code Examples for javax.bluetooth.DataElement#U_INT_4

The following examples show how to use javax.bluetooth.DataElement#U_INT_4 . 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: DataElementSerializer.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
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 2
Source File: DataElementSerializer.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
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 3
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;
}