Refactored generic byte-related code out of AdRecordsUtil

This commit is contained in:
Alexandros Schillings
2014-03-10 19:38:35 +00:00
parent 0400f11f59
commit 103df3e66e
6 changed files with 38 additions and 34 deletions
@@ -6,6 +6,7 @@ import uk.co.alt236.btlescan.R;
import uk.co.alt236.btlescan.containers.AdRecord;
import uk.co.alt236.btlescan.containers.BluetoothLeDevice;
import uk.co.alt236.btlescan.util.AdRecordUtils;
import uk.co.alt236.btlescan.util.ByteUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
@@ -20,7 +21,7 @@ public class DetailsActivity extends Activity{
private BluetoothLeDevice mDevice;
private void append(StringBuilder sb, byte[] value){
append(sb, AdRecordUtils.byteArrayToHexString(value), null);
append(sb, ByteUtils.byteArrayToHexString(value), null);
}
private void append(StringBuilder sb, String label, String value){
@@ -5,7 +5,7 @@ import java.util.List;
import uk.co.alt236.btlescan.R;
import uk.co.alt236.btlescan.containers.BluetoothLeDevice;
import uk.co.alt236.btlescan.util.ManufacturerDataParser;
import uk.co.alt236.btlescan.util.IBeaconUtils;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
@@ -81,9 +81,7 @@ import android.widget.TextView;
viewHolder.deviceName.setText(R.string.unknown_device);
}
final boolean isIBeacon = ManufacturerDataParser.isThisAnIBeacon(device);
if (isIBeacon){
if (IBeaconUtils.isThisAnIBeacon(device)){
viewHolder.deviceIcon.setImageResource(R.drawable.ic_bluetooth_ibeacon);
} else {
viewHolder.deviceIcon.setImageResource(R.drawable.ic_bluetooth);
@@ -3,6 +3,7 @@ package uk.co.alt236.btlescan.containers;
import java.util.Arrays;
import uk.co.alt236.btlescan.util.AdRecordUtils;
import uk.co.alt236.btlescan.util.ByteUtils;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
@@ -108,7 +109,7 @@ public class BluetoothLeDevice implements Parcelable{
@Override
public String toString() {
return "BluetoothLeDevice [mDevice=" + mDevice + ", mRssi=" + mRssi + ", mScanRecord=" + AdRecordUtils.byteArrayToHexString(mScanRecord) + ", mRecordStore=" + mRecordStore + ", getBluetoothDeviceBondState()=" + getBluetoothDeviceBondState() + ", getBluetoothDeviceClassName()=" + getBluetoothDeviceClassName() + "]";
return "BluetoothLeDevice [mDevice=" + mDevice + ", mRssi=" + mRssi + ", mScanRecord=" + ByteUtils.byteArrayToHexString(mScanRecord) + ", mRecordStore=" + mRecordStore + ", getBluetoothDeviceBondState()=" + getBluetoothDeviceBondState() + ", getBluetoothDeviceClassName()=" + getBluetoothDeviceClassName() + "]";
}
@Override
@@ -12,30 +12,6 @@ import android.annotation.SuppressLint;
import android.util.SparseArray;
public class AdRecordUtils {
static final String HEXES = "0123456789ABCDEF";
public static String byteArrayToHexString(final byte[] array){
final StringBuffer sb = new StringBuffer();
boolean firstEntry = true;
sb.append('[');
for ( final byte b : array ) {
if(!firstEntry){
sb.append(", ");
}
sb.append(HEXES.charAt((b & 0xF0) >> 4));
sb.append(HEXES.charAt((b & 0x0F)));
firstEntry = false;
}
sb.append(']');
return sb.toString();
}
public static int convertByteToInt(byte bite){
return Integer.valueOf(bite & 0xFF);
}
public static String getRecordDataAsString(final AdRecord nameRecord) {
if(nameRecord == null){return new String();}
@@ -75,7 +51,7 @@ public class AdRecordUtils {
//Done once we run out of records
if (length == 0) break;
final int type = convertByteToInt(scanRecord[index]);
final int type = ByteUtils.convertByteToInt(scanRecord[index]);
//Done if our record isn't a valid type
if (type == 0) break;
@@ -101,7 +77,7 @@ public class AdRecordUtils {
//Done once we run out of records
if (length == 0) break;
final int type = convertByteToInt(scanRecord[index]);
final int type = ByteUtils.convertByteToInt(scanRecord[index]);
//Done if our record isn't a valid type
if (type == 0) break;
@@ -126,7 +102,7 @@ public class AdRecordUtils {
//Done once we run out of records
if (length == 0) break;
final int type = convertByteToInt(scanRecord[index]);
final int type = ByteUtils.convertByteToInt(scanRecord[index]);
//Done if our record isn't a valid type
if (type == 0) break;
@@ -0,0 +1,28 @@
package uk.co.alt236.btlescan.util;
public class ByteUtils {
static final String HEXES = "0123456789ABCDEF";
public static String byteArrayToHexString(final byte[] array){
final StringBuffer sb = new StringBuffer();
boolean firstEntry = true;
sb.append('[');
for ( final byte b : array ) {
if(!firstEntry){
sb.append(", ");
}
sb.append(HEXES.charAt((b & 0xF0) >> 4));
sb.append(HEXES.charAt((b & 0x0F)));
firstEntry = false;
}
sb.append(']');
return sb.toString();
}
public static int convertByteToInt(byte bite){
return Integer.valueOf(bite & 0xFF);
}
}
@@ -2,7 +2,7 @@ package uk.co.alt236.btlescan.util;
import uk.co.alt236.btlescan.containers.BluetoothLeDevice;
public class ManufacturerDataParser {
public class IBeaconUtils {
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};