Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benjamingwynn/c3359dfffe155d76660174ff40a41ecf to your computer and use it in GitHub Desktop.
Save benjamingwynn/c3359dfffe155d76660174ff40a41ecf to your computer and use it in GitHub Desktop.
Actually gets Google Home's bluetooth to connect reliably on Windows 10. Fixes pairing issues, stuck paired issue and just makes the whole process a lot less awful.
# Actually gets Google Home's bluetooth to connect reliably on Windows 10.
# Unpairs Windows AND Google Home using MAC address, then sets everything up for a one-click connection.
# * Prevents Google Home from getting stuck paried using bttool, which is apparently the only way to fix that?
# * Uses some code from Stack Overflow to get the IP from the MAC address so you don't have to dick around with static IP's
# * Connects reliably by repairing bluetooth on Google Home using the "secret" REST API (https://rithvikvibhu.github.io/GHLocalApi)
# * Opens control legacy panel after setting up Google for pairing
#
# Needs btpair installed first, get it from http://bluetoothinstaller.com/bluetooth-command-line-tools/
# Also needs "requests", do `pip install requests`
#
# @author Benjamin Gwynn <[email protected]>
import os
import requests # pip install requests (or pipenv if you're into that kind of thing)
import sys
import ctypes
import subprocess
# by the way this is for windows only, if you're using linux or macos just do something else.
# 99% sure all python is copy-pasted from the internet, https://stackoverflow.com/a/41930586
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
# more code copied from stack overflow https://stackoverflow.com/a/56022769
def find_ip(mac):
macstr = mac.replace(":", '-') # apparently you can't use ':', you have to use '-' for ARP, la dee da.
cmd = 'arp -a | findstr "' + macstr + '" '
returned_output = subprocess.check_output((cmd),shell=True,stderr=subprocess.STDOUT)
parse=str(returned_output).split(' ',1)
ip=parse[1].split(' ')
return ip[1]
# spit out usage errors
if (sys.argv.count < 2):
print("usage " + sys.argv[0] + " <google home mac address>")
print("- you can find the mac address on the bottom of the device settings on the Google Home app")
exit(1)
mac = sys.argv[1].lower() # lower case is important for correct ARP lookup in find_ip.
print("Google Home MAC Address: " + mac)
ip = find_ip(mac)
print("Google Home IP: " + ip)
if is_admin():
# btpair requires admin.
print("Hey, you're an admin, that's great!")
print("Unpairing Windows Bluetooth devices...")
os.system("btpair -u") # why not btpair -u with a specific mac? because it doesn't work, try it.
else:
print("\n!! You must be an admin to run this script!\n\n* Why? The btpair program needs it to unpair devices RELIABLY, unlike Control Panel which always does so at user-level, causing devices to get stuck. Please check the source code before running this as an admin to ensure it has not been tampered with.\n")
exit(1)
# this is done using https://rithvikvibhu.github.io/GHLocalApi/#bluetooth-bluetooth-bond-post
print("Forgetting paired devices on Google Home...")
requests.post("http://" + ip + ":8008/setup/bluetooth/bond", headers={"Content-Type": "application/json"})
print("Setting Google Home to discoverable...")
requests.post("http://" + ip + ":8008/setup/bluetooth/discovery", headers={"Content-Type": "application/json"})
print("Trying to use btpair to connect, this NEVER works for me, but might for you...")
os.system("btpair -p -b " + mac)
print("Opening the legacy Bluetooth panel (fun-fact: you can close this panel after the device starts to install!)")
os.system("DevicePairingWizard.exe")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment