gpt4 book ai didi

c# - BLE 扫描间隔 Windows 10

转载 作者:太空狗 更新时间:2023-10-29 21:32:41 31 4
gpt4 key购买 nike

在使用 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com