I use C# to find Bluetooth Low Enegergy devices on Windows 10. When I run the following codes, I met such an error:
"An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code".
The line of the error is Debug.WriteLine("Found device: " + devices[0].Id);
I do not know why it is out of range. Thanks!
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);
}
}
}
Your error is on this line:
Debug.WriteLine("Found device: " + devices[0].Id);
If you debug you're code, you'll see that devices
has a length of 0 and you're trying to access the property id
of the first one (which doesn't exist).
You may want to consider using a foreach loop to see what is returned like so:
foreach(var device in devices){
Debug.WriteLine("Found device: " + device.Id);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.