Added share button to share device info
This commit is contained in:
@@ -27,6 +27,7 @@ import butterknife.InjectView;
|
||||
public class MainActivity extends ListActivity {
|
||||
@InjectView(R.id.tvBluetoothLe) TextView mTvBluetoothLeStatus;
|
||||
@InjectView(R.id.tvBluetoothStatus) TextView mTvBluetoothStatus;
|
||||
@InjectView(R.id.tvItemCount) TextView mTvItemCount;
|
||||
|
||||
private BluetoothUtils mBluetoothUtils;
|
||||
private BluetoothLeScanner mScanner;
|
||||
@@ -44,13 +45,21 @@ public class MainActivity extends ListActivity {
|
||||
public void run() {
|
||||
mDeviceStore.addDevice(deviceLe);
|
||||
mLeDeviceListAdapter.replaceData(mDeviceStore.getDeviceList());
|
||||
updateItemCount(mLeDeviceListAdapter.getCount());
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
private void updateItemCount(int count){
|
||||
mTvItemCount.setText(
|
||||
getString(
|
||||
R.string.formatter_item_count,
|
||||
String.valueOf(count)));
|
||||
}
|
||||
|
||||
private void displayAboutDialog(){
|
||||
// REALLY REALLY LAZY LIKIFIED DIALOG
|
||||
// REALLY REALLY LAZY LINKIFIED DIALOG
|
||||
final int paddingSizeDp = 5;
|
||||
final float scale = getResources().getDisplayMetrics().density;
|
||||
final int dpAsPixels = (int) (paddingSizeDp * scale + 0.5f);
|
||||
@@ -83,6 +92,7 @@ public class MainActivity extends ListActivity {
|
||||
mDeviceStore = new BluetoothLeDeviceStore();
|
||||
mBluetoothUtils = new BluetoothUtils(this);
|
||||
mScanner = new BluetoothLeScanner(mLeScanCallback, mBluetoothUtils);
|
||||
updateItemCount(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,6 +107,13 @@ public class MainActivity extends ListActivity {
|
||||
menu.findItem(R.id.menu_scan).setVisible(false);
|
||||
menu.findItem(R.id.menu_refresh).setActionView(R.layout.actionbar_progress_indeterminate);
|
||||
}
|
||||
|
||||
if(getListView().getCount() > 0){
|
||||
menu.findItem(R.id.menu_share).setVisible(true);
|
||||
} else {
|
||||
menu.findItem(R.id.menu_share).setVisible(false);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -125,6 +142,8 @@ public class MainActivity extends ListActivity {
|
||||
case R.id.menu_about:
|
||||
displayAboutDialog();
|
||||
break;
|
||||
case R.id.menu_share:
|
||||
mDeviceStore.shareDataAsEmail(this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -160,6 +179,7 @@ public class MainActivity extends ListActivity {
|
||||
final boolean mIsBluetoothOn = mBluetoothUtils.isBluetoothOn();
|
||||
final boolean mIsBluetoothLePresent = mBluetoothUtils.isBluetoothLeSupported();
|
||||
mDeviceStore.clear();
|
||||
updateItemCount(0);
|
||||
|
||||
mLeDeviceListAdapter = new LeDeviceListAdapter(this, mDeviceStore.getDeviceList());
|
||||
setListAdapter(mLeDeviceListAdapter);
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package uk.co.alt236.btlescan.containers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
@@ -8,6 +11,11 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
|
||||
import uk.co.alt236.bluetoothlelib.util.ByteUtils;
|
||||
import uk.co.alt236.btlescan.util.CsvWriterHelper;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
|
||||
public class BluetoothLeDeviceStore {
|
||||
private final Map<String, BluetoothLeDevice> mDeviceMap;
|
||||
@@ -29,6 +37,26 @@ public class BluetoothLeDeviceStore {
|
||||
mDeviceMap.clear();
|
||||
}
|
||||
|
||||
|
||||
private FileWriter generateFile(File file, String contents){
|
||||
FileWriter writer = null;
|
||||
try {
|
||||
writer = new FileWriter(file);
|
||||
writer.append(contents);
|
||||
writer.flush();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return writer;
|
||||
}
|
||||
|
||||
public List<BluetoothLeDevice> getDeviceList(){
|
||||
final List<BluetoothLeDevice> methodResult = new ArrayList<BluetoothLeDevice>(mDeviceMap.values());
|
||||
|
||||
@@ -42,4 +70,55 @@ public class BluetoothLeDeviceStore {
|
||||
|
||||
return methodResult;
|
||||
}
|
||||
|
||||
|
||||
private String getListAsCsv(){
|
||||
final List<BluetoothLeDevice> list = getDeviceList();
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
sb.append(CsvWriterHelper.addStuff("mac"));
|
||||
sb.append(CsvWriterHelper.addStuff("name"));
|
||||
sb.append(CsvWriterHelper.addStuff("firstTimestamp"));
|
||||
sb.append(CsvWriterHelper.addStuff("firstRssi"));
|
||||
sb.append(CsvWriterHelper.addStuff("currentTimestamp"));
|
||||
sb.append(CsvWriterHelper.addStuff("currentRssi"));
|
||||
sb.append(CsvWriterHelper.addStuff("adRecord"));
|
||||
sb.append('\n');
|
||||
|
||||
for(BluetoothLeDevice device : list){
|
||||
sb.append(CsvWriterHelper.addStuff(device.getAddress()));
|
||||
sb.append(CsvWriterHelper.addStuff(device.getName()));
|
||||
sb.append(CsvWriterHelper.addStuff(device.getFirstTimestamp()));
|
||||
sb.append(CsvWriterHelper.addStuff(device.getFirstRssi()));
|
||||
sb.append(CsvWriterHelper.addStuff(device.getTimestamp()));
|
||||
sb.append(CsvWriterHelper.addStuff(device.getRssi()));
|
||||
sb.append(CsvWriterHelper.addStuff(ByteUtils.byteArrayToHexString(device.getScanRecord())));
|
||||
sb.append('\n');
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
public void shareDataAsEmail(Context context){
|
||||
String to = null;
|
||||
String subject = "Bluetooth LE Scan Results";
|
||||
String message = "Please find attached the scan results.";
|
||||
|
||||
final Intent i = new Intent(Intent.ACTION_SEND);
|
||||
i.setType("plain/text");
|
||||
try {
|
||||
final File outputDir = context.getCacheDir();
|
||||
final File outputFile = File.createTempFile("export_tmp", ".csv", outputDir);
|
||||
outputFile.setReadable(true, false);
|
||||
generateFile(outputFile, getListAsCsv());
|
||||
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile));
|
||||
i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
|
||||
i.putExtra(Intent.EXTRA_SUBJECT, subject);
|
||||
i.putExtra(Intent.EXTRA_TEXT, message);
|
||||
context.startActivity(Intent.createChooser(i, "Please select your email client:"));
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package uk.co.alt236.btlescan.util;
|
||||
|
||||
public class CsvWriterHelper {
|
||||
private static final String QUOTE = "\"";
|
||||
public static String addStuff(Integer text){
|
||||
return QUOTE + text + QUOTE + ",";
|
||||
}
|
||||
|
||||
public static String addStuff(Long text){
|
||||
return QUOTE + text + QUOTE + ",";
|
||||
}
|
||||
|
||||
public static String addStuff(boolean value){
|
||||
return QUOTE + value + QUOTE + ",";
|
||||
}
|
||||
|
||||
public static String addStuff(String text){
|
||||
if(text == null){text = "<blank>";}
|
||||
text = text.replace(QUOTE, "'");
|
||||
|
||||
return QUOTE + text.trim() + QUOTE + ",";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user