Added some more javadoc

This commit is contained in:
Alexandros Schillings
2014-03-20 12:28:25 +00:00
parent a0aa259606
commit f30cb50f3c
3 changed files with 128 additions and 32 deletions
@@ -10,8 +10,10 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.util.SparseArray;
/**
* The Class AdRecordStore.
*/
public class AdRecordStore implements Parcelable{
private final SparseArray<AdRecord> mAdRecords;
private final String mLocalNameComplete;
private final String mLocalNameShort;
@@ -33,6 +35,11 @@ public class AdRecordStore implements Parcelable{
mLocalNameShort = b.getString("local_name_short");
}
/**
* Instantiates a new Bluetooth LE device Ad Record Store.
*
* @param adRecords the ad records
*/
public AdRecordStore(final SparseArray<AdRecord> adRecords){
mAdRecords = adRecords;
@@ -44,41 +51,83 @@ public class AdRecordStore implements Parcelable{
}
/* (non-Javadoc)
* @see android.os.Parcelable#describeContents()
*/
@Override
public int describeContents() {
return 0;
}
/**
* Gets the short local device name.
*
* @return the local name complete
*/
public String getLocalNameComplete() {
return mLocalNameComplete;
}
/**
* Gets the complete local device name.
*
* @return the local name short
*/
public String getLocalNameShort() {
return mLocalNameShort;
}
/**
* retrieves an individual record.
*
* @param record the record
* @return the record
*/
public AdRecord getRecord(int record){
return mAdRecords.get(record);
}
/**
* Gets the record data as string.
*
* @param record the record
* @return the record data as string
*/
public String getRecordDataAsString(int record){
return AdRecordUtils.getRecordDataAsString(
mAdRecords.get(record));
}
/**
* Gets the record as collection.
*
* @return the records as collection
*/
public Collection<AdRecord> getRecordsAsCollection() {
return Collections.unmodifiableCollection(asList(mAdRecords));
}
/**
* Checks if is record present.
*
* @param record the record
* @return true, if is record present
*/
public boolean isRecordPresent(int record){
return mAdRecords.indexOfKey(record) >= 0;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "AdRecordStore [mLocalNameComplete=" + mLocalNameComplete + ", mLocalNameShort=" + mLocalNameShort + "]";
}
/* (non-Javadoc)
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
*/
@Override
public void writeToParcel(Parcel parcel, int arg1) {
final Bundle b = new Bundle();
@@ -89,6 +138,13 @@ public class AdRecordStore implements Parcelable{
parcel.writeBundle(b);
}
/**
* As list.
*
* @param <C> the generic type
* @param sparseArray the sparse array
* @return the collection
*/
public static <C> Collection<C> asList(SparseArray<C> sparseArray) {
if (sparseArray == null) return null;
@@ -3,6 +3,8 @@ package uk.co.alt236.bluetoothlelib.util;
import java.nio.ByteBuffer;
public class ByteUtils {
/** The Constant HEXES. */
private static final String HEXES = "0123456789ABCDEF";
/**
@@ -31,20 +33,31 @@ public class ByteUtils {
return sb.toString();
}
public static byte[] invertArray(byte[] array){
final int size = array.length;
byte temp;
/**
* Checks to see if a byte arry starts with another byte array.
*
* @param array the array
* @param prefix the prefix
* @return true, if successful
*/
public static boolean doesArrayBeginWith(byte[] array, byte[] prefix){
if(array.length < prefix.length){return false;}
for (int i = 0; i < size/2; i++)
{
temp = array[i];
array[i] = array[size-1 - i];
array[size-1 - i] = temp;
}
for(int i = 0; i < prefix.length; i++){
if(array[i] != prefix[i]){
return false;
}
}
return array;
return true;
}
/**
* Converts a byte array with a length of 2 into an int
*
* @param input the input
* @return the int from the array
*/
public static int getIntFrom2ByteArray(byte[] input){
final byte[] result = new byte[4];
@@ -69,7 +82,7 @@ public class ByteUtils {
}
/**
* Converts a byte array to an integer;
* Converts a byte array to an int.
*
* @param bytes the bytes
* @return the int from byte array
@@ -78,9 +91,8 @@ public class ByteUtils {
return ByteBuffer.wrap(bytes).getInt();
}
/**
* Converts a byte array to a long;
* Converts a byte array to a long.
*
* @param bytes the bytes
* @return the long from byte array
@@ -88,4 +100,25 @@ public class ByteUtils {
public static long getLongFromByteArray(final byte[] bytes) {
return ByteBuffer.wrap(bytes).getLong();
}
/**
* Inverts an array
*
* @param array the array
* @return the byte[]
*/
public static byte[] invertArray(byte[] array){
final int size = array.length;
byte temp;
for (int i = 0; i < size/2; i++)
{
temp = array[i];
array[i] = array[size-1 - i];
array[size-1 - i] = temp;
}
return array;
}
}
@@ -5,8 +5,8 @@ import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
public class IBeaconUtils {
private static final double DISTANCE_THRESHOLD_WTF = 0.0;
private static final double DISTANCE_THRESHOLD_IMMEDIATE = 0.5;
private static final double DISTANCE_THRESHOLD_NEAR = 0.5;
private static final double DISTANCE_THRESHOLD_NEAR = 3.0;
private static final byte[] SCAN_RECORD_PREFIX_IBEACON_1 = new byte[]{0x02, 0x01, 0x1A, 0x1A, (byte) 0xFF, 0x4C, 0x00, 0x02, 0x15};
private static final byte[] SCAN_RECORD_PREFIX_IBEACON_2 = new byte[]{0x02, 0x01, 0x06, 0x1A, (byte) 0xFF, 0x4C, 0x00, 0x02, 0x15};
@@ -26,7 +26,15 @@ public class IBeaconUtils {
return IBeaconDistanceDescriptor.FAR;
}
// Code taken from: http://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing
/**
* Calculates the accuracy of an RSSI reading.
*
* The code was taken from {@link http://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing}
*
* @param txPower the calibrated TX power of an iBeacon
* @param rssi the RSSI value of the iBeacon
* @return
*/
public static double calculateAccuracy(int txPower, double rssi) {
if (rssi == 0) {
return -1.0; // if we cannot determine accuracy, return -1.
@@ -42,29 +50,28 @@ public class IBeaconUtils {
}
}
private static boolean doesArrayBeginWith(byte[] array, byte[] prefix){
if(array.length < prefix.length){return false;}
for(int i = 0; i < prefix.length; i++){
if(array[i] != prefix[i]){
return false;
}
}
return true;
}
/**
* Ascertains whether a {@link uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice} is an iBeacon;
*
* @param device a {@link uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice} device.
* @return
*/
public static boolean isThisAnIBeacon(BluetoothLeDevice device){
return isThisAnIBeacon(device.getScanRecord());
}
/**
* Ascertains whether a scanRecord bytearray belongs to an iBeacon;
*
* @param scanRecord a Bluetooth LE device's scanRecord.
* @return
*/
public static boolean isThisAnIBeacon(byte[] scanRecord){
if(doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_1)){
if(ByteUtils.doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_1)){
return true;
}
if(doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_2)){
if(ByteUtils.doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_2)){
return true;
}