Skip to content

Instantly share code, notes, and snippets.

@Pretz
Last active December 24, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pretz/6773344 to your computer and use it in GitHub Desktop.
Save Pretz/6773344 to your computer and use it in GitHub Desktop.
Reads the blutooth link key from the windows registry for a given paired bluetooth device (specified via MAC). Useful for multi-pairing with OS X and Loonix.
import winreg
import itertools
import binascii
import array
def hexlify(_bytes, reverse=True):
a = array.array('b', _bytes)
if swap:
a.reverse()
return binascii.hexlify(a.tobytes()).decode('utf-8')
MAC = '442a60ef7939'
reg_keys = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,
'SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Keys')
mac_addr = None
for i in itertools.count():
try:
device_addr = winreg.EnumKey(reg_keys, i)
sub_key = winreg.OpenKey(reg_keys, device_addr)
for i in itertools.count():
try:
mac_addr, key, kind = winreg.EnumValue(sub_key, i)
if mac_addr == MAC:
print('Key Found: %s, on device: %s' %
(hexlify(key), device_addr))
except OSError:
break
except OSError:
break
if not mac_addr:
print('No bluetooth device with given mac address found. Is it paired?')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment