Split the project into a library and sample app
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package uk.co.alt236.btlescan.activities;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
|
||||
import uk.co.alt236.bluetoothlelib.device.adrecord.AdRecord;
|
||||
import uk.co.alt236.bluetoothlelib.device.mfdata.IBeaconManufacturerData;
|
||||
import uk.co.alt236.bluetoothlelib.util.AdRecordUtils;
|
||||
import uk.co.alt236.bluetoothlelib.util.ByteUtils;
|
||||
import uk.co.alt236.bluetoothlelib.util.IBeaconUtils;
|
||||
import uk.co.alt236.btlescan.R;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.TextView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
|
||||
public class DetailsActivity extends Activity{
|
||||
public static final String EXTRA_DEVICE = "extra_device";
|
||||
|
||||
@InjectView(R.id.tvDetails) TextView mTvDetails;
|
||||
|
||||
private BluetoothLeDevice mDevice;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_details);
|
||||
ButterKnife.inject(this);
|
||||
|
||||
mDevice = getIntent().getParcelableExtra(EXTRA_DEVICE);
|
||||
|
||||
pupulateDetails(mDevice);
|
||||
}
|
||||
|
||||
private void pupulateDetails(BluetoothLeDevice device) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
||||
if(device == null){
|
||||
append(sb, "Invalid Device Data!", null);
|
||||
} else {
|
||||
append(sb, "Device Name", device.getName());
|
||||
append(sb, "Device Address", device.getAddress());
|
||||
|
||||
append(sb, "", null);
|
||||
append(sb, "Device Class", device.getBluetoothDeviceClassName());
|
||||
append(sb, "Bonding State", device.getBluetoothDeviceBondState());
|
||||
|
||||
append(sb, "", null);
|
||||
append(sb, "Scan Record", null);
|
||||
append(sb, "-----------------", null);
|
||||
append(sb, device.getScanRecord());
|
||||
|
||||
append(sb, "", null);
|
||||
append(sb, "Raw Ad Records As String", null);
|
||||
append(sb, "-----------------", null);
|
||||
|
||||
|
||||
final Collection<AdRecord> adRecords = device.getAdRecordStore().getRecordsAsCollection();
|
||||
|
||||
for(final AdRecord record : adRecords){
|
||||
append(sb, "#" +record.getType() + " " + record.getHumanReadableType(), null);
|
||||
append(sb, AdRecordUtils.getRecordDataAsString(record), null);
|
||||
append(sb, "", null);
|
||||
}
|
||||
|
||||
append(sb, "Additional", null);
|
||||
append(sb, "-----------------", null);
|
||||
final boolean isIBeacon = IBeaconUtils.isThisAnIBeacon(device);
|
||||
append(sb, "Is iBeacon", isIBeacon);
|
||||
|
||||
if(isIBeacon){
|
||||
final IBeaconManufacturerData iBeaconData = new IBeaconManufacturerData(device);
|
||||
append(sb, "Company ID", iBeaconData.getCompanyIdentifier() + " (" + Integer.toHexString( iBeaconData.getCompanyIdentifier() ) + ")");
|
||||
append(sb, "iBeacon Advertisment", iBeaconData.getIBeaconAdvertisement() + " (" + Integer.toHexString( iBeaconData.getIBeaconAdvertisement() ) + ")");
|
||||
append(sb, "UUID", iBeaconData.getUUID().toString());
|
||||
append(sb, "Major", iBeaconData.getMajor() + " (" + Integer.toHexString( iBeaconData.getMajor() ) + ")");
|
||||
append(sb, "Minor", iBeaconData.getMinor() + " (" + Integer.toHexString( iBeaconData.getMinor() ) + ")");
|
||||
append(sb, "TX Power", iBeaconData.getCalibratedTxPower() + " (" + Integer.toHexString( iBeaconData.getCalibratedTxPower() ) + ")");
|
||||
}
|
||||
}
|
||||
|
||||
mTvDetails.setText(sb.toString());
|
||||
}
|
||||
|
||||
private static void append(StringBuilder sb, byte[] value){
|
||||
append(sb, ByteUtils.byteArrayToHexString(value), null);
|
||||
}
|
||||
|
||||
private static void append(StringBuilder sb, String label, boolean value) {
|
||||
append(sb, label, String.valueOf(value));
|
||||
}
|
||||
|
||||
private static void append(StringBuilder sb, String label, int value) {
|
||||
append(sb, label, String.valueOf(value));
|
||||
}
|
||||
|
||||
private static void append(StringBuilder sb, String label, long value) {
|
||||
append(sb, label, String.valueOf(value));
|
||||
}
|
||||
|
||||
private static void append(StringBuilder sb, String label, String value){
|
||||
if(value != null){
|
||||
sb.append("\u2022" + label +":\t" + value + "\n");
|
||||
} else {
|
||||
sb.append(label + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package uk.co.alt236.btlescan.activities;
|
||||
|
||||
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
|
||||
import uk.co.alt236.btlescan.R;
|
||||
import uk.co.alt236.btlescan.adapters.LeDeviceListAdapter;
|
||||
import uk.co.alt236.btlescan.util.BluetoothLeScanner;
|
||||
import uk.co.alt236.btlescan.util.BluetoothUtils;
|
||||
import android.app.ListActivity;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ListView;
|
||||
import android.widget.TextView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.InjectView;
|
||||
|
||||
public class MainActivity extends ListActivity {
|
||||
@InjectView(R.id.tvBluetoothLe) TextView mTvBluetoothLeStatus;
|
||||
@InjectView(R.id.tvBluetoothStatus) TextView mTvBluetoothStatus;
|
||||
|
||||
private BluetoothUtils mBluetoothUtils;
|
||||
private BluetoothLeScanner mScanner;
|
||||
private LeDeviceListAdapter mLeDeviceListAdapter;
|
||||
|
||||
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
|
||||
@Override
|
||||
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
||||
|
||||
final BluetoothLeDevice deviceLe = new BluetoothLeDevice(device, rssi, scanRecord);
|
||||
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mLeDeviceListAdapter.addDevice(deviceLe);
|
||||
mLeDeviceListAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onListItemClick(ListView l, View v, int position, long id) {
|
||||
final BluetoothLeDevice device = mLeDeviceListAdapter.getDevice(position);
|
||||
if (device == null) return;
|
||||
|
||||
|
||||
final Intent intent = new Intent(this, DetailsActivity.class);
|
||||
intent.putExtra(DetailsActivity.EXTRA_DEVICE, device);
|
||||
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.inject(this);
|
||||
mBluetoothUtils = new BluetoothUtils(this);
|
||||
mScanner = new BluetoothLeScanner(mLeScanCallback, mBluetoothUtils);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.main, menu);
|
||||
if (!mScanner.isScanning()) {
|
||||
menu.findItem(R.id.menu_stop).setVisible(false);
|
||||
menu.findItem(R.id.menu_scan).setVisible(true);
|
||||
menu.findItem(R.id.menu_refresh).setActionView(null);
|
||||
} else {
|
||||
menu.findItem(R.id.menu_stop).setVisible(true);
|
||||
menu.findItem(R.id.menu_scan).setVisible(false);
|
||||
menu.findItem(R.id.menu_refresh).setActionView(R.layout.actionbar_progress_indeterminate);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_scan:
|
||||
startScan();
|
||||
break;
|
||||
case R.id.menu_stop:
|
||||
mScanner.scanLeDevice(-1, false);
|
||||
invalidateOptionsMenu();
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
mScanner.scanLeDevice(-1, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume(){
|
||||
super.onResume();
|
||||
final boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
|
||||
final boolean mIsBluetoothLePresent = mBluetoothUtils.isBluetoothLeSupported();
|
||||
|
||||
if(mIsBluetoothOn){
|
||||
mTvBluetoothStatus.setText(R.string.on);
|
||||
} else {
|
||||
mTvBluetoothStatus.setText(R.string.off);
|
||||
}
|
||||
|
||||
if(mIsBluetoothLePresent){
|
||||
mTvBluetoothLeStatus.setText(R.string.supported);
|
||||
} else {
|
||||
mTvBluetoothLeStatus.setText(R.string.not_supported);
|
||||
}
|
||||
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
|
||||
private void startScan(){
|
||||
final boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
|
||||
final boolean mIsBluetoothLePresent = mBluetoothUtils.isBluetoothLeSupported();
|
||||
|
||||
mLeDeviceListAdapter = new LeDeviceListAdapter(this);
|
||||
setListAdapter(mLeDeviceListAdapter);
|
||||
|
||||
mBluetoothUtils.askUserToEnableBluetoothIfNeeded();
|
||||
if(mIsBluetoothOn && mIsBluetoothLePresent){
|
||||
mScanner.scanLeDevice(-1, true);
|
||||
invalidateOptionsMenu();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package uk.co.alt236.btlescan.adapters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
|
||||
import uk.co.alt236.bluetoothlelib.util.IBeaconUtils;
|
||||
import uk.co.alt236.btlescan.R;
|
||||
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>();
|
||||
mInflator = activity.getLayoutInflater();
|
||||
}
|
||||
|
||||
public void addDevice(BluetoothLeDevice device) {
|
||||
final int position = mLeDevices.indexOf(device);
|
||||
if(position == -1){
|
||||
mLeDevices.add(device);
|
||||
} else {
|
||||
mLeDevices.set(position, device);
|
||||
}
|
||||
}
|
||||
|
||||
public BluetoothLeDevice getDevice(int position) {
|
||||
return mLeDevices.get(position);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
mLeDevices.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return mLeDevices.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int i) {
|
||||
return mLeDevices.get(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int i, View view, ViewGroup viewGroup) {
|
||||
ViewHolder viewHolder;
|
||||
// General ListView optimization code.
|
||||
if (view == null) {
|
||||
view = mInflator.inflate(R.layout.list_item_device, null);
|
||||
viewHolder = new ViewHolder();
|
||||
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();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (IBeaconUtils.isThisAnIBeacon(device)){
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package uk.co.alt236.btlescan.util;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
public class BluetoothLeScanner {
|
||||
private final Handler mHandler;
|
||||
private final BluetoothAdapter.LeScanCallback mLeScanCallback;
|
||||
private final BluetoothUtils mBluetoothUtils;
|
||||
private boolean mScanning;
|
||||
|
||||
public BluetoothLeScanner(BluetoothAdapter.LeScanCallback leScanCallback, BluetoothUtils bluetoothUtils){
|
||||
mHandler = new Handler();
|
||||
mLeScanCallback = leScanCallback;
|
||||
mBluetoothUtils = bluetoothUtils;
|
||||
}
|
||||
|
||||
public boolean isScanning() {
|
||||
return mScanning;
|
||||
}
|
||||
|
||||
public void scanLeDevice(final int duration, final boolean enable) {
|
||||
if (enable) {
|
||||
if(mScanning){return;}
|
||||
Log.d("TAG", "~ Starting Scan");
|
||||
// Stops scanning after a pre-defined scan period.
|
||||
if(duration > 0){
|
||||
mHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d("TAG", "~ Stopping Scan (timeout)");
|
||||
mScanning = false;
|
||||
mBluetoothUtils.getBluetoothAdapter().stopLeScan(mLeScanCallback);
|
||||
}
|
||||
}, duration);
|
||||
}
|
||||
mScanning = true;
|
||||
mBluetoothUtils.getBluetoothAdapter().startLeScan(mLeScanCallback);
|
||||
} else {
|
||||
Log.d("TAG", "~ Stopping Scan");
|
||||
mScanning = false;
|
||||
mBluetoothUtils.getBluetoothAdapter().stopLeScan(mLeScanCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package uk.co.alt236.btlescan.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
public final class BluetoothUtils {
|
||||
private final Activity mActivity;
|
||||
private final BluetoothAdapter mBluetoothAdapter;
|
||||
private final BluetoothManager mBluetoothManager;
|
||||
|
||||
public final static int REQUEST_ENABLE_BT = 2001;
|
||||
|
||||
public BluetoothUtils(Activity activity){
|
||||
mActivity = activity;
|
||||
mBluetoothManager = (BluetoothManager) mActivity.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||
mBluetoothAdapter = mBluetoothManager.getAdapter();
|
||||
}
|
||||
|
||||
public void askUserToEnableBluetoothIfNeeded(){
|
||||
if (isBluetoothLeSupported() && (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled())) {
|
||||
final Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||
mActivity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
||||
}
|
||||
}
|
||||
|
||||
public BluetoothAdapter getBluetoothAdapter(){
|
||||
return mBluetoothAdapter;
|
||||
}
|
||||
|
||||
public boolean isBluetoothLeSupported(){
|
||||
return mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
|
||||
}
|
||||
|
||||
public boolean isBluetoothOn(){
|
||||
if (mBluetoothAdapter == null) {
|
||||
return false;
|
||||
} else {
|
||||
return mBluetoothAdapter.isEnabled();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user