1

我将设备列表添加到 ArrayAdapter 并在 ListView 中显示,其中设备列表是从蓝牙扫描中获取的,扫描的设备首先添加到 arrayadapter。然后稍后我将其添加到列表视图中,以向用户显示扫描的蓝牙设备列表。

但是当我正在扫描设备时,正在添加重复的设备,假设扫描了一个设备,A则意味着再次扫描其显示设备的两到三倍A。我只想显示一次扫描设备的列表。如何实现它。抱歉,如果问题含糊不清。

以下代码用于查询新设备并将其添加到 arrayadapter:

if (BluetoothDevice.ACTION_FOUND.equals(action))
        {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed already
            if (device.getBondState() != BluetoothDevice.BOND_BONDED)
            {
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        // When discovery is finished, change the Activity title
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.select_device);
            if (mNewDevicesArrayAdapter.getCount() == 0) {
                String noDevices = getResources().getText(R.string.none_found).toString();
                mNewDevicesArrayAdapter.add(noDevices);
            }
        }

它是 4.1 版 Android 示例中的一个程序,示例名称为Bluetooth Chat. 活动在哪里DeviceListActivityScan.java

4

2 回答 2

1

我在我的两部手机上运行这个演示,只有一部会扫描多次。所以我想这与设备有关。

你可以这样做来避免它:

            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                String deviceInfo = device.getName() + "\n" + device.getAddress();
                if(mNewDevicesArrayAdapter.getPosition(deviceInfo) < 0) {
                    mNewDevicesArrayAdapter.add(deviceInfo);
                }
            }
于 2012-10-19T09:54:50.520 回答
0

这是我存储的一个类,用于显示蓝牙设备及其信号强度。

由于 RSSI 值更新频繁,它会检查蓝牙设备是否已在addDevice方法中的列表中存在 - 并且仅在这种情况下更新 RSSI 值:

public class DeviceListAdapter extends ArrayAdapter<String> {
    private static class ViewHolder {
        ImageView signalImage;
        TextView deviceName;
        TextView deviceAddress;
    }

    private LayoutInflater mInflator;
    // for each BLE device address store the RSSI value and name
    private Map<String, Integer> mRssi = new HashMap<String, Integer>();
    private Map<String, String> mName = new HashMap<String, String>();

    public DeviceListAdapter(Context context) {
        super(context, R.layout.rowlayout);
        mInflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addDevice(String address, String name, int rssi) {
        if (mRssi.put(address, rssi) == null) {
            // add a new BLE device to the List
            add(address);
            mName.put(address, name);
        } else {
            // just refresh the List after updating RSSI value
            notifyDataSetChanged();
        }
    }

    // return bitmap resource id for a signal strength
    private int rssi2res(int rssi) {
        if (rssi < -100)
            return R.drawable.signal1;

        if (rssi < -75)
            return R.drawable.signal2;

        if (rssi < -50)
            return R.drawable.signal3;

        return R.drawable.signal4;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.rowlayout, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.signalImage = (ImageView) view.findViewById(R.id.signalImage);
            viewHolder.deviceAddress = (TextView) view.findViewById(R.id.deviceAddress);
            viewHolder.deviceName = (TextView) view.findViewById(R.id.deviceName);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        String address = getItem(i);

        int rssi = mRssi.get(address);
        viewHolder.signalImage.setImageResource(rssi2res(rssi));

        String name = mName.get(address);
        viewHolder.deviceName.setText(name);

        viewHolder.deviceAddress.setText(address);
        return view;
    }
}
于 2015-11-12T09:25:44.327 回答