Made the containers parcelable
This commit is contained in:
1 parent
cc18c86b80
commit
827e3ba19c
4 files changed
+162
-20
No files matched your search
@@ -1,12 +1,17 @@
|
||||
package uk.co.alt236.btlescan.containers;
|
||||
import java.util.Arrays;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
/**
|
||||
* Created by Dave Smith
|
||||
* Double Encore, Inc.
|
||||
* AdRecord
|
||||
*
|
||||
* Expanded by Alexandros Schillings
|
||||
*/
|
||||
public final class AdRecord {
|
||||
public final class AdRecord implements Parcelable{
|
||||
|
||||
// 02 # Number of bytes that follow in first AD structure
|
||||
// 01 # Flags AD type
|
||||
@@ -125,12 +130,35 @@ public final class AdRecord {
|
||||
private final int mType;
|
||||
private final byte[] mData;
|
||||
|
||||
|
||||
public static final Parcelable.Creator<AdRecord> CREATOR = new Parcelable.Creator<AdRecord>() {
|
||||
public AdRecord createFromParcel(Parcel in) {
|
||||
return new AdRecord(in);
|
||||
}
|
||||
|
||||
public AdRecord[] newArray(int size) {
|
||||
return new AdRecord[size];
|
||||
}
|
||||
};
|
||||
|
||||
public AdRecord(int length, int type, byte[] data) {
|
||||
mLength = length;
|
||||
mType = type;
|
||||
mData = data;
|
||||
}
|
||||
|
||||
public AdRecord(Parcel in) {
|
||||
final Bundle b = in.readBundle();
|
||||
mLength = b.getInt("record_length");
|
||||
mType = b.getInt("record_type");
|
||||
mData = b.getByteArray("record_data");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public byte[] getData(){
|
||||
return mData;
|
||||
}
|
||||
@@ -152,6 +180,17 @@ public final class AdRecord {
|
||||
return "AdRecord [mLength=" + mLength + ", mType=" + mType + ", mData=" + Arrays.toString(mData) + ", getHumanReadableType()=" + getHumanReadableType() + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int arg1) {
|
||||
final Bundle b = new Bundle();
|
||||
|
||||
b.putInt("record_length", mLength);
|
||||
b.putInt("record_type", mType);
|
||||
b.putByteArray("record_data", mData);
|
||||
|
||||
parcel.writeBundle(b);
|
||||
}
|
||||
|
||||
private static String getHumanReadableAdType(int type){
|
||||
switch(type){
|
||||
case TYPE_CONNECTION_INTERVAL_RANGE:
|
||||
|
||||
@@ -1,19 +1,39 @@
|
||||
package uk.co.alt236.btlescan.containers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
public class AdRecordStore {
|
||||
private final Map<Integer, AdRecord> mAdRecords;
|
||||
private final int mServiceDataUUId;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.util.SparseArray;
|
||||
|
||||
public class AdRecordStore implements Parcelable{
|
||||
|
||||
private final SparseArray<AdRecord> mAdRecords;
|
||||
private final String mLocalNameComplete;
|
||||
private final String mLocalNameShort;
|
||||
|
||||
public AdRecordStore(Map<Integer, AdRecord> adRecords){
|
||||
public static final Parcelable.Creator<AdRecordStore> CREATOR = new Parcelable.Creator<AdRecordStore>() {
|
||||
public AdRecordStore createFromParcel(Parcel in) {
|
||||
return new AdRecordStore(in);
|
||||
}
|
||||
|
||||
public AdRecordStore[] newArray(int size) {
|
||||
return new AdRecordStore[size];
|
||||
}
|
||||
};
|
||||
|
||||
public AdRecordStore(Parcel in) {
|
||||
final Bundle b = in.readBundle();
|
||||
mAdRecords = b.getSparseParcelableArray("records_array");
|
||||
mLocalNameComplete = b.getString("local_name_complete");
|
||||
mLocalNameShort = b.getString("local_name_short");
|
||||
}
|
||||
|
||||
public AdRecordStore(final SparseArray<AdRecord> adRecords){
|
||||
mAdRecords = adRecords;
|
||||
mServiceDataUUId = AdRecordUtils.getServiceDataUuid(
|
||||
mAdRecords.get(AdRecord.TYPE_SERVICE_DATA));
|
||||
|
||||
mLocalNameComplete = AdRecordUtils.getRecordDataAsString(
|
||||
mAdRecords.get(AdRecord.TYPE_LOCAL_NAME_COMPLETE));
|
||||
@@ -23,6 +43,11 @@ public class AdRecordStore {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String getLocalNameComplete() {
|
||||
return mLocalNameComplete;
|
||||
}
|
||||
@@ -40,20 +65,37 @@ public class AdRecordStore {
|
||||
mAdRecords.get(record));
|
||||
}
|
||||
|
||||
public int getServiceDataUUID(){
|
||||
return mServiceDataUUId;
|
||||
public Collection<AdRecord> getRecordsAsCollection() {
|
||||
return Collections.unmodifiableCollection(asList(mAdRecords));
|
||||
}
|
||||
|
||||
public boolean isRecordPresent(int record){
|
||||
return mAdRecords.containsKey(record);
|
||||
return mAdRecords.indexOfKey(record) >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AdRecordStore [mServiceDataUUId=" + mServiceDataUUId + ", mLocalNameComplete=" + mLocalNameComplete + ", mLocalNameShort=" + mLocalNameShort + "]";
|
||||
return "AdRecordStore [mLocalNameComplete=" + mLocalNameComplete + ", mLocalNameShort=" + mLocalNameShort + "]";
|
||||
}
|
||||
|
||||
public Collection<AdRecord> getRecordsAsCollection() {
|
||||
return Collections.unmodifiableCollection(mAdRecords.values());
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int arg1) {
|
||||
final Bundle b = new Bundle();
|
||||
b.putString("local_name_complete", mLocalNameComplete);
|
||||
b.putString("local_name_short", mLocalNameShort);
|
||||
b.putSparseParcelableArray("records_array", mAdRecords);
|
||||
|
||||
parcel.writeBundle(b);
|
||||
}
|
||||
|
||||
public static <C> Collection<C> asList(SparseArray<C> sparseArray) {
|
||||
if (sparseArray == null) return null;
|
||||
|
||||
final Collection<C> arrayList = new ArrayList<C>(sparseArray.size());
|
||||
for (int i = 0; i < sparseArray.size(); i++){
|
||||
arrayList.add(sparseArray.valueAt(i));
|
||||
}
|
||||
|
||||
return arrayList;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.util.SparseArray;
|
||||
|
||||
public class AdRecordUtils {
|
||||
/* Helper functions to parse out common data payloads from an AD structure */
|
||||
@@ -108,4 +109,29 @@ public class AdRecordUtils {
|
||||
|
||||
return Collections.unmodifiableMap(records);
|
||||
}
|
||||
|
||||
|
||||
public static SparseArray<AdRecord> parseScanRecordAsSparseArray(byte[] scanRecord) {
|
||||
final SparseArray<AdRecord> records = new SparseArray<AdRecord>();
|
||||
|
||||
int index = 0;
|
||||
while (index < scanRecord.length) {
|
||||
final int length = scanRecord[index++];
|
||||
//Done once we run out of records
|
||||
if (length == 0) break;
|
||||
|
||||
int type = scanRecord[index];
|
||||
//Done if our record isn't a valid type
|
||||
if (type == 0) break;
|
||||
|
||||
final byte[] data = Arrays.copyOfRange(scanRecord, index+1, index+length);
|
||||
|
||||
records.put(type, new AdRecord(length, type, data));
|
||||
|
||||
//Advance
|
||||
index += length;
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
}
|
||||
@@ -4,18 +4,44 @@ import java.util.Arrays;
|
||||
|
||||
import android.bluetooth.BluetoothClass;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
public class BluetoothLeDevice {
|
||||
private final BluetoothDevice mDevice;
|
||||
private transient final int mRssi;
|
||||
private final byte[] mScanRecord;
|
||||
public class BluetoothLeDevice implements Parcelable{
|
||||
private final AdRecordStore mRecordStore;
|
||||
private final BluetoothDevice mDevice;
|
||||
private final byte[] mScanRecord;
|
||||
private transient final int mRssi;
|
||||
|
||||
public static final Parcelable.Creator<BluetoothLeDevice> CREATOR = new Parcelable.Creator<BluetoothLeDevice>() {
|
||||
public BluetoothLeDevice createFromParcel(Parcel in) {
|
||||
return new BluetoothLeDevice(in);
|
||||
}
|
||||
|
||||
public BluetoothLeDevice[] newArray(int size) {
|
||||
return new BluetoothLeDevice[size];
|
||||
}
|
||||
};
|
||||
|
||||
public BluetoothLeDevice(BluetoothDevice device, int rssi, byte[] scanRecord){
|
||||
mDevice = device;
|
||||
mRssi = rssi;
|
||||
mScanRecord = scanRecord;
|
||||
mRecordStore = new AdRecordStore(AdRecordUtils.parseScanRecordAsMap(scanRecord));
|
||||
mRecordStore = new AdRecordStore(AdRecordUtils.parseScanRecordAsSparseArray(scanRecord));
|
||||
}
|
||||
|
||||
private BluetoothLeDevice(Parcel in) {
|
||||
final Bundle b = in.readBundle();
|
||||
mDevice = b.getParcelable("bluetooth_device");
|
||||
mRecordStore = b.getParcelable("device_scanrecord_store");
|
||||
mRssi = b.getInt("device_rssi");
|
||||
mScanRecord = b.getByteArray("device_scanrecord");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,7 +109,16 @@ public class BluetoothLeDevice {
|
||||
return "BluetoothLeDevice [mDevice=" + mDevice + ", mRssi=" + mRssi + ", mScanRecord=" + AdRecordUtils.byteArrayToHexString(mScanRecord) + ", mRecordStore=" + mRecordStore + ", getBluetoothDeviceBondState()=" + getBluetoothDeviceBondState() + ", getBluetoothDeviceClassName()=" + getBluetoothDeviceClassName() + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int arg1) {
|
||||
final Bundle b = new Bundle();
|
||||
b.putByteArray("device_scanrecord", mScanRecord);
|
||||
b.putInt("device_rssi", mRssi);
|
||||
b.putParcelable("bluetooth_device", mDevice);
|
||||
b.putParcelable("device_scanrecord_store", mRecordStore);
|
||||
|
||||
parcel.writeBundle(b);
|
||||
}
|
||||
|
||||
private static String resolveBluetoothClass(int btClass){
|
||||
switch (btClass){
|
||||
|
||||
Reference in new issue
Block a user