Reputation: 9
I am developing an application where I have to connect to Bluetooth Low Energy device on Android 4.3. I tried this to display the rssi in my app but It doesn't work
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
//final LeDeviceListAdapter deviceLe = new LeDeviceListAdapter(device, rssi, scanRecord);
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device,rssi);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
my list adapter code is :
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<>();
mInflator = MainActivity.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if (!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
// notifyDataSetChanged();
}
@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.listitem_device,viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
// viewHolder.signal = (TextView) view.findViewById(R.id.device_signal);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(i);
//final String rssi_s=device.getRssi();
final String deviceName = device.getName();
/* ScanResult rssi=null;
final double rssi_s = rssi.getRssi();
String rssiString = String.valueOf(rssi_s);*/
//final String rssiString =getString(valueOf(rssi_s));
if
(deviceName != null && deviceName.length() > 0){
viewHolder.deviceName.setText(deviceName);
// viewHolder.signal.setText(rssiString);
}
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
//final LeDeviceListAdapter deviceLe = new LeDeviceListAdapter(device, rssi, scanRecord);
runOnUiThread(new Runnable() {
@Override
public void run() {
// String rssi_signal=String.valueOf(rssi);
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
//TextView signal;
}
Do you have any idea how I can display this ? do I still have to type this in my oncreate method?
mLeDeviceListAdapter=new LeDeviceListAdapter();
Upvotes: 0
Views: 1265
Reputation: 745
Your problem is (1) that the adapter itself does not have a method for tracking the RSSI value and (2) your adapter's list item view has no way to show the RSSI value and the adapter it self does not have a method for tracking the RSSI value. Here are the problems in detail:
Problem 1 The list adapter is not capable of tracking the RSSI value.
You are adding the device and RSSI value to the adapter with this call:
mLeDeviceListAdapter.addDevice(device,rssi);
BUT the adapter only has a method like this:
public void addDevice(BluetoothDevice device) {
if (!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
As you can see the methods do not match. I would propose doing the following: First create a new class that will hold both the BluetoothDevice and the RSSI value. Something like this:
private class LeScanRecord {
public final BluetoothDevice device;
public final int rssi;
public LeScanRecord(BluetoothDevice device, int rssi) {
this.device = device;
this.rssi = rssi;
}
}
Then change you adapter to accept track instances of the new class (note that this addressed Problem 2 by filling in the text field with the RSSI value):
/**
* Adapter for the scan record devices
*/
private class LeDeviceListAdapter extends ArrayAdapter<LeScanRecord >
{
/**
* View inflater
*/
private final LayoutInflater inflater;
public LeDeviceListAdapter ()
{
super(MainActivity.this, R.layout.listitem_device);
inflater = MainActivity.this.getLayoutInflater();
}
@Override
public void add(LeScanRecord object)
{
for (int i = 0; i < getCount(); i++)
{
if (getItem(i).device.equals(object.device))
{
return; // do not add duplicates
}
}
super.add(object);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device,viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
viewHolder.rssi= (TextView) view.findViewById(R.id.device_signal);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
LeScanRecord record = getItem(position);
final String deviceName = record.device.getName();
if (deviceName != null && deviceName.length() > 0){
viewHolder.deviceName.setText(deviceName);
}
else {
viewHolder.deviceName.setText(R.string.unknown_device);
}
viewHolder.deviceAddress.setText(record.device.getAddress());
viewHolder.rssi.setText(Integer.valueOf(record.rssi));
return view;
}
}
static class ViewHolder {
public TextView deviceName;
public TextView deviceAddress;
public TextView rssi;
}
The last thing to do is add instances of the scan record to the adapter. So change:
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
//final LeDeviceListAdapter deviceLe = new LeDeviceListAdapter(device, rssi, scanRecord);
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device,rssi);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
To:
@Override
public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.add(new LeScanRecord(device, rssi));
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
Upvotes: 1