Windows BLE scanning interval issue

Nguyen Ba Quy 11 Reputation points
2020-06-25T02:40:11.337+00:00

We are using the Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher object on a UWP professional application to manage groups of bluetooth devices.
Each device send an advertisement every 250ms so the scanning result of one device should report about 240 advertisements in one minute.
BluetoothLEAdvertisementWatcher only returns 70-80 advertises and sometimes we can have a 9s duration between two advertisement.

So our question : is there a way to improve BluetoothLEAdvertisementWatcher BLE scan interval ?

Below is my code:

class Program
    {
        public static Stopwatch scanWatch;
        static void Main(string[] args)
        {
            BluetoothLEAdvertisementWatcher _bleWatcher;
            scanWatch = new Stopwatch();
            int count;
            _bleWatcher = new BluetoothLEAdvertisementWatcher { ScanningMode = BluetoothLEScanningMode.Active };

            _bleWatcher.Received += DeviceFoundAsync;
            _bleWatcher.Start();

            Console.WriteLine("Hello World!");
            Console.ReadKey();            
        }



        private static void DeviceFoundAsync(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs btAdv)
        {
            var deviceId = ParseDeviceId(btAdv.BluetoothAddress);
            if (deviceId.ToString().Contains("d0cf5e3a613f"))
            {
                var zx = btAdv.Advertisement.ServiceUuids.ToList();
                var debug = btAdv.Advertisement.DataSections.Where(x => x.DataType == 6).ToList();
                if (debug != null && debug.Count != 0)
                {
                    LogSimple(scanWatch.ElapsedMilliseconds.ToString());
                    //LogSimple("strength: " + btAdv.RawSignalStrengthInDBm);
                    scanWatch.Restart();
                }
            }
        }



        private static Guid ParseDeviceId(ulong bluetoothAddress)
        {
            var macWithoutColons = bluetoothAddress.ToString("x");
            macWithoutColons = macWithoutColons.PadLeft(12, '0'); //ensure valid length
            var deviceGuid = new byte[16];
            Array.Clear(deviceGuid, 0, 16);
            var macBytes = Enumerable.Range(0, macWithoutColons.Length)
                .Where(x => x % 2 == 0)
                .Select(x => Convert.ToByte(macWithoutColons.Substring(x, 2), 16))
                .ToArray();
            macBytes.CopyTo(deviceGuid, 10);
            return new Guid(deviceGuid);
        }



        //private static string LogFile = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Plugin_" + DateTime.Now.ToString("yyyyMMdd") + ".logs");
        private static ReaderWriterLockSlim _readWriteLock = new ReaderWriterLockSlim();
        public static void LogSimple(string x)
        {
            _readWriteLock.EnterWriteLock();
            try
            {
                using (StreamWriter sw = File.AppendText("D:/logfile.txt"))
                {
                    sw.WriteLine(x);
                    sw.Close();
                }
            }
            finally
            {
                _readWriteLock.ExitWriteLock();
            }
        }
    }
Developer technologies | Universal Windows Platform (UWP)
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.