如何在 Windows 上使用 PowerShell 获取蓝牙设备电池百分比?

发布于 2025-01-18 16:38:54 字数 121 浏览 4 评论 0原文

我正在尝试绘制蓝牙耳机电池放电图表。为此,我需要读取所连接设备的电池百分比。我可以在设备的 GUI 上看到电源信息。有没有办法使用 PowerShell 获取连接的蓝牙设备的电池百分比信息? (比如使用 wmi 或其他任何东西)

I am trying to plot a graph for Bluetooth headphone battery discharge. For that I need to read battery percentage of the connected device. I can see power information is available on GUI for the device. Is there any way to get the battery percentage info for connected Bluetooth device using PowerShell? (like using wmi or anything else)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

揽清风入怀 2025-01-25 16:38:54

根据我的发现,您可以使用 Get-PnpDevice cmdlet 获取有关蓝牙设备的信息。这应该返回 PnP 设备及其状态、类、FriendlyName 和 InstanceID 的列表。

Get-PnpDevice

您可以使用 -Class 参数过滤结果。要指定蓝牙 PnP 设备,您可以输入“Bluetooth”作为 -Class 参数的字符串值。

Get-PnpDevice -Class 'Bluetooth'

然后,您可以使用 -FriendlyName 参数通过其FriendlyName 从此列表中指定您想要的设备,并输入所需设备的FriendlyName 作为参数的字符串值。

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName'

注意:您还可以使用 -InstanceId 参数指定设备,并将设备的 InstanceId 作为参数的字符串值提供。

如果随后将上一个命令通过管道传递给 Get-PnpDeviceProperty cmdlet,它将返回设备属性的列表,包括其 InstanceId、KeyName、Type 和 Data。

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
    Get-PnpDeviceProperty

除此之外,我还可以通过使用 -KeyName 参数并输入(我假设)包含设备电源数据的属性的 KeyName 作为参数的字符串值来进一步过滤命令的结果。

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
    Get-PnpDeviceProperty -KeyName 'PropertyKeyName'

不幸的是,这就是我所能解决的问题。希望我的贡献有所帮助。

In my findings, you can get information on Bluetooth devices using the Get-PnpDevice cmdlet. This should return a list of PnP Devices, their Status, Class, FriendlyName and InstanceID.

Get-PnpDevice

You can filter the results with -Class parameter. To specify Bluetooth PnP devices, you can enter "Bluetooth" as a string value for the -Class parameter.

Get-PnpDevice -Class 'Bluetooth'

You can then specify the device you have in mind from this list by their FriendlyName using the -FriendlyName parameter and enter the desired device's FriendlyName as a string value for the parameter.

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName'

Note: You can also specify the device using the -InstanceId parameter and providing the device's InstanceId as a string value for the parameter.

If you then pipe the previous command to the Get-PnpDeviceProperty cmdlet, it will return a list of the device's properties, including its InstanceId, KeyName, Type and Data.

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
    Get-PnpDeviceProperty

Beyond this point, I was able to further filter the results of the command by using the -KeyName parameter and entering the KeyName of the property that (I assume) contains Device Power Data as a string value for the parameter.

Get-PnpDevice -Class 'Bluetooth' -FriendlyName 'Device FriendlyName' |
    Get-PnpDeviceProperty -KeyName 'PropertyKeyName'

Unfortunately this is as far as I've gotten to solving the problem. Hopefully my contribution helps.

殊姿 2025-01-25 16:38:54

保罗·阿马拉尔(Paulo Amaral)的评论确实是答案。

下面的代码将为您提供一个命令,您可以重用以查询设备的电池状态:

Get-PnpDevice -FriendlyName "*<Device Name>*" | ForEach-Object {
    $local:test = $_ |
    Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' |
        Where Type -ne Empty;
    if ($test) {
        "To query battery for $($_.FriendlyName), run the following:"
        "    Get-PnpDeviceProperty -InstanceId '$($test.InstanceId)' -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data"
        ""
        "The result will look like this:";
        Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data
    }
}

对于我的&lt; headSET&gt;设备,输出看起来与:

 查询电池的&lt;耳机,运行以下内容:
get-pnpdeviceproperty -instanceid'bthenum \ {0000 ####  -  0000- ####  -  #####  -  ############## _pid&amp; #### \#&amp&amp; ####### 104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2'

结果看起来像这样:
90
 

Paulo Amaral's comment above is indeed the answer.

The code below will provide you with a command you can reuse to query the battery state of your device:

Get-PnpDevice -FriendlyName "*<Device Name>*" | ForEach-Object {
    $local:test = $_ |
    Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' |
        Where Type -ne Empty;
    if ($test) {
        "To query battery for $($_.FriendlyName), run the following:"
        "    Get-PnpDeviceProperty -InstanceId '$($test.InstanceId)' -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data"
        ""
        "The result will look like this:";
        Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data
    }
}

For my <Headset> device, the output looked similar to:

To query battery for <Headset>, run the following:
Get-PnpDeviceProperty -InstanceId 'BTHENUM\{0000####-0000-####-####-############}_VID&########_PID&####\#&########&#&############_#########' -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2'

The result will look like this:
90
硬不硬你别怂 2025-01-25 16:38:54

据我所知,由于Windows设置中看到的电池状态 - &gt;蓝牙设备来自供应商/设备驱动程序,截至目前,PowerShell似乎无法接近,除非我不知道有一些异国情调的Snapin。

您可以通过此命令获取所有可能的设备信息:

 Get-WmiObject -Query "select * from win32_PnPEntity" | Where Name -like "MyDeviceName"

