internal BluetoothDevice(BluetoothRadio radio, BluetoothInterop.BluetoothDeviceInfo devInfo) { Contract.Requires(radio != null && !radio.IsDisposed); this.radio = radio; this.devInfo = devInfo; }
public BluetoothDevice[] FindDevices(TimeSpan timeout, bool includeAuthenticated = true, bool includeConnected = true, bool includeRemembered = true, bool includeUnknown = true, bool issueInquiry = true) { Contract.Requires(timeout >= TimeSpan.Zero); Contract.Requires(timeout <= TimeSpan.FromSeconds(48 * BluetoothInterop.TIMEOUT_MULTIPLIER_UNIT_SEC)); Contract.Requires(!IsDisposed); Contract.Ensures(Contract.Result <BluetoothDevice[]>() != null); Contract.Ensures(Contract.ForAll(Contract.Result <BluetoothDevice[]>(), d => d != null && d.Radio == this)); var prms = new BluetoothInterop.BluetoothDeviceSearchParams { Size = BluetoothInterop.BluetoothDeviceSearchParams.SIZE, hRadio = handle, ReturnAuthenticated = includeAuthenticated, ReturnConnected = includeConnected, ReturnRemembered = includeRemembered, ReturnUnknown = includeUnknown, IssueInquiry = issueInquiry, TimeoutMultiplier = Convert.ToByte(Math.Round(timeout.TotalSeconds / BluetoothInterop.TIMEOUT_MULTIPLIER_UNIT_SEC)), }; var devInfo = new BluetoothInterop.BluetoothDeviceInfo { Size = BluetoothInterop.BluetoothDeviceInfo.SIZE }; var res = new List <BluetoothDevice>(); var hFind = BluetoothInterop.BluetoothFindFirstDevice(ref prms, ref devInfo); if (hFind != IntPtr.Zero) { while (true) { res.Add(new BluetoothDevice(this, devInfo)); if (!BluetoothInterop.BluetoothFindNextDevice(hFind, ref devInfo)) { var hr = Marshal.GetLastWin32Error(); if (hr != 0 && hr != BluetoothInterop.ERROR_NO_MORE_ITEMS) { Trace.TraceWarning("BluetoothFindNextDevice failed with error code {0}", hr); BluetoothInterop.ThrowBluetoothException(hr); } break; } } if (!BluetoothInterop.BluetoothFindDeviceClose(hFind)) { var hr = Marshal.GetLastWin32Error(); Trace.TraceWarning("BluetoothFindDeviceClose failed with error code {0}", hr); } } else { var hr = Marshal.GetLastWin32Error(); if (hr != 0 && hr != BluetoothInterop.ERROR_NO_MORE_ITEMS) { Trace.TraceWarning("BluetoothFindFirstDevice failed with error code {0}", hr); BluetoothInterop.ThrowBluetoothException(hr); } } return(res.ToArray()); }