DEV Community

Shane Duffy
Shane Duffy

Posted on • Updated on • Originally published at shaneduffy.io

Handling Bluetooth Programmatically on Windows

I use a command to connect to my Pixel Buds now, and switch between Bluetooth devices. It's pretty convenient, especially if you're still on Windows 10, with its buggy Bluetooth auto-connection.

Install Bluetooth Command Line Tools

This is the CLI tool provided by bluetoothinstaller.com, which has provided a humble suite of Bluetooth quality of life tools since 2009.

Connecting to a Bluetooth Device

You can connect by associating a COM service with a Bluetooth device. This is done via the btcom command.

btcom {-c|-r} {-b BluetoothAddress | -n FriendlyName} [-s {sp|dun|GUID|UUID}]

  -c  Create association between COM port and a remote service (Enable non-COM service).
  -r  Remove association between COM port and a remote service (Disable non-COM service).
  -s  Remote service to use (Default is Serial Port Service)
  -b  Bluetooth address of remote device in (XX:XX:XX:XX:XX:XX) format. 
  -n  Friendly name of remote device.
  -h  Prints this help screen.
Enter fullscreen mode Exit fullscreen mode

You'll need to figure out the specific COM services that you want to hook into. Windows connects headsets via two COM services: A2DP (110b) and HFP (111e), so you may need to connect to both for it to work properly.

If you're having issues connecting (-c), try disconnecting (-r) the device first. This has worked effectively for me, although it will generally take a few seconds longer to connect.

Connect

btcom -n "Shane's Pixel Buds" -c -s110e
btcom -n "Shane's Pixel Buds" -c -s110b
Enter fullscreen mode Exit fullscreen mode

Disconnect

btcom -n "Shane's Pixel Buds" -r -s110e
btcom -n "Shane's Pixel Buds" -r -s110b
Enter fullscreen mode Exit fullscreen mode

What Bluetooth Devices Are Available?

The Friendly Name for my Pixel Buds is "Shane's Pixel Buds". You can list available Bluetooth devices and COM services using btdiscovery command. This will list the ID as well as the Friendly Name.

btdiscovery [-i Seconds] [-b BluetoothAddress | -n FriendlyName] [-d Format] [-s [Format]]

  -i  Set the length of inquiry to the specified number of seconds.
  -b  Bluetooth address of remote device in (XX:XX:XX:XX:XX:XX) format.
  -n  Friendly name of temote device.
  -d  Set output format for the list of discovered devices.
  -s  Make service discovery. Optionally set output format for the list of services.
  -h  Prints help screen.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)