Starting to abstract more so we can support more beacon types

This commit is contained in:
Alexandros Schillings
2015-07-14 14:01:32 +01:00
parent cb43be74ef
commit 459eb1d2d4
11 changed files with 136 additions and 84 deletions
@@ -216,15 +216,6 @@ public class BluetoothLeDevice implements Parcelable {
return BluetoothClassResolver.resolveDeviceClass(mDevice.getBluetoothClass().getDeviceClass());
}
/**
* Gets the bluetooth device major class name.
*
* @return the bluetooth device major class name
*/
public String getBluetoothDeviceMajorClassName() {
return BluetoothClassResolver.resolveMajorDeviceClass(mDevice.getBluetoothClass().getMajorDeviceClass());
}
public Set<BluetoothService> getBluetoothDeviceKnownSupportedServices() {
if (mServiceSet == null) {
synchronized (this) {
@@ -244,6 +235,15 @@ public class BluetoothLeDevice implements Parcelable {
return mServiceSet;
}
/**
* Gets the bluetooth device major class name.
*
* @return the bluetooth device major class name
*/
public String getBluetoothDeviceMajorClassName() {
return BluetoothClassResolver.resolveMajorDeviceClass(mDevice.getBluetoothClass().getMajorDeviceClass());
}
/**
* Gets the device.
*
@@ -0,0 +1,31 @@
package uk.co.alt236.bluetoothlelib.device.beacon;
import java.util.Arrays;
/**
*
*/
public abstract class BeaconManufacturerData {
private final BeaconType mBeaconType;
private final byte[] mData;
protected BeaconManufacturerData(final BeaconType expectedType, final byte[] data){
if (BeaconUtils.getBeaconType(data) != expectedType) {
throw new IllegalArgumentException(
"Manufacturer record '"
+ Arrays.toString(data)
+ "' is not from a " + expectedType);
}
this.mData = data;
this.mBeaconType = expectedType;
}
public BeaconType getBeaconType(){
return mBeaconType;
}
public byte[] getData(){
return mData;
}
}
@@ -5,5 +5,5 @@ package uk.co.alt236.bluetoothlelib.device.beacon;
*/
public enum BeaconType {
NOT_A_BEACON,
IBEACON
IBEACON,
}
@@ -21,7 +21,7 @@ public final class BeaconUtils {
* @return the {@link BeaconType}
*/
public static BeaconType getBeaconType(final byte[] manufacturerData) {
if (manufacturerData == null) {
if (manufacturerData == null || manufacturerData.length == 0) {
return BeaconType.NOT_A_BEACON;
}
@@ -25,7 +25,6 @@ public class IBeaconDevice extends BluetoothLeDevice implements BeaconDevice{
*/
public IBeaconDevice(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
super(device, rssi, scanRecord, 0);
validate();
mIBeaconData = new IBeaconManufacturerData(this);
}
@@ -40,7 +39,6 @@ public class IBeaconDevice extends BluetoothLeDevice implements BeaconDevice{
*/
public IBeaconDevice(final BluetoothDevice device, final int rssi, final byte[] scanRecord, final long timestamp) {
super(device, rssi, scanRecord, timestamp);
validate();
mIBeaconData = new IBeaconManufacturerData(this);
}
@@ -53,13 +51,11 @@ public class IBeaconDevice extends BluetoothLeDevice implements BeaconDevice{
*/
public IBeaconDevice(final BluetoothLeDevice device) {
super(device);
validate();
mIBeaconData = new IBeaconManufacturerData(this);
}
private IBeaconDevice(final Parcel in) {
super(in);
validate();
mIBeaconData = new IBeaconManufacturerData(this);
}
@@ -143,10 +139,4 @@ public class IBeaconDevice extends BluetoothLeDevice implements BeaconDevice{
public String getUUID() {
return getIBeaconData().getUUID();
}
private void validate() {
if (BeaconUtils.getBeaconType(this) != BeaconType.IBEACON) {
throw new IllegalArgumentException("Device " + getDevice() + " is not an iBeacon.");
}
}
}
@@ -4,8 +4,8 @@ import java.util.Arrays;
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
import uk.co.alt236.bluetoothlelib.device.adrecord.AdRecord;
import uk.co.alt236.bluetoothlelib.device.beacon.BeaconManufacturerData;
import uk.co.alt236.bluetoothlelib.device.beacon.BeaconType;
import uk.co.alt236.bluetoothlelib.device.beacon.BeaconUtils;
import uk.co.alt236.bluetoothlelib.util.ByteUtils;
/**
@@ -45,8 +45,7 @@ import uk.co.alt236.bluetoothlelib.util.ByteUtils;
* @author Alexandros Schillings
*/
public final class IBeaconManufacturerData {
private final byte[] mData;
public final class IBeaconManufacturerData extends BeaconManufacturerData{
private final int mCalibratedTxPower;
private final int mCompanyIdentidier;
private final int mIBeaconAdvertisment;
@@ -71,24 +70,17 @@ public final class IBeaconManufacturerData {
* @throws IllegalArgumentException if the data is not from an iBeacon.
*/
public IBeaconManufacturerData(final byte[] manufacturerData) {
mData = manufacturerData;
super(BeaconType.IBEACON, manufacturerData);
if (BeaconUtils.getBeaconType(mData) != BeaconType.IBEACON) {
throw new IllegalArgumentException(
"Manufacturer record '"
+ Arrays.toString(manufacturerData)
+ "' is not from an iBeacon.");
}
final byte[] intArray = Arrays.copyOfRange(mData, 0, 2);
final byte[] intArray = Arrays.copyOfRange(manufacturerData, 0, 2);
ByteUtils.invertArray(intArray);
mCompanyIdentidier = ByteUtils.getIntFrom2ByteArray(intArray);
mIBeaconAdvertisment = ByteUtils.getIntFrom2ByteArray(Arrays.copyOfRange(mData, 2, 4));
mUUID = IBeaconUtils.calculateUuidString(Arrays.copyOfRange(mData, 4, 20));
mMajor = ByteUtils.getIntFrom2ByteArray(Arrays.copyOfRange(mData, 20, 22));
mMinor = ByteUtils.getIntFrom2ByteArray(Arrays.copyOfRange(mData, 22, 24));
mCalibratedTxPower = mData[24];
mIBeaconAdvertisment = ByteUtils.getIntFrom2ByteArray(Arrays.copyOfRange(manufacturerData, 2, 4));
mUUID = IBeaconUtils.calculateUuidString(Arrays.copyOfRange(manufacturerData, 4, 20));
mMajor = ByteUtils.getIntFrom2ByteArray(Arrays.copyOfRange(manufacturerData, 20, 22));
mMinor = ByteUtils.getIntFrom2ByteArray(Arrays.copyOfRange(manufacturerData, 22, 24));
mCalibratedTxPower = manufacturerData[24];
}
/**