The wireless keyboard, again.

So, again, I got a Bluetooth keyboard. This time, I’m pleased with the fact that it will, in principle, never need to have its batteries changed. It’s solar. I got the Logitech K760.


So of course, I set out to test it a good while before concluding whether or not it would be charged enough to keep up with me. But how much typing do I do in a day? Well, let’s find out!

The first problem I encountered had nothing to do with the keyboard itself. On some Linux boxes, it will pair with no problem, but with my aging (it’s 3-4 years old) i7 box, it won’t pair automatically. It’s a known bug with an easy fix (once you know it).

When you try to pair a Bluetooth device with your computer, you’ll see “please type 926193” (or something else) on your device. On some machines, you have to get the number from the Bluetooth system since the Gnome Bluetooth panel displays you a different one, and pairing will fail if you enter the PIN shown by the interface.

Before pairing, open a shell and type (you’ll need the package(s) that contain(s) hcitool and hcidump)

sudo hcidump -at | grep -A 1 "User Passkey Notification"

The sudo is important because hcidump behaves differently depending on its permission level when run. Then launch the interface for pairing, and wait for it to display its PIN. Enter the PIN displayed in the shell.

* *

We can know check if it’s paired correctly (or if it’s still there)

#!/usr/bin/env bash
for dev in $(hcitool con | grep ACL | cut -b 8-24)
 echo found: $dev
 hcitool info $dev | grep -i "device name:"

* *

And we can start counting events from the keyboard:

#!/usr/bin/env bash


# find keyboard

if [ $UID != 0 ]
    echo this script must be run as root
    exit 1

devs=$(hcitool con | grep ACL | cut -b 8-24)

if [ "${devs[@]}" != "" ]
    for d in ${devs[@]}
        hcitool info $d

    hcidump --timestamp --hex \
        | tail -n +3 \
        | sed -e 'N;N;s/\n/ /g' \
        | tr -s '[:space:]'
    echo "No devices found!"

This script must be run in as root (because, remember, hcidump behaves differently when run as user or root). It prints keyboard events with timestamp, so not only will I know how much typing I do, but also the speed at which I type.

So basically the script does nothing fancy except merging logs lines together. The sed -e 'N;N;s/\n/ /g' \ takes 3 lines of input to treat as one, and replaces the newlines within the 3 lines with spaces. The tr -s '[:space:]' merges multiple consecutive spaces into a single space.

When launched (as root), the script produces

Requesting information ...
	BD Address:  00:1F:20:3D:65:F9
	Device Name: Logitech K760
	LMP Version: 3.0 (0x5) LMP Subversion: 0x2211
	Manufacturer: Broadcom Corporation (15)
	Features page 0: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
		<3-slot packets> 
	Features page 1: 0xbf 0x04 0x82 0x78 0x00 0x06 0x59 0x83
2014-10-12 10:01:06.629800 > ACL data: handle 21 flags 0x02 dlen 14 L2CAP(d): cid 0x0041 len 10 [psm 0] A1 01 00 00 05 00 00 00 00 00 
2014-10-12 10:01:06.808554 > ACL data: handle 21 flags 0x02 dlen 14 L2CAP(d): cid 0x0041 len 10 [psm 0] A1 01 00 00 05 12 00 00 00 00 

Now, let’s grab data for a week or so.

To be continued…

5 Responses to The wireless keyboard, again.

  1. Zhihua Lai says:

    This is great, I was looking for this!

  2. […] last week’s entry, I wrote a small script to monitor my wireless keyboard’s activity, logging keys, and mode […]

  3. Do you know how to grab data from a non-bluetooth wireless keyboard? I would like to try this my self!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: