我遇到export class test
{
private static t:test;
private name:string;
constructor()
{
t=this;
}
public static sample()
{
return t.name;
}
问题,每当我搜索蓝牙设备时,它会像往常一样显示可用设备列表,但在第二次点击时,它会生成重复项,因为第一次扫描的结果仍然存在,因此,每当用户点击按钮进行扫描时,如何摆脱第一次扫描结果,并在最终用户点击设备的地址或名称时连接到蓝牙设备。
MainActivity.cs
listviews
Device.cs
public class BTReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var application = context.ApplicationContext;
string action = intent.Action;
if (action == BluetoothDevice.ActionFound)
{
BluetoothDevice device = intent.GetParcelableExtra(BluetoothDevice.ExtraDevice) as BluetoothDevice;
devices.Add(new Devices(device.Name, device.Address));
Devices item = list.First(d => d.name == device.Name);
//if devices does not exist in the list, add it in
if (item != null)
{
return;
}
aadapter = new CustomAdapter(_instance, devices);
availableList.Adapter = aadapter;
aadapter.NotifyDataSetChanged();
Toast toast = Toast.MakeText(Android.App.Application.Context, device.Name, ToastLength.Long);
toast.Show();
}
}
}
BTDevice.cs 又称我创建的自定义适配器
public class Devices
{
string _name;
string _address;
public string Name { get { return _name; } set { _name = value; } }
public string Address { get { return _address; } set { _address = value; } }
public Devices(string name, string address)
{
Name = name;
Address = address;
}
}
答案 0 :(得分:2)
您应该在向其添加新内容之前清除设备。 像...这样的东西。
clear(devices)
devices.add(newdevice)