# [HOWTO] Bluetooth Keyboard (Apple) and Mouse (Microsoft)

## Spida

Goal:

Using Bluetooth-HID-devices like the  "Apple Wireless Keyboard" or the "Microsoft Intellimouse	Explorer for bluetooth" with a Bluetooth-USB-dongle like the "Anycom BT 220" with Linux

Hardware:

Check this devices-list to see if your bluetooth-adapter is supported and which driver you should use.  There is also the Bluetoothdb from the german ct-magazine with a compatibility database. Both are not complete but provide good hints on how much trouble to expect.

Prerequisites:

You should have a fully working linux base-system 

	(for non-gentooists, that should include:

	- compiler and matching tools

	- configured kernelsources)

You should have a kernel of (at least) version 2.6.7, and know how to configure and compile it.

Kernel:

These are the things that I believe are the minimum required settings for bluetooth-HID-devices to work:

```
General setup  --->

    [*] Support for hot-pluggable devices

Device Drivers  --->

   Networking support  --->

      <*> Bluetooth subsystem support  ---> 

         <*>   L2CAP protocol support

         Bluetooth device drivers  --->

            <*> HCI USB driver 

   Input device support  --->

      <*> Event interface

      [*] Misc

      <*>   User level driver support

   USB support  --->

      <*> Support for Host-side USB 

      [*]   USB device filesystem

      # choose your driver here

      <*>   UHCI HCD (most Intel and VIA) support

      <*>   USB Human Interface Device (full HID) support

      [*] HID input layer support
```

Software:

You need at least bluez-libs bluez-utils bluez-pan and bluez-sdp, all from the net-wireless category. I used the masked version of bluez-libs (2.8 instead of 2.7), and from all other packages the stable versions (bluez-pan-1.1, bluez-sdp-1.5 bluez-utils-2.7)

```
ACCEPT_KEYWORDS="~x86" emerge bluez-libs

emerge bluez-utils bluez-pan bluez-sdp
```

This should get you the relevant programs.

Now we get to the root of all evil: bthid

Lets fetch the fresh source:

```
mkdir /usr/src/bluetooth

cd /usr/src/bluetooth

cvs -d :pserver:anonymous@cvs.bluez.sourceforge.net/cvsroot/bluez login

cvs -d :pserver:anonymous@cvs.bluez.sourceforge.net/cvsroot/bluez co utils2

cvs -d :pserver:anonymous@cvs.bluez.sourceforge.net/cvsroot/bluez co libs2
```

Then compile it

```
cd libs2

./bootstrap && ./configure && make && make install

cd ../utils2

./bootstrap && ./configure && make && make install
```

That should have been all.

Test:

Lets test...

```
/etc/init.d/bluetooth start
```

type

```
hciconfig hci0
```

to check if you bluetooth-controller works. It should give you an output like this:

```
hci0:   Type: USB

BD Address: 01:23:45:67:89:AB  ACL MTU: 339:4  SCO MTU: 64:0

UP RUNNING PSCAN ISCAN

RX bytes:317930 acl:6450 sco:0 events:25059 errors:0

TX bytes:130901 acl:2983 sco:0 commands:8859 errors:0
```

Now switch on some bluetooth-devices and  make them visible so that they can be found. Visibility is often limited in time, so after you have switched your device on and visible, you may have to hurry to find it with 

```
hcitool scan

Scanning ...

11:22:33:44:55:66   Apple Wireless Keyboard

06:05:04:03:02:01   Microsoft Mouse
```

If you find the devices, your hardware is probably working OK.

Now the software:

Your /etc/bluetooth/hcid.conf should look similar to this. Make sure you have auth and encrypt activated for use with keyboards (when they support

it)

```
options {

autoinit yes;

security user;

pairing multi;

pin_helper /etc/bluetooth/pin;

}

device {

name "mybox";

class 0x100;

iscan enable; pscan enable;

lm accept;

lp rswitch,hold,sniff,park;

auth enable;

encrypt enable;

}
```

if you canged anything in /etc/bluetooth/hcid.conf, restart your bluetooth with /etc/init.d/bluetooth restart

now start bthid in daemon-mode

```
bthid --daemon
```

and take a device to connect to

```
bthid --connect  11:22:33:44:55:66
```

a quick glance into the log should tell if it worked

```
Jul 21 02:15:21 mybox hcid[6768]: Bluetooth HCI daemon

Jul 21 02:15:21 mybox hcid[6768]: Starting security manager 0

Jul 21 02:15:21 mybox sdpd[6772]: sdpd v1.5 started

Jul 21 02:15:34 mybox bthid[6779]: Bluetooth HID service started

Jul 21 02:15:36 mybox hcid[6768]: link_key_request (sba=01:23:45:67:89:AB, dba=11:22:33:44:55:66)

Jul 21 02:15:36 mybox hcid[6768]: pin_code_request (sba=01:23:45:67:89:AB, dba=11:22:33:44:55:66)

Jul 21 02:15:42 mybox hcid[6768]: link_key_notify (sba=01:23:45:67:89:AB) Jul 21 02:15:42 mybox hcid[6768]: Replacing link key 01:23:45:67:89:AB 00:0A:95:3A:01:C0

Jul 21 02:15:43 mybox bthid[6779]: Connected: Apple Computer, Inc. Apple Wireless Keyboard (05ac:0209)
```

