BetterScanner.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4. namespace BluetoothLENet
  5. {
  6. class BetterScanner
  7. {
  8. /// <summary>
  9. /// The BLUETOOTH_FIND_RADIO_PARAMS structure facilitates enumerating installed Bluetooth radios.
  10. /// </summary>
  11. [StructLayout(LayoutKind.Sequential)]
  12. private struct BLUETOOTH_FIND_RADIO_PARAM
  13. {
  14. internal UInt32 dwSize;
  15. internal void Initialize()
  16. {
  17. this.dwSize = (UInt32)Marshal.SizeOf(typeof(BLUETOOTH_FIND_RADIO_PARAM));
  18. }
  19. }
  20. /// <summary>
  21. /// Closes an open object handle.
  22. /// </summary>
  23. /// <param name="handle">[In] A valid handle to an open object.</param>
  24. /// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
  25. [DllImport("Kernel32.dll", SetLastError = true)]
  26. static extern bool CloseHandle(IntPtr handle);
  27. /// <summary>
  28. /// Finds the first bluetooth radio present in device manager
  29. /// </summary>
  30. /// <param name="pbtfrp">Pointer to a BLUETOOTH_FIND_RADIO_PARAMS structure</param>
  31. /// <param name="phRadio">Pointer to where the first enumerated radio handle will be returned. When no longer needed, this handle must be closed via CloseHandle.</param>
  32. /// <returns>In addition to the handle indicated by phRadio, calling this function will also create a HBLUETOOTH_RADIO_FIND handle for use with the BluetoothFindNextRadio function.
  33. /// When this handle is no longer needed, it must be closed via the BluetoothFindRadioClose.
  34. /// Returns NULL upon failure. Call the GetLastError function for more information on the error. The following table describe common errors:</returns>
  35. [DllImport("irprops.cpl", SetLastError = true)]
  36. static extern IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAM pbtfrp, out IntPtr phRadio);
  37. [StructLayout(LayoutKind.Sequential)]
  38. private struct LE_SCAN_REQUEST
  39. {
  40. internal int scanType;
  41. internal ushort scanInterval;
  42. internal ushort scanWindow;
  43. }
  44. [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
  45. static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
  46. ref LE_SCAN_REQUEST lpInBuffer, uint nInBufferSize,
  47. IntPtr lpOutBuffer, uint nOutBufferSize,
  48. out uint lpBytesReturned, IntPtr lpOverlapped);
  49. /// <summary>
  50. /// Starts scanning for LE devices.
  51. /// Example: BetterScanner.StartScanner(0, 29, 29)
  52. /// </summary>
  53. /// <param name="scanType">0 = Passive, 1 = Active</param>
  54. /// <param name="scanInterval">Interval in 0.625 ms units</param>
  55. /// <param name="scanWindow">Window in 0.625 ms units</param>
  56. public static void StartScanner(int scanType, ushort scanInterval, ushort scanWindow)
  57. {
  58. var thread = new Thread(() =>
  59. {
  60. BLUETOOTH_FIND_RADIO_PARAM param = new BLUETOOTH_FIND_RADIO_PARAM();
  61. param.Initialize();
  62. IntPtr handle;
  63. BluetoothFindFirstRadio(ref param, out handle);
  64. uint outsize;
  65. LE_SCAN_REQUEST req = new LE_SCAN_REQUEST
  66. { scanType = scanType, scanInterval = scanInterval, scanWindow = scanWindow };
  67. DeviceIoControl(handle, 0x41118c, ref req, 8, IntPtr.Zero, 0, out outsize, IntPtr.Zero);
  68. });
  69. thread.Start();
  70. }
  71. }
  72. }