How to determine Original BLE device from ScanResponse packet

PizzaCake-0876 0 Reputation points
2024-03-22T04:45:11.07+00:00

Hello,

I am trying to filter for a certain list of devices based on Service UUIDs available from the BLE advertising data. From what I understand, this is possible to do by enabling a watcher's scanner mode to be Active. By doing this, I am able to see the service UUID I am looking for. However, the device name is not present in this packet. Unfortunately I am a bit confused how to determine the original device that sent the scan response packet. Would anyone be able to provide assistance how to obtain the original device that sent out the scan response packet?

I do understand it's possible to obtain the Service UUIDs by connecting to the device, but I am not interested in connecting to each device in my area to determine these services.

My code in the OnAdvertisementReceived function is nearly the same as the sample:

private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
            // We can obtain various information about the advertisement we just received by accessing 
            // the properties of the EventArgs class

            // The timestamp of the event
            DateTimeOffset timestamp = eventArgs.Timestamp;

            // The type of advertisement
            BluetoothLEAdvertisementType advertisementType = eventArgs.AdvertisementType;

            // The received signal strength indicator (RSSI)
            Int16 rssi = eventArgs.RawSignalStrengthInDBm;

            // The local name of the advertising device contained within the payload, if any
            string localName = eventArgs.Advertisement.LocalName;

			// Get the Bluetooth address to later connect to
            ulong address = eventArgs.BluetoothAddress;
            string addressStr = eventArgs.BluetoothAddress.ToString();

            // Check if there are any manufacturer-specific sections.
            // If there is, print the raw data of the first manufacturer section (if there are multiple).
            string manufacturerDataString = "";
            var manufacturerSections = eventArgs.Advertisement.ManufacturerData;
            if (manufacturerSections.Count > 0)
            {
                // Only print the first one of the list
                var manufacturerData = manufacturerSections[0];
                var data = new byte[manufacturerData.Data.Length];
                using (var reader = DataReader.FromBuffer(manufacturerData.Data))
                {
                    reader.ReadBytes(data);
                }
                // Print the company ID + the raw data in hex format
                manufacturerDataString = string.Format("0x{0}: {1}",
                    manufacturerData.CompanyId.ToString("X"),
                    BitConverter.ToString(data));
            }

			var servicesFound = eventArgs.Advertisement.ServiceUuids;
            var dataSections = eventArgs.Advertisement.DataSections;
            foreach (var service in servicesFound)
            {
                Console.WriteLine("Service: {0}", service.ToString());
            }

   				Console.WriteLine(string.Format("[{0}]: type={1}, rssi={2}, name={3}, manufacturerData=[{4}, address={5}, services count:{6}, scanResponse {7}]",
                    timestamp.ToString("hh\\:mm\\:ss\\.fff"),
                    advertisementType.ToString(),
                    rssi.ToString(),
                    localName,
                    manufacturerDataString, addressStr, servicesFound.Count, eventArgs.IsScanResponse));
            });
        }

For my device, the list of Service UUIDs from

eventArgs.Advertisement.ServiceUuids

is always zero, unless it comes from the scan response packet. Any help would be greatly appreciated.

Kind regards

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,918 questions
{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.