Here you see that bthid wants a pin on the keyboard ("pin_code_request"). Just enter the pin like it is set in /etc/bluetooth/pin.

```
#!/bin/sh

echo "PIN:1234"
```

On the apple keyboard, use the num-block for this. You will not be prompted for the pin, and it won't show up on the display.

For a mouse, diable auth and enc, at least the ms-mouse doesn't seem to support auth.

Putting it all together:

I have written a small init-script to start bthid

Use 

```
/etc/init.d/bthid start
```

to start bthid. You will be asked to run

/etc/init-d/bthid firsttime, that will run a bluetooth-scan (make sure all devices you want to use are switched on and visible) and write the results to /etc/conf.d/bluetooth_devices the resulting file will have 4 tab-seperated-colums, the first will contain a nickname (to later access the device with that nickname), then the Bluetooth-address, the name the device uses to announce itself, and (if necessary) options like noauth or noenc (comma-seperated). Copy all lines of devices you want to use to /etc/conf.d/bthid_devices

```
/etc/init.d/bthid start
```

will start bluetooth and bthid anytime and connect to all configured

devices

```
/etc/init.d/bthid firsttime
```

will scan for bluetooth-devices and add them to /etc/conf.d/bluetooth_devices

```
/etc/init.d/bthid connect
```

will connect to all configured devices from /etc/conf.d/bthid_devices

my script and the software from bluez-cvs are experimental, use at your own risk and write bug-reports if anything breaks.

The Script

