有馬総一郎のブログ

(彼氏の事情)

2019年06月03日 00:52:59 JST - 3 minute read - Comments - Linux

Ubuntuサーバー端末にCLIでbluetoothキーボードを接続する

普段、キーボードに接続していないUbuntuサーバーの端末、それでも何かトラブルがあったときはキーボードに接続して直接操作する時がある。

BOXNUCPPYHは、なんとbluetooth機能がついてるということで、bluetoothキーボードと接続してみた。 Linux PCにBluetooth機器を接続する - Qiitaを参考に挑戦。

arimasou16@ubuntu:~$ bluetoothctl
[bluetooth]#

早速、bluetoothctlしても、なにも動かない。Ctrl+cで抜けるしかない。デバイスが認識しているかの確認をしようとhcitoolしても駄目。

arimasou16@ubuntu:~$ hcitool -a
hcitool: invalid option -- 'a'
hcitool - HCI Tool ver 5.37
Usage:
        hcitool [options] <command> [command parameters]
Options:

それぞれsudoをつけないと駄目。sudo bluetoothctlsudo hciconfig -aなら上手く行く。

arimasou16@ubuntu:~$ sudo bluetoothctl
[NEW] Controller E0:94:67:8C:FC:4D ubuntu [default]
[bluetooth]# scan on
Failed to start discovery: org.bluez.Error.NotReady

scan onをして、Failed to start discovery: org.bluez.Error.NotReadyとなるのはhci0がupされていないらしい。sudo hciconfig -aをする。

arimasou16@ubuntu:~$ sudo hciconfig -a
hci0:   Type: BR/EDR  Bus: USB
        BD Address: EE:99:66:88:CC:44  ACL MTU: 1021:5  SCO MTU: 96:6
        DOWN
        RX bytes:1799 acl:0 sco:0 events:101 errors:0
        TX bytes:1185 acl:0 sco:0 commands:85 errors:0
        Features: 0xff 0xfe 0x0f 0xfe 0xdb 0xff 0x7b 0x87
        Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
        Link policy: RSWITCH HOLD SNIFF
        Link mode: SLAVE ACCEPT

文字どおり DOWNしているのでsudo hciconfig hci0 upでupする。

arimasou16@ubuntu:~$ sudo hciconfig hci0 up
arimasou16@ubuntu:~$ sudo hciconfig -a
hci0:   Type: BR/EDR  Bus: USB
        BD Address: EE:99:66:88:CC:44  ACL MTU: 1021:5  SCO MTU: 96:6
        UP RUNNING
        RX bytes:2403 acl:0 sco:0 events:136 errors:0
        TX bytes:1883 acl:0 sco:0 commands:120 errors:0
        Features: 0xff 0xfe 0x0f 0xfe 0xdb 0xff 0x7b 0x87
        Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
        Link policy: RSWITCH HOLD SNIFF
        Link mode: SLAVE ACCEPT
        Name: 'ubuntu'
        Class: 0x000104
        Service Classes: Unspecified
        Device Class: Computer, Desktop workstation
        HCI Version: 4.2 (0x8)  Revision: 0x1000
        LMP Version: 4.2 (0x8)  Subversion: 0x1000
        Manufacturer: Intel Corp. (2)

UP RUNINGとなった。

いよいよ接続、まずはパワーオンpower on(する必要あるのかね?)。そして、scan on

[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
[CHG] Controller E0:94:67:8C:FC:4D Discovering: yes
[NEW] Device 12:34:56:78:90:AB 22-11-00-99-88-77
[CHG] Device 12:34:56:78:90:AB RSSI: -80
[CHG] Device 12:34:56:78:90:AB RSSI: -66
[NEW] Device 65:43:21:09:AB:CD 65-43-21-09-AB-CD
[CHG] Device 12:34:56:78:90:AB LegacyPairing: no
[CHG] Device 12:34:56:78:90:AB Name: Bluetooth 3.0 Keyboard
[CHG] Device 12:34:56:78:90:AB Alias: Bluetooth 3.0 Keyboard
[CHG] Device 12:34:56:78:90:AB LegacyPairing: yes
[CHG] Device 12:34:56:78:90:AB RSSI: -58
[CHG] Device 12:34:56:78:90:AB RSSI: -69
[CHG] Device 12:34:56:78:90:AB RSSI: -61

ペアリングpair MACアドレスする。でもInvalid commandと返される。

[bluetooth]# piar 12:34:56:78:90:AB
Invalid command
[CHG] Device 12:34:56:78:90:AB RSSI: -77
[CHG] Device 12:34:56:78:90:AB RSSI: -69
[CHG] Device 12:34:56:78:90:AB RSSI: -79
[CHG] Device 12:34:56:78:90:AB RSSI: -71
[CHG] Device 12:34:56:78:90:AB RSSI: -79
[CHG] Device 12:34:56:78:90:AB RSSI: -69
[CHG] Device 12:34:56:78:90:AB RSSI: -85
[CHG] Device 12:34:56:78:90:AB RSSI: -76
[CHG] Device 12:34:56:78:90:AB RSSI: -68
[bluetooth]# devices
Device 12:34:56:78:90:AB Bluetooth 3.0 Keyboard
Device 65:43:21:09:AB:CD 6A-5A-F9-07-EE-8F

信用登録trust MACアドレスする。

[bluetooth]# trust 12:34:56:78:90:AB
[CHG] Device 12:34:56:78:90:AB Trusted: yes
Changing 12:34:56:78:90:AB trust succeeded

今度こそ、ペアリングしてみる。

[bluetooth]# pair 12:34:56:78:90:AB
Attempting to pair with 12:34:56:78:90:AB
[CHG] Device 12:34:56:78:90:AB Connected: yes
[CHG] Device 12:34:56:78:90:AB Modalias: usb:v04E8p7021d0001
[CHG] Device 12:34:56:78:90:AB UUIDs: 00001000-0000-1000-9999-8888ffff4444
[CHG] Device 12:34:56:78:90:AB UUIDs: 00001234-0000-1000-9999-8888ffff4444
[CHG] Device 12:34:56:78:90:AB UUIDs: 00001200-0000-1000-9999-8888ffff4444
[CHG] Device 12:34:56:78:90:AB Paired: yes
Pairing successful
[CHG] Device 12:34:56:78:90:AB Connected: no

成功したので、接続connect MACアドレスする。

[bluetooth]# connect 12:34:56:78:90:AB
Attempting to connect to 12:34:56:78:90:AB
[CHG] Device 12:34:56:78:90:AB Connected: yes
Connection successful

やったー。初めは上手く行かなったりしたので、何度か試行してみる必要あるかも。まあ、ここらへんはGUIでも一緒ですね。

Attempting to connect to 00:1D:43:6D:03:26
Failed to connect: org.bluez.Error.Failed