我使用C#在Windows 10中查找藍牙低Enegergy設備當我運行下面的代碼,我遇到了這樣的錯誤:C#掃描藍牙LE設備
"An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code".
錯誤的行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);
}
}
}
'DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()'返回任何設備,因此試圖訪問索引0(第一個項目)拋出一個異常 –