c# - BLE 扫描间隔 Windows 10

标签 c# windows-10

在使用 Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher 时,是否有任何方法可以调整 Windows 10 上的 BLE 广告扫描间隔?在 Android 上扫描时,我每 100 毫秒就会看到一次广告,但在使用 C# 的 Windows 10 上,我可能每 700 毫秒就会收到一个 BluetoothLEAdvertisementWatcher.Received 事件。

最佳答案

我猜不是。

扫描参数被硬编码为 118.125 毫秒的扫描间隔和 18.125 毫秒的扫描窗口。

这就是为什么您只能获得所有数据包的 1/7(因为 18.125/118.125 是 ~1/7)。

不过,您可以使用 DeviceIoControl 在更底层进行操作。这是一个例子。您必须将它与您的 BluetoothLEAdvertisementWatcher 并行运行(例如 BetterScanner.StartScanner(0, 29, 29))。如果两个扫描仪同时处于事件状态,Windows 似乎总是会选择“最佳”参数。

DeviceIoControl 永远不会返回,所以我在一个单独的线程中运行它。如果您需要能够取消扫描,您必须使用重叠的 io 才能执行 CancelIoEx。此代码不检查错误,因此请注意。

using System;
using System.Runtime.InteropServices;
using System.Threading;

class BetterScanner {
    /// <summary>
    /// The BLUETOOTH_FIND_RADIO_PARAMS structure facilitates enumerating installed Bluetooth radios.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    private struct BLUETOOTH_FIND_RADIO_PARAM
    {
        internal UInt32 dwSize;
        internal void Initialize()
        {
            this.dwSize = (UInt32)Marshal.SizeOf(typeof(BLUETOOTH_FIND_RADIO_PARAM));
        }
    }

    /// <summary>
    /// Closes an open object handle.
    /// </summary>
    /// <param name="handle">[In] A valid handle to an open object.</param>
    /// <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>
    [DllImport("Kernel32.dll", SetLastError = true)]
    static extern bool CloseHandle(IntPtr handle);

    /// <summary>
    /// Finds the first bluetooth radio present in device manager
    /// </summary>
    /// <param name="pbtfrp">Pointer to a BLUETOOTH_FIND_RADIO_PARAMS structure</param>
    /// <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>
    /// <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.
    /// When this handle is no longer needed, it must be closed via the BluetoothFindRadioClose.
    /// Returns NULL upon failure. Call the GetLastError function for more information on the error. The following table describe common errors:</returns>
    [DllImport("irprops.cpl", SetLastError = true)]
    static extern IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAM pbtfrp, out IntPtr phRadio);

    [StructLayout(LayoutKind.Sequential)]
    private struct LE_SCAN_REQUEST
    {
        internal int scanType;
        internal ushort scanInterval;
        internal ushort scanWindow;
    }

    [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
    ref LE_SCAN_REQUEST lpInBuffer, uint nInBufferSize,
    IntPtr lpOutBuffer, uint nOutBufferSize,
    out uint lpBytesReturned, IntPtr lpOverlapped);

    /// <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();
    }
}

关于c# - BLE 扫描间隔 Windows 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37307301/

相关文章:

c# - 如何使用代码优先方法在 IdentityServer4 的客户端表中添加新列?

selenium - 如何更新 Windows 10 的 Chrome 驱动程序

c# - 在 Windows 10 更新 1803 之后,我的程序在从网络共享运行时无法打开套接字

c# - 如何为在 Windows 10 上运行的 Windows 8.1 应用程序指定初始窗口大小

c# - 存储有关文件的其他元数据

javascript - 使用 codedUI 选择网页中隐藏的单选按钮

c# - 在构建时将所有依赖程序集合并到 Exe

python - 我可以在Windows 10 Pro上使用带有最新pycharm 2016.3.2的docker引擎吗

Python pyHook 返回无效字符(框而不是字符)

c# - 将别名/前缀添加到现有 XML 命名空间