ServiceRecordSerializerpublic class ServiceRecordSerializer extends Object Serializes ServiceRecord objects. |
Fields Summary |
---|
static DataElementSerializer | desHelper object used for data element serialization. |
Methods Summary |
---|
public static synchronized ServiceRecordImpl | restore(BluetoothNotifier notifier, byte[] data)Restores previously serialized service record.
DataElement seq;
try {
seq = des.restore(data);
} catch (IOException e) {
return null;
}
Enumeration elements = (Enumeration)seq.getValue();
int[] attrIDs = new int[seq.getSize() / 2];
DataElement[] attrValues = new DataElement[attrIDs.length];
for (int i = 0; i < attrIDs.length; i++) {
attrIDs[i] = (int)((DataElement)elements.nextElement()).getLong();
DataElement attrValue = (DataElement)elements.nextElement();
DataElement newAttrValue;
int dataType = attrValue.getDataType();
switch (dataType) {
case DataElement.BOOL:
newAttrValue = new DataElement(attrValue.getBoolean());
break;
case DataElement.NULL:
newAttrValue = new DataElement(DataElement.NULL);
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:
newAttrValue = new DataElement(dataType,
attrValue.getLong());
break;
case DataElement.DATALT:
case DataElement.DATSEQ:
Enumeration e = (Enumeration)attrValue.getValue();
newAttrValue = new DataElement(dataType);
while (e.hasMoreElements()) {
newAttrValue.addElement((DataElement)e.nextElement());
}
break;
default:
newAttrValue = new DataElement(attrValue.getDataType(),
attrValue.getValue());
break;
}
attrValues[i] = newAttrValue;
}
return new ServiceRecordImpl(notifier, attrIDs, attrValues);
| public static synchronized byte[] | serialize(javax.bluetooth.ServiceRecord record)Serializes given service record - creates an array of bytes representing
data elements as described in Bluetooth Specification Version 1.2,
vol 3, page 127.
des = new DataElementSerializer();
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;
}
|
|