我将设备列表添加到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
。
答案 0 :(得分:1)
我在两部手机中运行此演示,只有一部分会扫描多次。 所以我猜这与设备有关。
你可以做到这一点来避免它:
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
String deviceInfo = device.getName() + "\n" + device.getAddress();
if(mNewDevicesArrayAdapter.getPosition(deviceInfo) < 0) {
mNewDevicesArrayAdapter.add(deviceInfo);
}
}
答案 1 :(得分:0)
这是一个用于显示蓝牙设备及其信号强度的I类存储。
由于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;
}
}
方法中的列表中 - 并且仅在这种情况下更新RSSI值:
{{1}}