Skip to content

Instantly share code, notes, and snippets.

@stevemason
Last active March 30, 2021 12:53
Show Gist options
  • Save stevemason/7905ade97ccd134eb8d616b00e0a22ba to your computer and use it in GitHub Desktop.
Save stevemason/7905ade97ccd134eb8d616b00e0a22ba to your computer and use it in GitHub Desktop.
Bluetooth mouse - Windows and Ubuntu dual boot.
r"""
Bluetooth mouse - Windows and Ubuntu dual boot.
[ more info on my blog here https://wp.me/p1u8Xj-ib ]
Ordinarily, if you pair a Bluetooth mouse in Linux,
then boot into a Windows partition, you'll need to
pair the mouse again in Windows for it to work. As a
consequence, the pairing will be lost in Linux. You'll
have to pair again when you switch partition to get it
to work. This is not sustainable!
To get it to work across both partitions once and for
all, you need to export some of the pairing data from
the registry of the Windows partition, manipulate the
data a little, then import it into the relevant
Bluetooth config file in Linux.
There are quite a few guides out there on how to do
this - I tried a few and none of them quite worked for
me, though they did help me to put this solution
together.
In this instance, I used a Microsoft Bluetooth Mouse,
pairing with an Inateck Bluetooth USB dongle. The
operating systems were Windows 10 and Ubuntu 18.04.
Step 1 - pair the mouse in the Linux partition
Step 2 - pair the mouse in the Windows partition
Step 3 - using regedit running as administrator in
Windows, browse to
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Keys
Step 4 - change the permissions on the registry key
to give yourself read access
Step 5 - press f5 to refresh the view
Step 6 - this should reveal a key with an 'adapter
address' as its name, and a similar-looking
'remote device address' sub-key
Step 7 - you'll hopefully see some values called LTK,
KeyLength, EDIV and ERand
Step 8 - fill those values in in the section below,
replacing the example values:
"""
LTK = "f3,c4,b6,43,51,70,8d,6a,ff,45,81,e4,4c,99,75,e4"
KeyLength = "00000010"
EDIV = "0000b333"
ERand = "e6,5f,88,bc,cc,09,0b,32"
"""
Step 9 - run this Python code, and it'll generate the
corresponding values that you'll need in Linux
Step 10 - boot into Linux, and substitute the generated
values into the 'info' file in
/var/lib/bluetooth/<adapter address>/<remote device address>
Step 11 - rename the <remote device address> folder to
match the remote device address shown in the
Windows registry
Step 12 - reboot, or restart the bluetooth service
('# service bluetooth restart')
[I found it could take a couple of minutes for
the mouse to show as connected]
Step 13 - mouse!
Step 14 - once you have it working, it's probably a good
idea to go back and set the Windows registry
permissions back to how they were originally
"""
# Calculate 'Key' value
Key = "".join(LTK.split(",")).upper()
print("Key={}".format(Key))
# Calculate 'EncSize' value
EncSize = int(KeyLength, 16) # Convert hex to decimal
print("EncSize={}".format(EncSize))
# Calculate EDiv value
EDiv = int(EDIV, 16) # Convert hex to decimal
print("EDiv={}".format(EDiv))
# Calculate Rand value
ERand_list = ERand.split(",")
ERand_list.reverse() # Reverse order of hex pairs
ERand_rev_hex = "".join(ERand_list)
Rand = int(ERand_rev_hex, 16) # Convert hex to decimal
print("Rand={}".format(Rand))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment