Xamarin BLE插件:列表视图不显示扫描设备的信息



我正在使用Xamarin的BLE插件来扫描设备。然后,我想列出设备的 ID。但是,我的列表视图不显示 Id(仅显示空字段(。

Cs:

IBluetoothLE ble;
IAdapter adapter;
ObservableCollection<IDevice> deviceList;
IDevice device;
public BltScan()
{
InitializeComponent();
ble = CrossBluetoothLE.Current;
adapter = CrossBluetoothLE.Current.Adapter;
deviceList = new ObservableCollection<IDevice>();
bltlist.ItemsSource = deviceList;
scan();
}
public async void scan()
{
try
{
deviceList.Clear();
adapter.DeviceDiscovered += (s, a) =>
{
deviceList.Add(a.Device);
// DisplayAlert("Disc", a.Device.Id.ToString(), "OK");
};
//We have to test if the device is scanning 
if (!ble.Adapter.IsScanning)
{
await adapter.StartScanningForDevicesAsync();
}
}
catch (Exception ex)
{
DisplayAlert("Notice", ex.Message.ToString(), "Error !");
}
}

XAML:

<ListView x:Name="bltlist">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Id}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

我可以在将设备添加到设备列表时发出警报,它会正确显示扫描设备的 ID。

甚至更多:我创建了一个存储 IDevice 的包装类,它的 Id 作为字符串,但 ListView 仍然不显示 Id。我什至可以在Text="{Binding GarbageField}"中输入一个不存在的字段,它不会抱怨,而只是列出所有设备,并显示任何内容。

我关注了 https://www.youtube.com/watch?v=UMbb5FwGWSw 和 https://github.com/didourebai/BLEPluginDemo,并在YouTube评论中看到其他人也有同样的挣扎。但我找不到任何问题的答案。

万一你没有让它工作。 您还可以创建字符串的集合,并在找到新设备时从该列表中添加和删除。 当用户单击设备时,获取该列表中的 ID,并从设备列表中获取相同的 ID

最新更新