简体   繁体   中英

Bluetooth LE C# Advertising and Scan Interval

I have recently started developing UWP projects and have been trying to establish a connection with a 'Minew S1' BLE device, which, according to its documentation, has three primary services running.

The documentation does not point out such a way, but I have found a possibility to read the device data (frameType, productModel, batteryLevel, temperature, humidity, MAC address) during the BLE scan, because the device responds (?) to these scans with an undocumented service (UUID: 0000ffe1-0000-1000-8000-00805f9b34fb) and a corresponding serviceData string.

I have created a simple BluetoothLEAdvertisementWatcher object and subscribed to its Received events. This way I get only one serviceData string from this object during runtime. The code snippet is below:

BluetoothLEAdvertisementWatcher bleWatcher;
bleWatcher = new BluetoothLEAdvertisementWatcher();
bleWatcher.ScanningMode = BluetoothLEScanningMode.Active;
bleWatcher.Received += BleWatcher_Received;
bleWatcher.Stopped += BleWatcher_Stopped;
bleWatcher.Start();

private void BleWatcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
    {
        DataReader dataReader;
        foreach (var item in args.Advertisement.ServiceUuids)
        {
            if (item == new Guid("0000ffe1-0000-1000-8000-00805f9b34fb"))
            {
                foreach (var data in args.Advertisement.DataSections)
                {
                    dataReader = DataReader.FromBuffer(data.Data);

                    byte[] bytes = new byte[data.Data.Length];
                    dataReader.ReadBytes(bytes);
                    if (BitConverter.ToString(bytes).Length > 5)
                    {
                        S1Sensor s1 = new 1Sensor(BitConverter.ToString(bytes));
                        Debug.WriteLine("New S1 Sensor advertisement received:");
                        Debug.WriteLine("Timestamp: " + DateTime.Now + ", Battery level: " + s1.BatteryLevel + "%, Temperature: " + 
                            s1.Temperature + "°C, " + "Humidity: " + s1.Humidity + "%, MAC Address: " + s1.MAC_Address + "==");
                    }
                 }
             }
         }
     }

Problem 1: The above code successfully prints the required data using the S1Sensor helper class method that I wrote. The only problem is that the device's advertising interval, by default, is set to 1000 ms, but I only receive this string once (rarely twice) during application run. Is there a way to receive and process these serviceData more frequently? (preferrably every 1000 ms)

Problem 2: My other problem is that this code only returns values when the device is paired to my computer. I think I should be able to scan for devices without connecting and see their response (serviceData) even when the device is not paired to the computer. Is there a way to query the serviceData string even when the computer that is running this application not paired to the device?

Thank you for your time.

Problem 1: Is there a way to receive and process these serviceData more frequently? (preferrably every 1000 ms)

If the device is in connection state instead of advertising state, it will not advertise and can cause you can't receive serviceData anymore. You need to disconnect your device in order to put it back in advertising state. You can check this thread .

Problem 2: Is there a way to query the serviceData string even when the computer that is running this application not paired to the device?

From Windows 10 Creators Update , this can be achieved by using BluetoothLEDevice.GetGattServicesAsync() , GattDeviceService.GetCharacteristicsAsync() as well as GattCharacteristic.GetDescriptorsAsync() to query a remote device without pairing.

I test UWP Bluetooth Low Energy sample with TI SensorTag BLE device. Without pairing I can read device data like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM