簡體   English   中英

BLE 掃描間隔 Windows 10

[英]BLE Scan Interval Windows 10

使用Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher時,有沒有辦法調整 Windows 10 上的 BLE 廣告掃描間隔? 在 Android 上掃描時,我每 100 毫秒看到一次廣告,但在使用 C# 的 Windows 10 上,我只收到一個BluetoothLEAdvertisementWatcher.Received事件,每 700 毫秒拋出一次。

我猜不是。

掃描參數被硬編碼為 118.125 ms 的掃描間隔和 18.125 ms 的掃描窗口。

這就是為什么你只能得到所有數據包的 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();
    }
}

可能是如果您設置SignalStrengthFilter頻率的SamplingInterval將會改變。 試試這個:

BluetoothLEAdvertisementWatcher watcher=new BluetoothLEAdvertisementWatcher();
watcher.SignalStrengthFilter.SamplingInterval = 100;

對於 Windows 通用應用程序嘗試更改 StartScanner 如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM