Connect to a paired Bluetooth device from C#.
It is a duplicate of my question on StackOverflow.
There is a "Connect" and "Disconnect" buttons in Windows Bluetooth settings.
I want to create a console application which would make Windows connect and disconnect from my Bluetooth Headphones or other Bluetooth devices. However, I can't find a proper API to do that.
I have done some investigation described here but none of the ways I have found worked.
Could you help me finding a proper API or a workaround?
Windows API - Win32
C#
-
Jack J Jun 24,491 Reputation points • Microsoft Vendor
2022-02-02T09:12:10.133+00:00 @PolarGoose , you could try to use the following code to get a list of connected bluetooth devices.
The answer could tell you about it.
-
PolarGoose 11 Reputation points
2022-02-02T11:37:00.57+00:00 Why do I need a list of connected Bluetooth devices?
-
Castorix31 82,751 Reputation points
2022-02-02T12:41:01.047+00:00 There are some sample codes on Google, but not sure if they are useful, like :
(in C++, but if they work, not too difficult to convert into C# with P/Invoke...)
BluetoothLowEnergy.cpp
Bluetooth connection sample
(I have no BT devices, so I cannot test...) -
PolarGoose 11 Reputation points
2022-02-02T13:35:49.203+00:00 No, unfortunately it is not what I'm asking for. I don't need to connect to a Bluetooth device. I need to command Windows to connect or disconnect from a Bluetooth device.
-
Jack J Jun 24,491 Reputation points • Microsoft Vendor
2022-02-03T09:40:00.1+00:00 Why do I need a list of connected Bluetooth devices?
Because you want to connect to a paired Bluetooth device.
DeviceInformationCollection PairedBluetoothDevices =await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelectorFromPairingState(true));
We could get the connection of paired Bluetooth Device, then we could filter it by name to connect which we want to connect
foreach (DeviceInformation item in PairedBluetoothDevices) { if(item.Name=="") { //connect to device use DeviceID } }
-
PolarGoose 11 Reputation points
2022-02-03T09:47:03.96+00:00 I don't need to connect to a Bluetooth device. What I want is the following quote from my question:
I want to create a console application which would make Windows connect and disconnect from my Bluetooth Headphones or other Bluetooth devices
I just want to automate the procedure of connecting to my headphones for instance, instead of always opening Windows settings and pressing the "Connect" button.
-
Jack J Jun 24,491 Reputation points • Microsoft Vendor
2022-02-09T07:15:26.4+00:00 @PolarGoose , Sorry for the late repsponse, Based on my search, I sugegst that you could use powershell to enable it to make Windows connect and disconnect the Bluetooth Device.
Like the following link:
how-to-connect-and-disconnect-a-bluetooth-device-with-a-powershell-script-on-win -
PolarGoose 11 Reputation points
2022-02-13T09:16:21.283+00:00 Thank you for your proposal. Unfortunately, the script that you proposed doesn't work:
- When my audio device is connected, calling
Disable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
does nothing. I would expect it to disconnect my headphones. - When my headphones are disconnected, calling the script
Disable-PnpDevice, Wait, Enable-PnpDevice
doesn't make Windows to connect to my audio device. This sequence of commands just does nothing (maybe it does something but I don't see any effect).
- When my audio device is connected, calling
-
zpcc 11 Reputation points
2022-03-11T13:55:53.777+00:00 I've encountered the same problem and find your post here, and I noticed a strange device name ends with " Avrcp transport" using
Get-PnpDevice -class Bluetooth
. Maybe you'd change the friendlyname option. -
PolarGoose 11 Reputation points
2022-03-19T15:45:56.147+00:00 @zpcc ,
Thank you for the hint. It indeed works.
I have posted the results of my testing to my original StackOverflow post, please check it out:
https://stackoverflow.com/a/71539568/7585517However, it is still a workaround. And it seems it is not what "Bluetooth devices" dialog does when you click "Connect". I would really like to find a proper solution.
-
Eric 0 Reputation points
2024-05-04T17:17:06.5+00:00 idk if this is still needed, or not, as it has not been marked as having an answer, from as far as I can tell, but here is a YouTuber, who created a C# program, to communicate with a Bluetooth device.
https://github.com/michaelreeves08/car-bci-shit
Sign in to comment