console.systems

How to pair a Low Energy (LE) Bluetooth device in dual boot with Windows & Linux

Thursday, September 18, 2014

Those who dualboot know the pain of re-pairing your keyboard or mouse everytime you boot to a different OS. In this tutorial I will show how to pair a LE Bluetooth mouse in both Windows 8 and Debian simultaneously.

First pair the device in Debian, then reboot in Windows and pair the device there too. Yes, this will reset the paring in Debian, just carry on. Now we need to access the pairing keys in Windows. Download psexec.exe and open a command prompt with Administrator rights.

> cd Downloads
> psexec.exe -s -i regedit /e C:\BTKeys.reg HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters\Keys

The keys should be now exported to C:\BTKeys.reg. You should get something like:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters\Keys\7512a3185b2c\84abd4a25ee1]
"LTK"=hex:6c,54,ee,80,40,47,6c,cb,fc,8e,f3,f1,c6,b2,04,9e
"KeyLength"=dword:00000000
"ERand"=hex(b):1e,12,aa,37,39,cc,af,34
"EDIV"=dword:00003549
"CSRK"=hex:38,d7,aa,c1,42,06,31,25,12,b8,5a,6d,c3,90,98,f2

7512a3185b2c is the MAC address of the Bluetooth adapter, which can also be written in standard format as 75:12:A3:18:5B:2C. 84abd4a25ee1 is the mouse address that was assigned during the pairing. We will need those numbers in the next steps.

Now boot again to Debian. The mouse wont paired automatically, because it is now assigned to a different address and with different keys. Let’s fix it.

$ cd /var/lib/bluetooth/75:12:A3:18:5B:2C/
$ ls
cache 84:AB:D4:A2:5F:E1 settings

If you look closely, the mouse address is not the same as in Windows. In my case only the fifth group is different. We need the device addresses to match, so rename the file.

$ mv 84:AB:D4:A2:{5F,5E}:E1
$ cd 84:AB:D4:A2:5E:E1/
$ ls
attributes gatt info

Now open the info file for editing and update the keys values. The relation between Windows and Bluez keys format is as follows:

At the end you should get this.

[LocalSignatureKey]
Key=38D7AAC14206312512B85A6DC39098F2

[LongTermKey]
Key=6C54EE8040476CCBFC8EF3F1C6B2049E
Authenticated=0
EncSize=0
EDiv=13641
Rand=3796477557015712286

Save the changes and reboot. From now on Debian and Windows will connect the mouse automatically. Awesome!

P.S. Mygod was nice to turn the instructions into a Python script, try it out.

Expanding wildcards with qmake

Tuesday, March 26, 2013

I was always wondering why doing something like this never worked

SOURCES += *.cpp
SOURCES -= excluded.cpp

excluded.cpp always stayed in SOURCES. The thing is that when you add a new value to the list, it’s just a string. Thus using wildcard in files names is nothing more but an asteriks character. And therefore using -= operator will only remove exact strings. But today I found another undocumented function I’ve been looking for years!

$$files(glob) — Returns a list of files which match the specified glob pattern.

So now I can do

SOURCES += $$files(*.cpp)
SOURCES -= excluded.cpp

and it actually works.

Shortcut to show folders first in Nautilus

Saturday, November 24, 2012

I really miss a menu shortcut in Nautilus to toggle “Show folders before files” option. Going to preferences every time is too much hassle.

But there is other way to toggle it using dconf or gconftool. For Ubuntu 12.04 or newer I suggest using dconf, install it if you don’t have it yet.

Create a script with the following content:

#!/bin/bash

# Version: Sun Sep 8 2013

OLD_VALUE=
LSB_RELEASE=$(lsb_release -sr)

if [ $LSB_RELEASE \< '12.04' ]; then
    OLD_VALUE=$(gconftool --get /apps/nautilus/preferences/sort_directories_first)
else
    OLD_VALUE=$(dconf read /org/gnome/nautilus/preferences/sort-directories-first)
fi

NEW_VALUE='false'
if [ $OLD_VALUE == 'false' ]; then
    NEW_VALUE='true'
fi

if [ $LSB_RELEASE \< '12.04' ]; then
    gconftool --type Boolean --set /apps/nautilus/preferences/sort_directories_first $NEW_VALUE
else
    dconf write /org/gnome/nautilus/preferences/sort-directories-first $NEW_VALUE
fi

If you have older Ubuntu, dconf maybe not available. Uncomment gconftool lines and comment dconf ones.

UPDATE 2013: the new version of the script works across all versions of Ubuntu.

Save the script somewhere, I prefer to keep mine in ~/.bin. Also give it execute permissions.

The next step may vary depending on Gnome version, I will describe it as it’s in Ubuntu 12.04. Go to keyboard shortcut setting and add a new custom shortcut. The command field set to path to the script we just created, in my case it’s ~/.bin/nautilus-folders-first.sh. You may also try to set full path if relative doesn’t work. Right click on the shortcut and set an actual hotkey combination. Choose something that doesn’t interferer with any other application, the shortcut is going to be system global. Ctrl+Alt+[some key] is good choice, I prefer Ctrl+Alt+F.

Now open Nautilus and browse where you have both files and folders, test the shortcut. If it doesn’t work, try the script in terminal and see if there are any errors.

Using Focusrite Scarlett 18i6 in Linux

Saturday, September 8, 2012

With the current 1.0.25 ALSA release you need to do some hacking to get Scarlett working. But the hack is as simple as it can be.

Download the tarball ftp://ftp.alsa-project.org/pub/driver/alsa-driver-1.0.25.tar.bz2 and comment the call to snd_usb_create_mixer() on the line 517 in alsa-kernel/usb/card.c so it would look like:

if (err > 0) {
    /* create normal USB audio interfaces */
    if (snd_usb_create_streams(chip, ifnum) < 0 /*||
        snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0*/) {
        goto __error;
    }
}

Build and install:

$ ./configue
$ make
$ sudo make install

Considering a bad practice to use sudo make install, in our case you don’t need to worry. Just reboot and select Scarlett as a default device in Sound Settings. Voila!

To restore Scarlett after reconnecting it from the USB port use:

$ sudo alsa force-reload
$ alsactl restore 0

Bash auto-completion trick

Tuesday, July 31, 2012

zhs has a nice feature. If a program supports --help then zsh can do auto-completion of its arguments. The trick is that zsh silently runs the program with --help argument, parses the output and use it for auto-completion. I wish bash could do that. Well, not exactly but it can. Bash has auto-completions too using templates. But if you want to add auto-completion to your program you would need to write a new template and either put it to /etc/bash_completion.d/ or source in .bashrc or .profile.

But you don’t have to do any of it if you can re-use bash templates as is. For example the template for configure scripts does almost the same what zsh does for all programs. It simply parses the output from ./configure --help. Unfortunately it works only for arguments that prefixed with double dash. Better than nothing anyways.

<- Newer Posts