gpt4 book ai didi

android - 蓝牙两次而不是一次添加到 ListView

转载 作者:搜寻专家 更新时间:2023-11-01 08:14:33 25 4
gpt4 key购买 nike

我正在搜索范围内可用的蓝牙设备。出于某种原因,每个找到的设备都被添加到 ListView 两次,而它应该只显示一次

有人知道我在这里做错了什么吗?代码包含在下面。

When discovery finds a device

if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device;
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) {
Log.v("test", "test");

mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}

} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
// When discovery is finished, change the Activity title

setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);

if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}

最佳答案

在您的代码中,您使用了等号操作。这就是为什么它增加了两次。已配对设备列表中的一个。并且相同的项目再次添加到新发明的列表中。试试这个代码。

// When discovery finds a device
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);
}
}

关于android - 蓝牙两次而不是一次添加到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6159818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com