Accessing Battery via Windows.Devices.Custom.CustomDevice RRS feed

  • Question

  • I have one WPF application accessing all sort of low level Battery information (DesignedCapacity, wear level, current charge/discharge rate etc). I am using PInvoke and .Net 4.5.1 (On the lines of Enumerating Battery Devices). Now I am trying to migrate it to WinRT. I understand that CreateFile mechanism or Open handle to devices can't be used in WinRT due to access restrictions.  

    I am trying to use Windows.Devices.Custom.CustomDevice. Below is the core logic of enumerating all batteries in the device.

    private async void EnumerateBatteries() { var batteryGUID = new Guid (0x72631e54, 0x78a4, 0x11d0, 0xbc, 0xf7, 0x00, 0xaa, 0x00, 0xb7, 0xb3, 0x2a); var deviceFilter = Windows.Devices.Custom.CustomDevice.GetDeviceSelector (batteryGUID); var allDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync (deviceFilter); foreach ( var device in allDevices ) { var battery = await CustomDevice.FromIdAsync (device.Id, DeviceAccessMode.Read, DeviceSharingMode.Shared); } return; }

        <Capability Name="internetClient" />
          <DeviceCapability Name="72631E54-78A4-11D0-BCF7-00AA00B7B32A" />

    Now when I run this code, in the CustomDevice.FromIdAsync step, I am getting an error

    WinRT information: The target device interface is not marked as restricted.

    Additional information: Access is denied.

    Now my question is, how do I access the battery information using built in Microsoft battery driver? I want to tie my app to a device class, rather than specific version of the device, so I am not sure if I can go for a custom driver in this case. I am open to hybrid app ideas too.


    Regards, Adarsha

    • Edited by mvadu Monday, November 11, 2013 4:48 PM typo
    Saturday, November 9, 2013 2:58 PM

All replies

  • Hi, mvadu

    Battery info is maintained by system. You cannot access it in windows store app.

    There do not have Power APIs in windows store app. Please check Alternatives to Windows APIs in Windows store app

    You would need to write that app as a desktop app. GetSystemPowerStatus API is used to retrieve the power status of the system. The status indicates whether the system is running on AC or DC power, whether the battery is currently charging, and how much battery life remains.

    Best Wishes!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Edited by Anne Jing Monday, November 11, 2013 5:00 AM edit
    Monday, November 11, 2013 4:56 AM
  • Thanks for the response Anne. I am not sure if you read my question properly. I already have a WPF (aka desktop) application doing exactly what I want. I wanted to use my app hub membership by submitting an app. Looks like it will have to wait.

    On a side note, GetSystemPowerStatus API is actually very limited. It does not report the batteries designed capacity, which is needed to calculate battery wear level (do you know once your Lithium battery charges only up to 80% of its designed capacity i.e. 20% wear, its essentially treated as dead?). For these information you have to get dirty and get to WIn32 device control APIs, as shown in the example MSDN article I linked earlier.

    Regards, Adarsha

    Monday, November 11, 2013 4:46 PM