Added some more javadoc
This commit is contained in:
@@ -10,8 +10,10 @@ import android.os.Parcel;
|
|||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Class AdRecordStore.
|
||||||
|
*/
|
||||||
public class AdRecordStore implements Parcelable{
|
public class AdRecordStore implements Parcelable{
|
||||||
|
|
||||||
private final SparseArray<AdRecord> mAdRecords;
|
private final SparseArray<AdRecord> mAdRecords;
|
||||||
private final String mLocalNameComplete;
|
private final String mLocalNameComplete;
|
||||||
private final String mLocalNameShort;
|
private final String mLocalNameShort;
|
||||||
@@ -33,6 +35,11 @@ public class AdRecordStore implements Parcelable{
|
|||||||
mLocalNameShort = b.getString("local_name_short");
|
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){
|
public AdRecordStore(final SparseArray<AdRecord> adRecords){
|
||||||
mAdRecords = adRecords;
|
mAdRecords = adRecords;
|
||||||
|
|
||||||
@@ -44,41 +51,83 @@ public class AdRecordStore implements Parcelable{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.os.Parcelable#describeContents()
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int describeContents() {
|
public int describeContents() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the short local device name.
|
||||||
|
*
|
||||||
|
* @return the local name complete
|
||||||
|
*/
|
||||||
public String getLocalNameComplete() {
|
public String getLocalNameComplete() {
|
||||||
return mLocalNameComplete;
|
return mLocalNameComplete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the complete local device name.
|
||||||
|
*
|
||||||
|
* @return the local name short
|
||||||
|
*/
|
||||||
public String getLocalNameShort() {
|
public String getLocalNameShort() {
|
||||||
return mLocalNameShort;
|
return mLocalNameShort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* retrieves an individual record.
|
||||||
|
*
|
||||||
|
* @param record the record
|
||||||
|
* @return the record
|
||||||
|
*/
|
||||||
public AdRecord getRecord(int record){
|
public AdRecord getRecord(int record){
|
||||||
return mAdRecords.get(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){
|
public String getRecordDataAsString(int record){
|
||||||
return AdRecordUtils.getRecordDataAsString(
|
return AdRecordUtils.getRecordDataAsString(
|
||||||
mAdRecords.get(record));
|
mAdRecords.get(record));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the record as collection.
|
||||||
|
*
|
||||||
|
* @return the records as collection
|
||||||
|
*/
|
||||||
public Collection<AdRecord> getRecordsAsCollection() {
|
public Collection<AdRecord> getRecordsAsCollection() {
|
||||||
return Collections.unmodifiableCollection(asList(mAdRecords));
|
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){
|
public boolean isRecordPresent(int record){
|
||||||
return mAdRecords.indexOfKey(record) >= 0;
|
return mAdRecords.indexOfKey(record) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#toString()
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "AdRecordStore [mLocalNameComplete=" + mLocalNameComplete + ", mLocalNameShort=" + mLocalNameShort + "]";
|
return "AdRecordStore [mLocalNameComplete=" + mLocalNameComplete + ", mLocalNameShort=" + mLocalNameShort + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void writeToParcel(Parcel parcel, int arg1) {
|
public void writeToParcel(Parcel parcel, int arg1) {
|
||||||
final Bundle b = new Bundle();
|
final Bundle b = new Bundle();
|
||||||
@@ -89,6 +138,13 @@ public class AdRecordStore implements Parcelable{
|
|||||||
parcel.writeBundle(b);
|
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) {
|
public static <C> Collection<C> asList(SparseArray<C> sparseArray) {
|
||||||
if (sparseArray == null) return null;
|
if (sparseArray == null) return null;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package uk.co.alt236.bluetoothlelib.util;
|
|||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class ByteUtils {
|
public class ByteUtils {
|
||||||
|
|
||||||
|
/** The Constant HEXES. */
|
||||||
private static final String HEXES = "0123456789ABCDEF";
|
private static final String HEXES = "0123456789ABCDEF";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,20 +33,31 @@ public class ByteUtils {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] invertArray(byte[] array){
|
/**
|
||||||
final int size = array.length;
|
* Checks to see if a byte arry starts with another byte array.
|
||||||
byte temp;
|
*
|
||||||
|
* @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++)
|
for(int i = 0; i < prefix.length; i++){
|
||||||
{
|
if(array[i] != prefix[i]){
|
||||||
temp = array[i];
|
return false;
|
||||||
array[i] = array[size-1 - i];
|
}
|
||||||
array[size-1 - i] = temp;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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){
|
public static int getIntFrom2ByteArray(byte[] input){
|
||||||
final byte[] result = new byte[4];
|
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
|
* @param bytes the bytes
|
||||||
* @return the int from byte array
|
* @return the int from byte array
|
||||||
@@ -78,9 +91,8 @@ public class ByteUtils {
|
|||||||
return ByteBuffer.wrap(bytes).getInt();
|
return ByteBuffer.wrap(bytes).getInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a byte array to a long;
|
* Converts a byte array to a long.
|
||||||
*
|
*
|
||||||
* @param bytes the bytes
|
* @param bytes the bytes
|
||||||
* @return the long from byte array
|
* @return the long from byte array
|
||||||
@@ -88,4 +100,25 @@ public class ByteUtils {
|
|||||||
public static long getLongFromByteArray(final byte[] bytes) {
|
public static long getLongFromByteArray(final byte[] bytes) {
|
||||||
return ByteBuffer.wrap(bytes).getLong();
|
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,7 +5,7 @@ import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
|
|||||||
public class IBeaconUtils {
|
public class IBeaconUtils {
|
||||||
private static final double DISTANCE_THRESHOLD_WTF = 0.0;
|
private static final double DISTANCE_THRESHOLD_WTF = 0.0;
|
||||||
private static final double DISTANCE_THRESHOLD_IMMEDIATE = 0.5;
|
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_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};
|
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;
|
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) {
|
public static double calculateAccuracy(int txPower, double rssi) {
|
||||||
if (rssi == 0) {
|
if (rssi == 0) {
|
||||||
return -1.0; // if we cannot determine accuracy, return -1.
|
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){
|
* Ascertains whether a {@link uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice} is an iBeacon;
|
||||||
if(array.length < prefix.length){return false;}
|
*
|
||||||
|
* @param device a {@link uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice} device.
|
||||||
for(int i = 0; i < prefix.length; i++){
|
* @return
|
||||||
if(array[i] != prefix[i]){
|
*/
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isThisAnIBeacon(BluetoothLeDevice device){
|
public static boolean isThisAnIBeacon(BluetoothLeDevice device){
|
||||||
return isThisAnIBeacon(device.getScanRecord());
|
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){
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_2)){
|
if(ByteUtils.doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_2)){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user