```

#!/sbin/runscript

opts="${opts} firsttime connect"

DEV="hci0"

depend() {

   need bluetooth

}

start() {

   local RETVAL

      ebegin "Starting BtHID"

   [ -x /usr/sbin/bthid ] && \

      /usr/sbin/bthid -i "$DEV" --daemon

   RETVAL=$?

   eend ${RETVAL}

   connect

}

connect() {

   PREVIOUSAUTH=`/usr/sbin/hciconfig "$DEV" | /bin/grep "AUTH" | /bin/wc -l`

   PREVIOUSENC=`/usr/sbin/hciconfig "$DEV" | /bin/grep "ENCRYPT" | /bin/wc -l`

   KNOWNCOUNT=`/bin/wc -l /etc/conf.d/bthid_devices  | /bin/cut -f 1 -d " "`

   if [ 0$KNOWNCOUNT -ge 1 ]; then

      for i in `/bin/seq 1 1 $KNOWNCOUNT`; 

      do

         LINE=`/bin/head -n $i /etc/conf.d/bthid_devices | /usr/bin/tail -n 1  | /bin/sed -e "s/#.*//" | /bin/tr -s "\t" ` 

         if [ "$LINE" ]; then

            ADDRESS=`echo "$LINE" | /bin/cut -f 2`

            DEVICE=`echo "$LINE" | /bin/cut -f 3`

            NICKNAME=`echo "$LINE" | /bin/cut -f 1`

            OPTIONS=`echo "$LINE" | /bin/cut -f 4`

            NOAUTH=0;

            NOENC=0;

            if [ "$OPTIONS" ]; then

               NOAUTH=`echo "$OPTIONS" | grep -i "noauth" | wc -l` 

               NOENC=`echo "$OPTIONS" | grep -i "noenc" | wc -l` 

            fi

            if [ $NOAUTH -eq 1 ]; then

               if [ $NOENC -eq 1 ]; then

                  ebegin "connecting to $NICKNAME ($DEVICE) on $ADDRESS without auth and enc..."

                  /usr/sbin/hciconfig "$DEV" auth disable

                  /usr/sbin/hciconfig "$DEV" encrypt disable

                         /usr/sbin/bthid -i "$DEV" -c "$ADDRESS$"

                  /usr/sbin/hciconfig "$DEV" encrypt enable

                  /usr/sbin/hciconfig "$DEV" auth enable

               else

                  ebegin "connecting to $NICKNAME ($DEVICE) on $ADDRESS without auth..."

                  /usr/sbin/hciconfig "$DEV" auth disable

                         /usr/sbin/bthid -i "$DEV" -c "$ADDRESS$"

                  /usr/sbin/hciconfig "$DEV" auth enable

               fi

            else

               if [ $NOENC -eq 1 ]; then

                  ebegin "connecting to $NICKNAME ($DEVICE) on $ADDRESS without encrypt..."

                  /usr/sbin/hciconfig "$DEV" auth enable

                  /usr/sbin/hciconfig "$DEV" encrypt disable

                         /usr/sbin/bthid -i "$DEV" -c "$ADDRESS$"

                  /usr/sbin/hciconfig "$DEV" encrypt enable

               else

                  ebegin "connecting to $NICKNAME ($DEVICE) on $ADDRESS..."

                  /usr/sbin/hciconfig "$DEV" auth enable

                  /usr/sbin/hciconfig "$DEV" encrypt enable

                         /usr/sbin/bthid -i "$DEV" -c "$ADDRESS$"

               fi

            fi

                 RETVAL=$?

                 eend ${RETVAL}

   

            sleep 1

         fi

      done

   else

      ewarn "There are no bluetooth devices in"

      ewarn "/etc/conf.d/bthid_devices"

      ewarn "run /etc/init/bthid firstime for instructions on how to add them"

   fi

}

firsttime() {

   ebegin "Scanning for bluetooth devices..."

   DEVICESFILEHASHEADER=`/bin/grep -i "Nickname" /etc/conf.d/bluetooth_devices | /bin/grep -i "BTAddress" | /bin/grep -i "Name" | /bin/grep -i "Options" | /bin/wc -l | /bin/cut -f 1 -d " "`

   /bin/mv /etc/conf.d/bluetooth_devices /etc/conf.d/bluetooth_devices.old

   if [ $DEVICESFILEHASHEADER -eq 0 ]; then

      echo "#Nickname   BTAddress      Name         Options" >  /etc/conf.d/bluetooth_devices 

   fi

   /bin/cat /etc/conf.d/bluetooth_devices.old >> /etc/conf.d/bluetooth_devices   

   /usr/bin/hcitool scan | /bin/grep -v "Scanning" | /bin/sed -e "s/^\t/Device\t\t/"  >> /etc/conf.d/bluetooth_devices

   FOUNDCOUNT=`/bin/wc -l /etc/conf.d/bluetooth_devices | /bin/cut -f 1 -d " "`

   if [ 0$FOUNDCOUNT -ge 1 ]; then

      einfo "Found $FOUNDCOUNT Bluetooth devices."

      einfo "Now put the lines from /etc/conf.d/bluetooth_devices"

      einfo "that contain devices you want to use to use into"

      einfo "/etc/conf.d/bthid_devices"

      einfo "You may add options like noauth or noenc, seperated with a \",\""

   else

      ewarn "No bluetooth devices found."

      ewarn "Are you sure you have bluetooth devices nearby that are"

      ewarn "visible for bluetooth scanning?"

   fi

}

status() {

   [ -x /usr/sbin/bthid ] && \

      /usr/sbin/bthid --show

}

stop() {

   local RETVAL

   ebegin "Shutting down BtHID"

   [ -x /usr/sbin/bthid ] && \

      /usr/sbin/bthid --kill

   RETVAL=$?   

   eend ${RETVAL}

}

```

My Config

```
#Nickname   BTAddress      Name         Options

Keyboard   11:22:33:44:55:66   Apple Wireless Keyboard

Mouse   06:05:04:03:02:01   Microsoft Mouse      noauth
```

----------

## cf1

Thanks for the great guide, however Im stuck.

All good up til connecting to the keyboard via bthid

USB dongle working

```

cfworks ~ # hciconfig 

hci0:   Type: USB

        BD Address: 00:xx:xx:xx:xx:01  ACL MTU: 192:8  SCO MTU: 64:8

        UP RUNNING PSCAN ISCAN 

        RX bytes:1143 acl:0 sco:0 events:56 errors:0

        TX bytes:1469 acl:0 sco:0 commands:43 errors:0

```

HCID finds keyboard

```

cfworks ~ # hcitool scan

Scanning ...

        00:xx:xx:xx:xx:02       Apple Wireless Keyboard

```

BTHID starts up ok

```

cfworks ~ # cat /var/log/messages

<snip>

Dec 29 23:29:00 cfworks hcid[11286]: Bluetooth HCI daemon

Dec 29 23:29:00 cfworks hcid[11286]: Starting security manager 0

Dec 29 23:29:00 cfworks sdpd[11291]: Bluetooth SDP daemon

Dec 29 23:29:00 cfworks hidd[11294]: Bluetooth HID daemon

Dec 29 23:29:24 cfworks bthid[11301]: Bluetooth HID service started

<snip>

```

BUT... BTHID connection refuses at atempted connection (no extra output in /var/log/messages)

```

cfworks ~ # bthid --connect 00:xx:xx:xx:02

Can't connect to HID service: Connection refused

```

I imagine this is just some thing simply blocking access to the HCID process, however Ive tried nearly everything I can think of but still cant get it to work.

Please help  :Razz: 

~cf1

----------

