private BluetoothRadio(IntPtr handle, BluetoothInterop.BluetoothRadioInfo radioInfo) { Contract.Requires(handle != IntPtr.Zero); this.handle = handle; Name = radioInfo.Name; Address = radioInfo.Address; Manufacturer = radioInfo.Manufacturer; }
public static BluetoothRadio[] FindAll(int maxCount = int.MaxValue) { Contract.Requires(maxCount > 0); Contract.Ensures(Contract.Result <BluetoothRadio[]>() != null); Contract.Ensures(Contract.ForAll(Contract.Result <BluetoothRadio[]>(), r => r != null && !r.IsDisposed)); var findParams = new BluetoothInterop.BluetoothFindRadioParams { Size = BluetoothInterop.BluetoothFindRadioParams.SIZE }; IntPtr hRadio; var hFind = BluetoothInterop.BluetoothFindFirstRadio(ref findParams, out hRadio); if (hFind == IntPtr.Zero) { var hr = Marshal.GetLastWin32Error(); if (hr != 0 && hr != BluetoothInterop.ERROR_NO_MORE_ITEMS) { Trace.TraceWarning("BluetoothFindFirstRadio failed with error code {0}", hr); BluetoothInterop.ThrowBluetoothException(hr); } return(new BluetoothRadio[0]); } try { var res = new List <BluetoothRadio>(); while (hRadio != IntPtr.Zero) { var radioInfo = new BluetoothInterop.BluetoothRadioInfo { Size = BluetoothInterop.BluetoothRadioInfo.SIZE }; var errCode = BluetoothInterop.BluetoothGetRadioInfo(hRadio, ref radioInfo); if (errCode != 0) { Trace.TraceWarning("BluetoothGetRadioInfo failed with error code {0}", errCode); BluetoothInterop.CloseHandle(hRadio); } else { res.Add(new BluetoothRadio(hRadio, radioInfo)); if (res.Count == maxCount) { break; } } if (!BluetoothInterop.BluetoothFindNextRadio(hFind, out hRadio)) { var hr = Marshal.GetLastWin32Error(); if (hr != 0 && hr != BluetoothInterop.ERROR_NO_MORE_ITEMS) { Trace.TraceWarning("BluetoothFindNextRadio failed with error code {0}", hr); BluetoothInterop.ThrowBluetoothException(hr); } break; } } return(res.ToArray()); } finally { if (!BluetoothInterop.BluetoothFindRadioClose(hFind)) { Trace.TraceWarning("BluetoothFindRadioClose failed with error code {0}", Marshal.GetLastWin32Error()); } } }