We can now recognise iBeacons (need to check how correct this is)
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 824 B |
Binary file not shown.
|
After Width: | Height: | Size: 3.1 KiB |
@@ -17,52 +17,66 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical" >
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal" >
|
||||
|
||||
<TextView
|
||||
android:id="@+id/device_name"
|
||||
android:layout_width="match_parent"
|
||||
<ImageView
|
||||
android:id="@+id/device_icon"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="24sp" />
|
||||
android:paddingRight="5dp"
|
||||
android:src="@drawable/ic_bluetooth" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal" >
|
||||
android:orientation="vertical" >
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:id="@+id/device_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingRight="5dp"
|
||||
android:text="MAC:"
|
||||
android:textSize="12sp" />
|
||||
android:textSize="24sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/device_address"
|
||||
android:layout_width="wrap_content"
|
||||
<LinearLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="12sp"
|
||||
android:typeface="monospace" />
|
||||
android:orientation="horizontal" >
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text=" - "
|
||||
android:textSize="12sp" />
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingRight="5dp"
|
||||
android:text="MAC:"
|
||||
android:textSize="12sp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingRight="5dp"
|
||||
android:text="RSSI:"
|
||||
android:textSize="12sp" />
|
||||
<TextView
|
||||
android:id="@+id/device_address"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="12sp"
|
||||
android:typeface="monospace" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/device_rssi"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingRight="5dp"
|
||||
android:textSize="12sp" />
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text=" - "
|
||||
android:textSize="12sp" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingRight="5dp"
|
||||
android:text="RSSI:"
|
||||
android:textSize="12sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/device_rssi"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingRight="5dp"
|
||||
android:textSize="12sp" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
@@ -4,8 +4,8 @@ import java.util.Collection;
|
||||
|
||||
import uk.co.alt236.btlescan.R;
|
||||
import uk.co.alt236.btlescan.containers.AdRecord;
|
||||
import uk.co.alt236.btlescan.containers.AdRecordUtils;
|
||||
import uk.co.alt236.btlescan.containers.BluetoothLeDevice;
|
||||
import uk.co.alt236.btlescan.util.AdRecordUtils;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
|
||||
@@ -31,11 +31,6 @@ public class MainActivity extends ListActivity {
|
||||
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
||||
|
||||
final BluetoothLeDevice deviceLe = new BluetoothLeDevice(device, rssi, scanRecord);
|
||||
// Log.d("TAG", "~ New BT Device: " + deviceLe);
|
||||
// final Collection<AdRecord> adRecords = deviceLe.getAdRecordStore().getRecordsAsCollection();
|
||||
// for(final AdRecord record : adRecords){
|
||||
// Log.d("TAG", "~ Has Record: " + record.getType() + ": '" + record.getHumanReadableType() +"', data: '"+ AdRecordUtils.getRecordDataAsString(record) + "'");
|
||||
// }
|
||||
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
|
||||
@@ -5,17 +5,19 @@ 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 android.app.Activity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
// Adapter for holding devices found through scanning.
|
||||
public class LeDeviceListAdapter extends BaseAdapter {
|
||||
private final List<BluetoothLeDevice> mLeDevices;
|
||||
private final LayoutInflater mInflator;
|
||||
|
||||
|
||||
public LeDeviceListAdapter(Activity activity) {
|
||||
super();
|
||||
mLeDevices = new ArrayList<BluetoothLeDevice>();
|
||||
@@ -64,6 +66,7 @@ import android.widget.TextView;
|
||||
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
|
||||
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
|
||||
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
|
||||
viewHolder.deviceIcon = (ImageView) view.findViewById(R.id.device_icon);
|
||||
view.setTag(viewHolder);
|
||||
} else {
|
||||
viewHolder = (ViewHolder) view.getTag();
|
||||
@@ -71,22 +74,31 @@ import android.widget.TextView;
|
||||
|
||||
final BluetoothLeDevice device = mLeDevices.get(i);
|
||||
final String deviceName = device.getName();
|
||||
|
||||
|
||||
if (deviceName != null && deviceName.length() > 0){
|
||||
viewHolder.deviceName.setText(deviceName);
|
||||
} else{
|
||||
viewHolder.deviceName.setText(R.string.unknown_device);
|
||||
}
|
||||
|
||||
|
||||
final boolean isIBeacon = ManufacturerDataParser.isThisAnIBeacon(device);
|
||||
|
||||
if (isIBeacon){
|
||||
viewHolder.deviceIcon.setImageResource(R.drawable.ic_bluetooth_ibeacon);
|
||||
} else {
|
||||
viewHolder.deviceIcon.setImageResource(R.drawable.ic_bluetooth);
|
||||
}
|
||||
|
||||
viewHolder.deviceAddress.setText(device.getAddress());
|
||||
viewHolder.deviceRssi.setText(String.valueOf(device.getRssi()) + "db");
|
||||
return view;
|
||||
}
|
||||
|
||||
|
||||
static class ViewHolder {
|
||||
TextView deviceName;
|
||||
TextView deviceAddress;
|
||||
TextView deviceRssi;
|
||||
ImageView deviceIcon;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -32,6 +32,10 @@ public final class AdRecord implements Parcelable{
|
||||
// c5 # The 2's complement of the calibrated Tx Power
|
||||
|
||||
|
||||
private static final String PARCEL_RECORD_DATA = "record_data";
|
||||
private static final String PARCEL_RECORD_TYPE = "record_type";
|
||||
private static final String PARCEL_RECORD_LENGTH = "record_length";
|
||||
|
||||
/**
|
||||
* General FLAGS
|
||||
*
|
||||
@@ -149,9 +153,9 @@ public final class AdRecord implements Parcelable{
|
||||
|
||||
public AdRecord(Parcel in) {
|
||||
final Bundle b = in.readBundle(getClass().getClassLoader());
|
||||
mLength = b.getInt("record_length");
|
||||
mType = b.getInt("record_type");
|
||||
mData = b.getByteArray("record_data");
|
||||
mLength = b.getInt(PARCEL_RECORD_LENGTH);
|
||||
mType = b.getInt(PARCEL_RECORD_TYPE);
|
||||
mData = b.getByteArray(PARCEL_RECORD_DATA);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -184,9 +188,9 @@ public final class AdRecord implements Parcelable{
|
||||
public void writeToParcel(Parcel parcel, int arg1) {
|
||||
final Bundle b = new Bundle(getClass().getClassLoader());
|
||||
|
||||
b.putInt("record_length", mLength);
|
||||
b.putInt("record_type", mType);
|
||||
b.putByteArray("record_data", mData);
|
||||
b.putInt(PARCEL_RECORD_LENGTH, mLength);
|
||||
b.putInt(PARCEL_RECORD_TYPE, mType);
|
||||
b.putByteArray(PARCEL_RECORD_DATA, mData);
|
||||
|
||||
parcel.writeBundle(b);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import uk.co.alt236.btlescan.util.AdRecordUtils;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
@@ -2,6 +2,7 @@ package uk.co.alt236.btlescan.containers;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import uk.co.alt236.btlescan.util.AdRecordUtils;
|
||||
import android.bluetooth.BluetoothClass;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.os.Bundle;
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package uk.co.alt236.btlescan.containers;
|
||||
|
||||
public final class ManufacturerDataIBeacon {
|
||||
private final byte[] mData;
|
||||
private final int mTxPower;
|
||||
|
||||
public ManufacturerDataIBeacon(byte[] data){
|
||||
mData = data;
|
||||
mTxPower = 0;
|
||||
}
|
||||
|
||||
|
||||
// Code taken from: http://stackoverflow.com/questions/20416218/understanding-ibeacon-distancing
|
||||
protected static double calculateAccuracy(int txPower, double rssi) {
|
||||
if (rssi == 0) {
|
||||
return -1.0; // if we cannot determine accuracy, return -1.
|
||||
}
|
||||
|
||||
double ratio = rssi*1.0/txPower;
|
||||
if (ratio < 1.0) {
|
||||
return Math.pow(ratio,10);
|
||||
}
|
||||
else {
|
||||
double accuracy = (0.89976)*Math.pow(ratio,7.7095) + 0.111;
|
||||
return accuracy;
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,4 +1,4 @@
|
||||
package uk.co.alt236.btlescan.containers;
|
||||
package uk.co.alt236.btlescan.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -7,6 +7,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import uk.co.alt236.btlescan.containers.AdRecord;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.util.SparseArray;
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package uk.co.alt236.btlescan.util;
|
||||
|
||||
import uk.co.alt236.btlescan.containers.BluetoothLeDevice;
|
||||
|
||||
public class ManufacturerDataParser {
|
||||
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};
|
||||
|
||||
public static boolean isThisAnIBeacon(BluetoothLeDevice device){
|
||||
return isThisAnIBeacon(device.getScanRecord());
|
||||
}
|
||||
|
||||
public static boolean isThisAnIBeacon(byte[] scanRecord){
|
||||
if(doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_1)){
|
||||
return true;
|
||||
}
|
||||
|
||||
if(doesArrayBeginWith(scanRecord, SCAN_RECORD_PREFIX_IBEACON_2)){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user