如何在BLE信标列表中更新设备的RSSI

时间:2017-04-20 16:32:49

标签: java android bluetooth-lowenergy rssi

我想更新每个信标的RSSI值。但是,我似乎没有让它工作。我想使用Map或HashMap来更新设备持有者的RSSI:

private Map<BluetoothDevice, Integer> mDevicesRssi;

这是我的设备持有人类:

class DeviceHolder{
    BluetoothDevice device;
    int rssi;
    public DeviceHolder(BluetoothDevice device, int rssi){
        this. device = device;
        this.rssi = rssi;
    }
}

和我的适配器类

private class LeDeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;
    private ArrayList<DeviceHolder> mLeHolders;
    private LayoutInflater mInflator;

    public LeDeviceListAdapter() {
        super();
        mLeDevices = new ArrayList<BluetoothDevice>();
        mLeHolders = new ArrayList<DeviceHolder>();
        mInflator = DeviceScanActivity.this.getLayoutInflater();
    }

    public void addDevice(DeviceHolder deviceHolder) {
       if(!mLeDevices.contains(deviceHolder.device)) {
            DeviceHolder actualDeviceHolder = new DeviceHolder
            mLeDevices.add(deviceHolder.device);
            mLeHolders.add(deviceHolder);
       }
       else {
            mDevicesRssi.put(deviceHolder.device, deviceHolder.rssi);
       }
       mLeDeviceListAdapter.notifyDataSetChanged();
    }

我想我必须在LeDeviceListAdapter类中创建一个数组,例如:

mDevicesRssi = new Map<BluetoothDevice, Integer>();

然而,这给了我Map是抽象的错误。 我的问题是:有没有人提示如何更新deviceHolder对象的rssi,以便在应用程序中显示的列表中更新RSSI?

其他一切似乎都运转良好。当我只添加没有if条件的设备时,应用会为每个广告列出一个新设备 - &gt;没有过滤器。

非常感谢

Sayus

1 个答案:

答案 0 :(得分:-1)

我能够自己找到解决方案。如果有人在使用google提供的有点过时的BluetoothLeGatt示例时遇到同样的问题,我希望这个答案可以帮助他:

import java.util.HashMap;

public class DeviceHolder{
    private BluetoothDevice device;
    private int rssi;


    public DeviceHolder(BluetoothDevice device, int rssi){
        this. device = device;
        this.rssi = rssi;
    }
    public String getAddress() {
        return device.getAddress();
    }
    public String getName() {
        return device.getName();
    }
    public void setRSSI(int rssi){
        this.rssi = rssi;
    }
    public int getRSSI() {
        return rssi;
    }
}

private class LeDeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;
    private ArrayList<DeviceHolder> mLeHolders;
    private LayoutInflater mInflator;
    private HashMap<String, DeviceHolder> mLeDevicesRssi;

    public LeDeviceListAdapter() {
        super();
        mLeDevices = new ArrayList<BluetoothDevice>();
        mLeHolders = new ArrayList<DeviceHolder>();
        mLeDevicesRssi = new HashMap<String, DeviceHolder>();
        mInflator = DeviceScanActivity.this.getLayoutInflater();
    }

    public void addDevice(DeviceHolder deviceHolder, int rssi) {
        String address = deviceHolder.device.getAddress();
       if(!mLeDevices.contains(deviceHolder.device)) {
           DeviceHolder cDeviceHolder = new DeviceHolder(deviceHolder.device, deviceHolder.rssi);
           cDeviceHolder.setRSSI(rssi);
           mLeDevicesRssi.put(address, cDeviceHolder);
           mLeDevices.add(cDeviceHolder.device);
           mLeHolders.add(cDeviceHolder);
       }
       else if(mLeDevices.contains(deviceHolder.device)) {
           mLeDevicesRssi.get(address).setRSSI(rssi);
       }
       mLeDeviceListAdapter.notifyDataSetChanged();
    }
....
}

 private BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                DeviceHolder deviceHolder = new DeviceHolder(device, rssi);
                final int new_rssi = rssi;
                runOnUiThread(new DeviceAddTask( deviceHolder, new_rssi ) );
            }
        };

class DeviceAddTask implements Runnable {
    DeviceHolder deviceHolder;

    public DeviceAddTask( DeviceHolder deviceHolder, int rssi ) {
        this.deviceHolder = deviceHolder;
        this.deviceHolder.rssi = rssi;
    }

    public void run() {
        mLeDeviceListAdapter.addDevice(deviceHolder, deviceHolder.rssi);
        mLeDeviceListAdapter.notifyDataSetChanged();
    }
}

它可能不是以连续方式显示RSSI的最有效方式。但它确实有效。