蓝牙BLE电池电量 - C#

时间:2018-06-13 09:39:09

标签: c# arrays windows visual-studio bluetooth

Hello Users, 我有一个关于Windows 10上的蓝牙BLE API的具体问题。 目前我在C#(Visual Studio)上编写一个工具,它将自己连接到给定的BLE - Device。然而,连接工作完美,我可以读出 ServiceUUIDs CharacterUUIDs

主要问题是在我尝试读取Character的值后,它总是返回00.我听说实现通知会改变它。我跟着来自https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client的指示,但是他们没有给我很多帮助(=>相同的结果);

(以下是bluetooth.com的规格:  https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml

这是我的代码:        //连接到BluetoothDevice             var device = await BluetoothLEDevice.FromIdAsync(address);

        //get UUID of Services
        var services = await device.GetGattServicesAsync();
        if (services != null)
        {
            foreach (var servicesID in services.Services)
            {

                //if there is a service thats same like the Battery Service
                if (servicesID.Uuid.ToString() == BluetoothBLE.Constants.BATTERY_SERVICE)
                {
                    //updateServiceList is like a console logging in my tool
                    updateServiceList($"Service: {servicesID.Uuid}");

                    var characteristics = await servicesID.GetCharacteristicsAsync();
                    foreach (var character in characteristics.Characteristics)
                    {
                        if (Constants.BATTERY_LEVEL == character.Uuid.ToString())
                        {

                            updateServiceList("C - UUID: "+ character.Uuid.ToString());
                            GattReadResult result = await character.ReadValueAsync();
                            if (result.Status == GattCommunicationStatus.Success)
                            {
                               var reader = DataReader.FromBuffer(result.Value);
                               byte[] input = new byte[reader.UnconsumedBufferLength];
                               reader.ReadBytes(input);
                               System.Diagnostics.Debug.WriteLine(BitConverter.ToString(input));


                            }







                        }
                        }

                }

因此,在运行我的代码后,系统会记录“00”。电池电量的字符UUID(0x2A19,来自https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml)成功为红色,但值很奇怪。< / p>

我很感激每一个答案。谢谢。

0 个答案:

没有答案