Saturday, October 27, 2018

Attaching a Bluetooth keyboard to a NetBSD machine

I am FINALLY able to use my Apple Bluetooth keyboard with NetBSD! I am writing this blog post right now using it. The NetBSD Guide helped me a LOT with this, but I believe it requires a little bit of updating… so in the meantime, let me show you how I did it.

Overall strategy

  • Enable the Bluetooth system service.
  • Figure out what your machine’s Bluetooth device node is called.
  • Put your keyboard into discover mode.
  • Figure out what your keyboard’s Bluetooth address is.
  • Register a temporary PIN to be used when pairing the keyboard.
  • Type the PIN in with the keyboard.
  • Attach the device.

In detail:

Enable the Bluetooth system service.

This is simply adding “bluetooth=YES” in your rc.conf and then turning it on with sudo service bluetooth start.

Figure out what your machine’s Bluetooth device node is called.

That’s just a matter of dmesg. We are interested in finding out where a Bluetooth remote device hub (bthub(4)) has set itself up:

$ dmesg | grep bthub
[    15.588860] bthub0 at ubt0 local-bdaddr 48:45:20:52:f6:60

ubt0 is where our Bluetooth remote device hub is located. That’s the part we’re interested in. We’ll make a note of it.

Put your keyboard into discover mode.

In the case of my keyboard from Apple, that means pressing and holding the power button until the status light blinks. When that happens, the keyboard will be discoverable when my laptop’s Bluetooth device tries to discover it (and anything else in the vicinity).

Figure out what your keyboard’s Bluetooth address is.

Apparently, the technical term for “discover all the discoverables” is actually an “inquiry.”

$ btconfig ubt0 inquiry
Device Discovery from device: ubt0 ... 3 responses
  1: bdaddr e4:8b:7f:2e:bb:60
   : name "Apple Keyboard"
   : class [0x002540] Peripheral Keyboard <Limited Discoverable>
   : page scan rep mode 0x01
   : clock offset 31094
   : rssi 0

  2: bdaddr b0:67:2f:00:54:ff
   : name "Audio Device"
   : class [0x080428] HiFi Audio <Capturing>
   : page scan rep mode 0x01
   : clock offset 23232
   : rssi 0

  3: bdaddr 60:6b:bd:c2:89:de
   : name "DTVBluetooth"
   : class [0x08043c] Video Display and Loudspeaker <Capturing>
   : page scan rep mode 0x01
   : clock offset 24943
   : rssi 0

I’m interested in the first device, the keyboard with the BDADDR “e4:8b:7f:2e:bb:60”. At this stage, we can register an alias or “nickname” for this device, so that we don’t have remember the address:

 $ sudo echo e4:8b:7f:2e:bb:60 CharlotteAppleKeyboard >> /etc/bluetooth/hosts

Register a temporary PIN to be used when pairing the keyboard.

The btpin command sets up a number to be typed in. I typed the number on my Bluetooth keyboard and pressed ENTER.

$ btpin -d ubt0 -a CharlotteAppleKeyboard -r -P
PIN: (numbers)
Pairing.. done

Pairing is done. Nice!

Attach the device.

The btdevctl -A command will do that for us.

$ sudo btdevctl -d ubt0 -a CharlotteAppleKeyboard -s HID -A

You can confirm that the device has attached as a keyboard (rather than something else) by inspecting /var/log/messages or dmesg. This is the third keyboard attached to my laptop (my laptop’s own keyboard and a USB keyboard were already attached). So the fact that it’s wskbd2 makes sense:

Oct 27 10:26:53 sakuracity /netbsd: [ 510454.1260370] btkbd0 at bthidev0 reportid 1
Oct 27 10:26:53 sakuracity /netbsd: [ 510454.1260370] wskbd2 at btkbd0 mux 1
Oct 27 10:26:53 sakuracity /netbsd: [ 510454.1260370] wskbd2: connecting to wsdisplay0

At this point you can try typing! That’s what I’m doing right now!

Unfortunately, there are a few shortcomings with this keyboard. For example, The media keys don’t work. Volume up, volume down, you name it. Page up and page down, even. All of these nonworking keys are “function” keys (not F1-F12, but, like, “extended” functionality) and none of them work because every time you press the Fn key, this happens:

[ 512317.5222648] bthidev0: autoconfiguration error: report id 17, len = 1 ignored

Not sure what that’s all about, but I’m willing to dig deeper into it.

Conclusions

So, the instructions here are NOT exactly what is detailed in the NetBSD Guide. I (or someone) should fix that.

Also, I’ve been inspired to write a tool or two which makes this process A LOT simpler. I feel like this should be a task that requires 1 command rather than 3 or 4. I also believe it would be useful to automate maintaining /etc/bluetooth/hosts somehow.

No comments:

Post a Comment