[英]C# scan bluetooth LE devices
我使用C#在Windows 10上查找Bluetooth Low Enegergy设备。当我运行以下代码时,遇到了这样的错误:
“ mscorlib.dll中发生了'System.ArgumentOutOfRangeException类型的异常,但未在用户代码中处理”。
错误的行是Debug.WriteLine("Found device: " + devices[0].Id);
我不知道为什么它超出范围。 谢谢!
namespace BluetoothLE
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void LookForPairedDevices()
{
// Get BLE devices paired with Windows
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector());
Debug.WriteLine("Found device: " + devices[0].Id);
}
}
}
您的错误在此行上:
Debug.WriteLine("Found device: " + devices[0].Id);
如果您调试代码,则会看到devices
的长度为0,并且您正在尝试访问第一个设备的属性id
(不存在)。
您可能要考虑使用foreach循环来查看返回的内容,如下所示:
foreach(var device in devices){
Debug.WriteLine("Found device: " + device.Id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.