Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect.bat script doesn't connect to headphones #5

Open
PolarGoose opened this issue May 14, 2020 · 18 comments
Open

connect.bat script doesn't connect to headphones #5

PolarGoose opened this issue May 14, 2020 · 18 comments

Comments

@PolarGoose
Copy link

PolarGoose commented May 14, 2020

Headphones: WH-1000XM3. The headphones are not paired with my PC.
OS: Windows 10.0.18362.

After I run connect.bat script with the mac address of my headphones, I get two disconnected sound devices as it is shown on the screenshot:
image

Could you please tell me what I should do next? I thought that the script will connect to the headphones. Am I doing something wrong?

@PolarGoose PolarGoose changed the title connect.bat script doesn't connect to the headphones connect.bat script doesn't connect to headphones May 15, 2020
@stanleyguevara
Copy link
Owner

If the headphones are not paired beforehand it won't work.

If by "not paired" you mean they are disconnected, but were paired beforehand it should work.

If it doesn't work clone the repo again, I just updated script to a simple on/off toggle instead of reading actual connection state, so at least it "kinda works" when you press the button. (Instead of "sometimes not working at all")

@PolarGoose
Copy link
Author

@stanleyguevara, thank you for the explanation.
While investigating my problem I have found that Windows has a very convenient API to work with Bluetooth devices.
Using this API I created a console utility for pairing and connecting to Bluetooth devices.

You can close this ticket.

@stanleyguevara
Copy link
Owner

That's nice, gonna check it out! Is it capable of doing also connect/disconnect or just pair/unpair?

@PolarGoose
Copy link
Author

Thank you, It does only pair/unpair but when you pair a device it also connects it. If the device is already paired it will unpair it first and then pair it and connect. I found it a better strategy because connecting doesn't always work and, in fact, it looks like only audio devices support the notion of "connect", for bluetooth mice, for instance, there is no such option. The advantage is also that you don't need to manually specify which services you want to enable, it enables all the services the same way Windows Bluetooth UI dialog does. It also works with Bluetooth LE devices.

@Paliverse
Copy link

Paliverse commented Mar 13, 2022

@stanleyguevara @PolarGoose

Hello, just like you guys, i hate having to go to BT settings to make my headphones work because im using Airpods, so I've actually found a good solution to this issue and building an app where it can auto connect the headphones that makes it behave as if you clicked the "Connect" button in bluetooth settings, it can also know the mac address of all your BT Devices without needing to install any other tools. I will update this when it's near completion :)

EDITI: just for clarafication, the app can connect to the headphones if it's in the "Paired" state.

@stanleyguevara
Copy link
Owner

@Paliverse cool! Definitely share here for visibility when you're done, I'll post a link in readme pointing to your repo and deprecate my script if yours can supersede it (it was a quick-hack-it-together solution anyways)

It'd be super nice if there's also some integration that allows connect/disconnect toggle by media button, similar to this script (honeslty this is the biggest value of this script for me, as one can use win+k to do the same, but that's like 3 actions and lots of looking at UI instead of a simple button press)

@PolarGoose
Copy link
Author

PolarGoose commented Mar 19, 2022

@Paliverse,
Could you please share what mechanism you use for that?

By the way I have found a way for making Windows connect to Bluetooth headphones:
https://stackoverflow.com/a/71539568/7585517
However, there is still no way to make Windows to disconnect.

@Paliverse
Copy link

@PolarGoose still working on the app, will be posting source code and a download when it’s ready, I’m developing this app in .Net WPF app. And I basically found a nice nugget package that handles Bluetooth devices

@Paliverse
Copy link

@stanleyguevara @PolarGoose The source of what i have built so far can be found here: AutoBT

@PolarGoose
Copy link
Author

PolarGoose commented Mar 20, 2022

@Paliverse ,
Thank you for sharing the source code.
I see, you use the same mechanism as the scripts from @stanleyguevara.
Pseudocode:

var device = GetPairedDevices().Where(d => d.Name == NameOfAudioDeviceWeWantToConnectTo);
foreach(service in device.GetServices())
{
    service.Disable();
}
foreach(service in device.GetServices())
{
    service.Enable()
}

There is a nice discussion related to the 32Feet library which you use, about ways of solving a problem of connecting to audio devices: inthehand/32feet#132
There we have discussed toggling services states and the author of the 32Feet library was not enthusiastic about this mechanism.
In this thread you can also find results of my investigation and why this mechanism didn't work for me.

@Paliverse
Copy link

@PolarGoose ah you’re already familiar with 32feet
Well it seems to do the job of connecting and disconnecting, just takes some time tho which sucks, if your pnp method works, could use that in the app instead. I wanted the to mainly auto try to connect the headphones if they’re available (turned on) when I turn on my pc

@PolarGoose
Copy link
Author

@Paliverse ,
Great that this method works for you. Just be aware that it is not completely reliable. I'm not sure "pnp method" is robust either.
If I figure out a better way I will let you know.
Thank you for your efforts, anyway.

@Paliverse
Copy link

@PolarGoose
In which ways would it be unreliable though? From testing disabling all services, disconnects the headphones, and enabling them all again, does connect them. However if you turn your pc on they need to be “forced” turned on, which is disabling all services then reenabling them only for the first time.

I really don’t know if there’s anything better, if you open control panel and manually uncheck all/recheck all services and hit apply, it takes about the same time as the app, which is telling me it’s a windows thing where the speed is what it is.

@stanleyguevara
Copy link
Owner

stanleyguevara commented Mar 20, 2022

@Paliverse I think by unreliable @PolarGoose means the state where headphones show "Connected" instead of "Connected voice, music" When it happens (with my script) the headphones become non-disconnectable (hitting "Disconnect" ends in "Couldn't disconnect" message IIRC). Restarting BT helps.

I couldn't pinpoint exact conditions when this happens, seemed for a while PC going to sleep has something to do. Might be also device-dependent, as all we know how wonderfully strict BT spec is and how nicely every peripheral vendor abides by it ;)

I'll try both solutions for some time and let you know how it went. Thanks!

@Paliverse
Copy link

@stanleyguevara
Ah yeah I k ow what you mean, I’ve ran into that, but it stopped happening as long as all services get disabled and all services are re-enabled again for it to work, if you just enable 1-2 out of 5 for example it might get to the “connected” state where it’s not active. I have AirPods Max and enabling all, and disabling all seems to work for disconnect/connect. I guess you can download my source and try to mess with it to see if you can achieve the same effect depending on your device, or if it’s the same.

@PolarGoose
Copy link
Author

Eventually, I found a way how to connect and disconnect Bluetooth headphones properly.
I have updated my utility BluetoothDevicePairing.
You can use it to connect and disconnect Bluetooth audio devices.

@Paliverse
Copy link

@PolarGoose It’s been so long, hope you’re doing alright. I actually totally forgot about this issue since I’ve mostly stopped using my AirPods and just opted for a soundbar on my desk lol. However if you're certain that you’ve found a reliable good working method, we can combine and create a little GUI App to make it easier for users.

@PolarGoose
Copy link
Author

@Paliverse,
I am glad that you have found the setup that works for you.

Yes, the approach works and is very reliable. You can read more details in my answer on StackOverflow and in the Readme of the ToothTray utility, whose author came up with the solution.

I don't think a GUI application is needed. There is already a default Windows Bluetooth Settings GUI that works pretty well. The only missing part was doing it from a console, which is currently covered by my BluetoothDevicePairing utility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants