Bluetooth keyboard auto connect

出自 flip the world
於 2018年8月23日 (四) 10:34 由 Tommy (對話 | 貢獻) 所做的修訂 (已建立頁面,內容為 "== Find keyboard & device MAC == <pre> #hcitool scan Scanning ... 23:E4:87:4C:B3:A1 Wireless Keyboard # hcitool dev Devices: hci0 06:05:04:03:02:01 <…")
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋

Find keyboard & device MAC

#hcitool scan
Scanning ...
    23:E4:87:4C:B3:A1       Wireless Keyboard

# hcitool dev
Devices:
   hci0   06:05:04:03:02:01

Paring

#bluez-simple-agent hci0 23:E4:87:4C:B3:A1

If everything goes well, you will be prompted to type a passkey with your keyboard:
DisplayPasskey (/org/bluez/14656/hci0/dev_23_E4_87_4C_B3_A1, 936319)
Your passkey is the number at the end of the line. Don’t forget to press ‘Enter’ after typing the passcode! Now your device is paired with the Raspberry Pi.

Trusting & Connecting

The final stage is making sure that the raspberry will automatically connect to the keyboard when it is turned on. This is called ‘trusting’ and is invoked by this command:
#bluez-test-device trusted 23:E4:87:4C:B3:A1 yes

After trusing it, all you need to do is connect to it:
#bluez-test-input connect 23:E4:87:4C:B3:A1 yes

Automative connecting with systemd

/etc/btkbd.conf

# Config file for btkbd.service
# change when required (e.g. keyboard hardware changes, more hci devices are connected)
BTKBDMAC = 6C:5D:63:22:0B:05
HCIDEVICE = 00:1A:7D:DA:71:13

/etc/systemd/system/btkbd.service

[Unit]
Description=systemd Unit to automatically start a Bluetooth keyboard
Documentation=https://wiki.archlinux.org/index.php/Bluetooth_Keyboard
Requires=dbus-org.bluez.service
After=dbus-bluez.org.service
ConditionPathExists=/etc/btkbd.conf
ConditionPathExists=/usr/bin/hcitool
ConditionPathExists=/usr/sbin/hciconfig

[Service]
Type=oneshot
EnvironmentFile=/etc/btkbd.conf
ExecStart=-/usr/sbin/hciconfig ${HCIDEVICE} up
# ignore errors on connect, spurious problems with bt? so start next command with -
ExecStart=-/usr/bin/hcitool cc ${BTKBDMAC}

reference