Added details activity
This commit is contained in:
+5
-1
@@ -21,7 +21,7 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:theme="@style/AppTheme" >
|
android:theme="@style/AppTheme" >
|
||||||
<activity
|
<activity
|
||||||
android:name="uk.co.alt236.btlescan.MainActivity"
|
android:name="uk.co.alt236.btlescan.activities.MainActivity"
|
||||||
android:label="@string/app_name" >
|
android:label="@string/app_name" >
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
@@ -29,6 +29,10 @@
|
|||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
<activity
|
||||||
|
android:name="uk.co.alt236.btlescan.activities.DetailsActivity"
|
||||||
|
android:label="@string/app_name" >
|
||||||
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||||
|
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingTop="@dimen/activity_vertical_margin" >
|
||||||
|
|
||||||
|
<ScrollView
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content" >
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvDetails"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:typeface="monospace" />
|
||||||
|
</ScrollView>
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package uk.co.alt236.btlescan.activities;
|
||||||
|
|
||||||
|
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 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, "Ad Records", null);
|
||||||
|
append(sb, "-----------------", null);
|
||||||
|
|
||||||
|
|
||||||
|
final Collection<AdRecord> adRecords = device.getAdRecordStore().getRecordsAsCollection();
|
||||||
|
|
||||||
|
for(AdRecord record : adRecords){
|
||||||
|
append(sb,
|
||||||
|
"Record Id'" +record.getType()+ "' - " + record.getHumanReadableType(), AdRecordUtils.getRecordDataAsString(record));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mTvDetails.setText(sb.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void append(StringBuilder sb, String label, boolean value){
|
||||||
|
append(sb, label, String.valueOf(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void append(StringBuilder sb, String label, long value){
|
||||||
|
append(sb, label, String.valueOf(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void append(StringBuilder sb, String label, String value){
|
||||||
|
if(value != null){
|
||||||
|
sb.append("#" + label +":\t" + value + "\n");
|
||||||
|
} else {
|
||||||
|
sb.append(label + "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+65
-56
@@ -1,20 +1,19 @@
|
|||||||
package uk.co.alt236.btlescan;
|
package uk.co.alt236.btlescan.activities;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
|
import uk.co.alt236.btlescan.R;
|
||||||
import uk.co.alt236.btlescan.adapters.LeDeviceListAdapter;
|
import uk.co.alt236.btlescan.adapters.LeDeviceListAdapter;
|
||||||
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.BluetoothLeScanner;
|
import uk.co.alt236.btlescan.util.BluetoothLeScanner;
|
||||||
import uk.co.alt236.btlescan.util.BluetoothUtils;
|
import uk.co.alt236.btlescan.util.BluetoothUtils;
|
||||||
import android.app.ListActivity;
|
import android.app.ListActivity;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ListView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
import butterknife.InjectView;
|
import butterknife.InjectView;
|
||||||
@@ -32,13 +31,11 @@ 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);
|
// Log.d("TAG", "~ New BT Device: " + deviceLe);
|
||||||
|
// final Collection<AdRecord> adRecords = deviceLe.getAdRecordStore().getRecordsAsCollection();
|
||||||
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) + "'");
|
||||||
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
|
||||||
@@ -51,7 +48,19 @@ public class MainActivity extends ListActivity {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@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) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
@@ -60,44 +69,44 @@ public class MainActivity extends ListActivity {
|
|||||||
mScanner = new BluetoothLeScanner(mLeScanCallback, mBluetoothUtils);
|
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);
|
|
||||||
mLeDeviceListAdapter.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@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);
|
||||||
|
mLeDeviceListAdapter.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onResume(){
|
public void onResume(){
|
||||||
super.onResume();
|
super.onResume();
|
||||||
final boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
|
final boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
|
||||||
@@ -114,14 +123,14 @@ public class MainActivity extends ListActivity {
|
|||||||
} else {
|
} else {
|
||||||
mTvBluetoothLeStatus.setText(R.string.not_supported);
|
mTvBluetoothLeStatus.setText(R.string.not_supported);
|
||||||
}
|
}
|
||||||
|
|
||||||
invalidateOptionsMenu();
|
invalidateOptionsMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startScan(){
|
private void startScan(){
|
||||||
final boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
|
final boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
|
||||||
final boolean mIsBluetoothLePresent = mBluetoothUtils.isBluetoothLeSupported();
|
final boolean mIsBluetoothLePresent = mBluetoothUtils.isBluetoothLeSupported();
|
||||||
|
|
||||||
mLeDeviceListAdapter = new LeDeviceListAdapter(this);
|
mLeDeviceListAdapter = new LeDeviceListAdapter(this);
|
||||||
setListAdapter(mLeDeviceListAdapter);
|
setListAdapter(mLeDeviceListAdapter);
|
||||||
|
|
||||||
@@ -129,7 +138,7 @@ public class MainActivity extends ListActivity {
|
|||||||
if(mIsBluetoothOn && mIsBluetoothLePresent){
|
if(mIsBluetoothOn && mIsBluetoothLePresent){
|
||||||
mScanner.scanLeDevice(-1, true);
|
mScanner.scanLeDevice(-1, true);
|
||||||
invalidateOptionsMenu();
|
invalidateOptionsMenu();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user