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"
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical" >
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal" >
|
||||||
|
|
||||||
<TextView
|
<ImageView
|
||||||
android:id="@+id/device_name"
|
android:id="@+id/device_icon"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:textSize="24sp" />
|
android:paddingRight="5dp"
|
||||||
|
android:src="@drawable/ic_bluetooth" />
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal" >
|
android:orientation="vertical" >
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:id="@+id/device_name"
|
||||||
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:paddingRight="5dp"
|
android:textSize="24sp" />
|
||||||
android:text="MAC:"
|
|
||||||
android:textSize="12sp" />
|
|
||||||
|
|
||||||
<TextView
|
<LinearLayout
|
||||||
android:id="@+id/device_address"
|
android:layout_width="fill_parent"
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:textSize="12sp"
|
android:orientation="horizontal" >
|
||||||
android:typeface="monospace" />
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text=" - "
|
android:paddingRight="5dp"
|
||||||
android:textSize="12sp" />
|
android:text="MAC:"
|
||||||
|
android:textSize="12sp" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:id="@+id/device_address"
|
||||||
android:layout_height="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:paddingRight="5dp"
|
android:layout_height="wrap_content"
|
||||||
android:text="RSSI:"
|
android:textSize="12sp"
|
||||||
android:textSize="12sp" />
|
android:typeface="monospace" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/device_rssi"
|
android:layout_width="wrap_content"
|
||||||
android:layout_width="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:text=" - "
|
||||||
android:paddingRight="5dp"
|
android:textSize="12sp" />
|
||||||
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>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -4,8 +4,8 @@ import java.util.Collection;
|
|||||||
|
|
||||||
import uk.co.alt236.btlescan.R;
|
import uk.co.alt236.btlescan.R;
|
||||||
import uk.co.alt236.btlescan.containers.AdRecord;
|
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.containers.BluetoothLeDevice;
|
||||||
|
import uk.co.alt236.btlescan.util.AdRecordUtils;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|||||||
@@ -31,11 +31,6 @@ public class MainActivity extends ListActivity {
|
|||||||
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
||||||
|
|
||||||
final BluetoothLeDevice deviceLe = new BluetoothLeDevice(device, rssi, 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() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import java.util.List;
|
|||||||
|
|
||||||
import uk.co.alt236.btlescan.R;
|
import uk.co.alt236.btlescan.R;
|
||||||
import uk.co.alt236.btlescan.containers.BluetoothLeDevice;
|
import uk.co.alt236.btlescan.containers.BluetoothLeDevice;
|
||||||
|
import uk.co.alt236.btlescan.util.ManufacturerDataParser;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.BaseAdapter;
|
import android.widget.BaseAdapter;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
// Adapter for holding devices found through scanning.
|
// Adapter for holding devices found through scanning.
|
||||||
public class LeDeviceListAdapter extends BaseAdapter {
|
public class LeDeviceListAdapter extends BaseAdapter {
|
||||||
@@ -64,6 +66,7 @@ import android.widget.TextView;
|
|||||||
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
|
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
|
||||||
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
|
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
|
||||||
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
|
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
|
||||||
|
viewHolder.deviceIcon = (ImageView) view.findViewById(R.id.device_icon);
|
||||||
view.setTag(viewHolder);
|
view.setTag(viewHolder);
|
||||||
} else {
|
} else {
|
||||||
viewHolder = (ViewHolder) view.getTag();
|
viewHolder = (ViewHolder) view.getTag();
|
||||||
@@ -78,6 +81,14 @@ import android.widget.TextView;
|
|||||||
viewHolder.deviceName.setText(R.string.unknown_device);
|
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.deviceAddress.setText(device.getAddress());
|
||||||
viewHolder.deviceRssi.setText(String.valueOf(device.getRssi()) + "db");
|
viewHolder.deviceRssi.setText(String.valueOf(device.getRssi()) + "db");
|
||||||
return view;
|
return view;
|
||||||
@@ -87,6 +98,7 @@ import android.widget.TextView;
|
|||||||
TextView deviceName;
|
TextView deviceName;
|
||||||
TextView deviceAddress;
|
TextView deviceAddress;
|
||||||
TextView deviceRssi;
|
TextView deviceRssi;
|
||||||
|
ImageView deviceIcon;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -32,6 +32,10 @@ public final class AdRecord implements Parcelable{
|
|||||||
// c5 # The 2's complement of the calibrated Tx Power
|
// 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
|
* General FLAGS
|
||||||
*
|
*
|
||||||
@@ -149,9 +153,9 @@ public final class AdRecord implements Parcelable{
|
|||||||
|
|
||||||
public AdRecord(Parcel in) {
|
public AdRecord(Parcel in) {
|
||||||
final Bundle b = in.readBundle(getClass().getClassLoader());
|
final Bundle b = in.readBundle(getClass().getClassLoader());
|
||||||
mLength = b.getInt("record_length");
|
mLength = b.getInt(PARCEL_RECORD_LENGTH);
|
||||||
mType = b.getInt("record_type");
|
mType = b.getInt(PARCEL_RECORD_TYPE);
|
||||||
mData = b.getByteArray("record_data");
|
mData = b.getByteArray(PARCEL_RECORD_DATA);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -184,9 +188,9 @@ public final class AdRecord implements Parcelable{
|
|||||||
public void writeToParcel(Parcel parcel, int arg1) {
|
public void writeToParcel(Parcel parcel, int arg1) {
|
||||||
final Bundle b = new Bundle(getClass().getClassLoader());
|
final Bundle b = new Bundle(getClass().getClassLoader());
|
||||||
|
|
||||||
b.putInt("record_length", mLength);
|
b.putInt(PARCEL_RECORD_LENGTH, mLength);
|
||||||
b.putInt("record_type", mType);
|
b.putInt(PARCEL_RECORD_TYPE, mType);
|
||||||
b.putByteArray("record_data", mData);
|
b.putByteArray(PARCEL_RECORD_DATA, mData);
|
||||||
|
|
||||||
parcel.writeBundle(b);
|
parcel.writeBundle(b);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import uk.co.alt236.btlescan.util.AdRecordUtils;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package uk.co.alt236.btlescan.containers;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import uk.co.alt236.btlescan.util.AdRecordUtils;
|
||||||
import android.bluetooth.BluetoothClass;
|
import android.bluetooth.BluetoothClass;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.os.Bundle;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -7,6 +7,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import uk.co.alt236.btlescan.containers.AdRecord;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.util.SparseArray;
|
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