蓝牙BLE电池电量 - C#(Bluetooth BLE Battery Level - C#)

2019-10-28 11:38发布

您好用户,我对在Windows 10蓝牙BLE API目前IM编程的C#(Visual Studio中)一个工具的具体问题本身连接到一个给定的BLE -设备。 然而,连接工作完美的,我可以读出ServiceUUIDsCharacterUUIDs。

主要的问题是在我尝试读取字符它总是返回我00,我听到实现notifcation会改变的价值。 我跟着从instrucion: 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设备=等待BluetoothLEDevice.FromIdAsync(地址);

        //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”。 对于电池级的characterUUID(0x2A19,从https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml )为红色,但成功的值是奇怪..

我希望每一个答案。 谢谢。

文章来源: Bluetooth BLE Battery Level - C#