/// <summary> /// Starts scanning for LE devices. /// Example: BetterScanner.StartScanner(0, 29, 29) /// </summary> /// <param name="scanType">0 = Passive, 1 = Active</param> /// <param name="scanInterval">Interval in 0.625 ms units</param> /// <param name="scanWindow">Window in 0.625 ms units</param> public static void StartScanner(int scanType, ushort scanInterval, ushort scanWindow) { var thread = new Thread(() => { BLUETOOTH_FIND_RADIO_PARAM param = new BLUETOOTH_FIND_RADIO_PARAM(); param.Initialize(); IntPtr handle; BluetoothFindFirstRadio(ref param, out handle); uint outsize; LE_SCAN_REQUEST req = new LE_SCAN_REQUEST { scanType = scanType, scanInterval = scanInterval, scanWindow = scanWindow }; DeviceIoControl(handle, 0x41118c, ref req, 8, IntPtr.Zero, 0, out outsize, IntPtr.Zero); }); thread.Start(); }
/// <summary> /// Starts scanning for LE devices. /// Example: BetterScanner.StartScanner(0, 29, 29) /// </summary> /// <param name="scanType">0 = Passive, 1 = Active</param> /// <param name="scanInterval">Interval in 0.625 ms units</param> /// <param name="scanWindow">Window in 0.625 ms units</param> public static void StartScanner(int scanType, ushort scanInterval, ushort scanWindow) { Action <object> action = (object obj) => { BLUETOOTH_FIND_RADIO_PARAM param = new BLUETOOTH_FIND_RADIO_PARAM(); param.Initialize(); IntPtr handle; BluetoothFindFirstRadio(ref param, out handle); uint outsize; LE_SCAN_REQUEST req = new LE_SCAN_REQUEST { scanType = scanType, scanInterval = scanInterval, scanWindow = scanWindow }; DeviceIoControl(handle, 0x41118c, ref req, 8, IntPtr.Zero, 0, out outsize, IntPtr.Zero); }; Task task = new Task(action, "nothing"); task.Start(); }
static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, ref LE_SCAN_REQUEST lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, IntPtr lpOverlapped);
private static extern bool DeviceIoControl_(SafeFileHandle hDevice, uint dwIoControlCode, ref LE_SCAN_REQUEST lpInBuffer, uint nInBufferSize, IntPtr lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, ref NativeOverlapped lpOverlapped);
/// <summary></summary> public static bool DeviceIoControl(SafeFileHandle hDevice, uint dwIoControlCode, ref LE_SCAN_REQUEST ble_scan_request, IntPtr lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, NativeOverlapped?overlapped) { var ovr = overlapped ?? new NativeOverlapped(); return(DeviceIoControl_(hDevice, dwIoControlCode, ref ble_scan_request, (uint)Marshal.SizeOf <LE_SCAN_REQUEST>(), lpOutBuffer, nOutBufferSize, out lpBytesReturned, ref ovr)); }