蓝牙低功耗Rssi Android

时间:2016-03-01 13:33:00

标签: android bluetooth-lowenergy

我正在开发一个应用程序,我必须连接到Android 4.3上的蓝牙低功耗设备。 我试过这个在我的应用程序中显示rssi但它不起作用

@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();

                    }
                });

我的列表适配器代码是:

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;
}

你知道我怎么能显示这个吗? 我还需要在我的oncreate方法中输入吗?

mLeDeviceListAdapter=new LeDeviceListAdapter();

1 个答案:

答案 0 :(得分:1)

你的问题是(1)适配器本身没有跟踪RSSI值的方法和(2)你的适配器的列表项视图无法显示RSSI值和它自己的适配器没有跟踪RSSI值的方法。以下是详细问题:

问题1 列表适配器无法跟踪RSSI值。

您正在使用此调用将设备和RSSI值添加到适配器:

mLeDeviceListAdapter.addDevice(device,rssi);

但是适配器只有这样的方法:

public void addDevice(BluetoothDevice device) {
    if (!mLeDevices.contains(device)) {
        mLeDevices.add(device);

    }
}

正如您所看到的方法不匹配。我建议做以下事情: 首先创建一个包含BluetoothDevice和RSSI值的新类。像这样:

private class LeScanRecord {
    public final BluetoothDevice device;
    public final int rssi;

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

然后更改适配器以接受新类的跟踪实例(请注意,这通过填写带有RSSI值的文本字段来解决问题2 ):

/**
 * 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;
}

最后要做的是将扫描记录的实例添加到适配器。所以改变:

@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();

        }
    });
}

要:

@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();

        }
    });
}