或者,如果您不确定设备的命名是如何命名的,则将返回“设备”的完整列表:

 Get-WmiObject -Query "select * from win32_PnPEntity" | Select Name

此外,我在注册表中找不到电池信息 - 也许有人可以扩展更多知识,因为注册表可能包含必要的信息,因为必须将其存储在设备上的某个地方。

As far as I am aware, there is no way to poll bluetooth device data beyond what you would get with Get-WmiObject, since the battery status seen in Windows Settings -> Bluetooth Devices is something coming from the vendors/devices driver and seems to, as of now, be inaccessible by PowerShell, unless there is some exotic snapin I am not aware of.

You can get all possible device information via this command:

 Get-WmiObject -Query "select * from win32_PnPEntity" | Where Name -like "MyDeviceName"

Or if you are unsure how the device is named as of now, this would return a complete list of "devices":

 Get-WmiObject -Query "select * from win32_PnPEntity" | Select Name

Additionally, I couldn't find battery information in the registry - maybe someone more knowledge can expand on that because the registry probably contains the necessary information as it must be stored somewhere on the device.

何处潇湘 2025-01-25 16:38:54

看来这个解决方案对来这里的人很少有效。
它没有给我想要的结果。我正在使用 Jabra 的耳机。
我在不同的电池电量下收集了这整组数据。
对我来说不幸的是,我没有得到一个值反映电池百分比的KeyName
我没有看到列表中的任何其他值根据 GUI 中显示的电池百分比发生变化。

我的 KeyName 集如下:

 {2BD67D8B-8BEB-48D5-87E0-6CDA3428040A} 4
 {3464F7A4-2444-40B1-980A-E0903CB6D912} 10
 {3B2CE006-5E61-4FDE-BAB8-9B8AAC9B26DF} 8
 {80497100-8C73-48B9-AAD9-CE387E19C56E} 6
 {83DA6326-97A6-4088-9453-A1923F573B29} 10
 {83DA6326-97A6-4088-9453-A1923F573B29} 15
 {83DA6326-97A6-4088-9453-A1923F573B29} 17
 {83DA6326-97A6-4088-9453-A1923F573B29} 3
 {A35996AB-11CF-4935-8B61-A6761081ECDF} 12
 {A8B865DD-2E3D-4094-AD97-E593A70C75D6} 26

Microsoft 本身正在读取该数据,这就是他们在通知中显示数据的方式。
微软准备好为这些重要信息推出更容易的阅读机制了吗?

It seems the solution worked for few who came looking here.
It is NOT giving me the desired results. I am using Jabra's headset.
I gathered this whole set of data at its different battery level.
Unfortunately for me, I am NOT getting a KeyName who's value reflects battery percentage.
I do NOT see any other value from the list that changed as per battery percentage shown in GUI.

My set of KeyName is as follwing:

 {2BD67D8B-8BEB-48D5-87E0-6CDA3428040A} 4
 {3464F7A4-2444-40B1-980A-E0903CB6D912} 10
 {3B2CE006-5E61-4FDE-BAB8-9B8AAC9B26DF} 8
 {80497100-8C73-48B9-AAD9-CE387E19C56E} 6
 {83DA6326-97A6-4088-9453-A1923F573B29} 10
 {83DA6326-97A6-4088-9453-A1923F573B29} 15
 {83DA6326-97A6-4088-9453-A1923F573B29} 17
 {83DA6326-97A6-4088-9453-A1923F573B29} 3
 {A35996AB-11CF-4935-8B61-A6761081ECDF} 12
 {A8B865DD-2E3D-4094-AD97-E593A70C75D6} 26

Microsoft is itself reading that data, that's how they display it in notification.
Is Microsoft ready to roll out easier reading mechanism for such crucial information?

聚集的泪 2025-01-25 16:38:54

我被蓝牙赶上了班级。事实证明,我的蓝牙耳机将电池信息存储在系统类中的设备中。我找到了一种过滤以基本上像Lockzsmith一样过滤找到合适的设备的方法。就像对于Jabra设备一样,您可以这样做:

Get-PnpDevice -FriendlyName '*jabra*' | Select-Object Class,FriendlyName,@{L="Battery";E={(Get-PnpDeviceProperty -DeviceID $_.PNPDeviceID -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2').Data}}

I got caught up on the Class being Bluetooth. It turns out my bluetooth headset stored the battery info in a device in the System Class. I found a way to filter to find the right device basically like Lockzsmith. Like for a jabra device you might do this:

Get-PnpDevice -FriendlyName '*jabra*' | Select-Object Class,FriendlyName,@{L="Battery";E={(Get-PnpDeviceProperty -DeviceID $_.PNPDeviceID -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2').Data}}
葮薆情 2025-01-25 16:38:54

要检查连接到计算机的所有蓝牙设备的电池百分比,只需运行以下命令:

Get-PnpDevice -Class 'Bluetooth' | ForEach-Object {
    $local:test = $_ |
    Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' |
        Where Type -ne Empty;
    if ($test) {
        "Battery percentage of $($_.FriendlyName) is: ";
        Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data
    }
}

to check the battery percentage of all Bluetooth devices connected to the computer, simply run the following command:

Get-PnpDevice -Class 'Bluetooth' | ForEach-Object {
    $local:test = $_ |
    Get-PnpDeviceProperty -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' |
        Where Type -ne Empty;
    if ($test) {
        "Battery percentage of $($_.FriendlyName) is: ";
        Get-PnpDeviceProperty -InstanceId $($test.InstanceId) -KeyName '{104EA319-6EE2-4701-BD47-8DDBF425BBE5} 2' | % Data
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文