How to know if a bluetooth device is connected or disconnected by Powershell

Viewed 158

I have the next sentence to show all bluetooth devices in Powershell.

(Get-PnpDevice -PresentOnly -class Bluetooth | ? HardwareID -match 'DEV_' | Select *).FriendlyName

The main problem, it's that even if the device isn't connected, it still shows, i haven't find a way to find a way to see if it's connected or disconnected, cause it only shows "OK" in its status, even if it's disconnected.

1 Answers

For audio devices, this will show Status as either OK or Unknown for connected/disconnected

Get-PnpDevice -class AudioEndpoint | Select FriendlyName,Status 

For other devices, it probably depends on the driver. The closest I could get without loading a bunch of windows api .dll files was by checking the driver properties.

However, this property is missing a standard friendly name for my device, so I have to use a property guid:

# Returns true/false for connection status:
$BtStatus = Get-PnpDevice -class Bluetooth -FriendlyName 'JVC HA-S65BN' | 
  Get-PnpDeviceProperty -KeyName '{83DA6326-97A6-4088-9453-A1923F573B29} 15' |
  Select -ExpandProperty Data

If that property ID isn't right for you, try and find one you can use by comparing the pnp device properties while connected/disconnected like so:

# while disconnected, get property values
$Disconnected = Get-PnpDevice -class Bluetooth -FriendlyName 'JVC HA-S65BN' | Get-PnpDeviceProperty
# run again while connected
$Connected = Get-PnpDevice -class Bluetooth -FriendlyName 'JVC HA-S65BN' | Get-PnpDeviceProperty

# compare to look for different values while connected/disconnected
Compare-Object $Disconnected $Connected -Property Data,Keyname -PassThru | 
  Select SideIndicator,KeyName,Data | Format-Table -AutoSize

# output:

SideIndicator KeyName                                   Data
------------- -------                                   ----
<=            {2BD67D8B-8BEB-48D5-87E0-6CDA3428040A} 5  3/24/2022 1:31:16 PM
<=            {83DA6326-97A6-4088-9453-A1923F573B29} 15 False
=>            {83DA6326-97A6-4088-9453-A1923F573B29} 15 True
<=            DEVPKEY_Bluetooth_LastConnectedTime       3/24/2022 1:31:16 PM
=>            DEVPKEY_Device_DevNodeStatus              25190410
<=            DEVPKEY_Device_DevNodeStatus              58744842

As you can see, the only useful device property that I could use is named {83DA6326-97A6-4088-9453-A1923F573B29} 15. I don't have other bluetooth devices to test with though, maybe someone else can confirm or add another example

Related