Skip to content

windows设备管理

蓝牙

setup api

使用一些上层API的时候可能会用到setup api,这套api比较底层,比较难用,涉及一些概念,如IO control、PNP设备等。

setup api 对设备做了两种分类,一类是device setup class,另一类是device interface class。

每个class都有对应的GUID,查找GUID有两种方式

  1. 可以在设备管理器中查到。
    例如,蓝牙设备的GUID是e0cbf06c-cd8b-4647-bb8a-263b43f0f974
  2. GUID分为系统提供的和供应商提供的,系统提供的 GUID 被定义成了不同的宏,类似GUID_DEVCLASS_Xxx这样的形式,其中device setup class GUID 在 Devguid.h 这个头文件中,而device interface class的 GUID 定义在不同的头文件中。
    例如,蓝牙设备的GUID宏是GUID_DEVCLASS_BLUETOOTH

每个设备都有设备实例 ID,设备实例 ID 是系统提供的设备标识字符串,用于在系统中唯一标识设备。 设备实例ID在设备管理器中显示的名称是“设备实例路径”。设备实例ID可以通过SetupDiGetDeviceInstanceIdW这个api获取。

以下是一段微软官方提供的powershell脚本,可以用来获取蓝牙适配器的信息

# Copyright and License https://github.com/Microsoft/busiotools/blob/master/LICENSE

$devices = Get-PnpDevice -Class Bluetooth |? InstanceId -notlike "BTH*"

$radios = New-Object System.Collections.ArrayList
foreach ($device in $devices) {   
    $radio = New-Object PSObject
    Add-Member -InputObject $radio -MemberType NoteProperty -Name "InstanceId" -Value $device.InstanceId
    $property = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName 'DEVPKEY_Bluetooth_RadioAddress'
    Add-Member -InputObject $radio -MemberType NoteProperty -Name "MAC" -Value $(-join ($property.Data |  foreach { "{0:X2}" -f $_ } ))
    $radios.Add($radio) | Out-Null

    # Driver Info
    $property = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName 'DEVPKEY_Device_DriverDesc'
    Add-Member -InputObject $radio -MemberType NoteProperty -Name "DriverDescription" -Value $property.Data
    $property = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName 'DEVPKEY_Device_DriverVersion'
    Add-Member -InputObject $radio -MemberType NoteProperty -Name "DriverVersion" -Value $property.Data

    # Error Recovery
    $property = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName '{A92F26CA-EDA7-4B1D-9DB2-27B68AA5A2EB} 14'
    $supportedTypes = $property.Data
    if ($supportedTypes -eq 0) {
        Add-Member -InputObject $radio -MemberType NoteProperty -Name "ErrorRecovery" -Value "None"
    } elseif ($supportedTypes -band 1 -shl 0) {
        Add-Member -InputObject $radio -MemberType NoteProperty -Name "ErrorRecovery" -Value "FLDR"
    } elseif ($supportedTypes -band 1 -shl 1) {
        Add-Member -InputObject $radio -MemberType NoteProperty -Name "ErrorRecovery" -Value "PLDR"
    }

    # ScoType
    $property = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName '{A92F26CA-EDA7-4B1D-9DB2-27B68AA5A2EB} 21'
    if (([int32]$property.Type) -eq  0) {
        Add-Member -InputObject $radio -MemberType NoteProperty -Name "ScoType" -Value "Unknown"
    } else {
        $scoType = $property.Data
        if ($scoType -eq 0) {
            Add-Member -InputObject $radio -MemberType NoteProperty -Name "ScoType" -Value "SideBand"
        } else {
            Add-Member -InputObject $radio -MemberType NoteProperty -Name "ScoType" -Value "InBand"
        }
    }

    ## LMP 和 HCI
    $property = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName '{A92F26CA-EDA7-4B1D-9DB2-27B68AA5A2EB} 4'
    $LmpVersion = $property.Data
    Add-Member -InputObject $radio -MemberType NoteProperty -Name "LMP/HCI" -Value $LmpVersion

    $property = Get-PnpDeviceProperty -InstanceId $device.InstanceId -KeyName '{A92F26CA-EDA7-4B1D-9DB2-27B68AA5A2EB} 6'
    $HCIVersion = $property.Data
    Add-Member -InputObject $radio -MemberType NoteProperty -Name "LMP/HCI" -Value $HCIVersion
}
$radios

参考

获取设备电量

怎么获取,分不同的设备类型和协议

  • 蓝牙设备
    • BLE battery service
    • HFP(Hands-Free-Profile,免提协议)
  • HID设备(Human Interface Devices,人机接口设备)

对于windows的系统蓝牙功能,蓝牙设备的电量是在设备连接时获取一次的,不是实时的。参考https://blogs.windows.com/windows-insider/2018/04/04/announcing-windows-10-insider-preview-build-17639-for-skip-ahead/