LoginSignup
32
34

More than 5 years have passed since last update.

RaspberryPi (jessie)とMac間のBluetooth通信[16/10/12時点 最新版]

Last updated at Posted at 2016-10-12

古い記事に翻弄された情弱が送る、まとめ。

目的

 Raspberry Pi2 Model B(jessie)に乗っけているGrovePi+に繋がった温度センサからのデータを
Bluetoothで接続したMacBook AirにSPP通信で送信する。

使用したもの

 Raspberry Pi2 Model B Rasbian jessie 以下RasPi
 MacBook Air OS X Yosemite(10.10.5)  以下Mac
 Planex BT-Micro4
 GrovePi+
 Grove - Starter Kit Temperature Sensor(Analog)

もくじ

  • ※ GUIログイン時を想定しています。
  1. USBドングルが認識されているか確認。
  2. bluetoothモジュールが起動しているか確認。
  3. hciconfigが起動しているか確認。
  4. ペアリング
  5. シリアル通信
  • テスト

はじめに

この手引きはgoogle力のない情弱が必死に集めた情報です。

この記事を元に行った操作に対して一切の責任を持ちません。
自己責任ということでよろしくお願いします。
ご了承ください。

私はこの方法で無事目的を達することができました。
参考程度にご覧下さい。
何処かの誰かの助けになれば幸いです。

今回の内容はGUIログイン状態を想定したものです。

1. USBドングルが認識されているか確認。

lsusb

 - Bus 001 Device 005: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
 - Bus 001 Device 006: ID 046d:c52b Logitech, Inc. Unifying Receiver
 - Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
 - Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
 - Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

2. bluetoothモジュールが起動しているか確認。

/etc/init.d/bluetooth status

 - bluetooth.service - Bluetooth service
 -    Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
 -    Active: active (running) since Tue 2016-10-11 18:15:24 JST; 35min ago
 -      Docs: man:bluetoothd(8)
 -  Main PID: 23440 (bluetoothd)
 -    Status: "Running"
 -    CGroup: /system.slice/bluetooth.service
 -            └─23440 /usr/lib/bluetooth/bluetoothd --compat

3. hciconfigが起動しているか確認。

sudo hciconfig

   - hci0:    Type: BR/EDR  Bus: USB
   -     BD Address: 00:1B:DC:06:32:C6  ACL MTU: 310:10  SCO MTU: 64:8
   -     DOWN
   -     RX bytes:82948893 acl:272264 sco:0 events:755 errors:0
   -     TX bytes:14924 acl:277 sco:0 commands:188 errors:0

DOWNなら、

sudo hcicofig hic0 up

 - hci0:    Type: BR/EDR  Bus: USB
 -     BD Address: 00:1B:DC:06:32:C6  ACL MTU: 310:10  SCO MTU: 64:8
 -     UP RUNNING PSCAN ISCAN
 -     RX bytes:82950062 acl:272264 sco:0 events:796 errors:0
 -     TX bytes:15642 acl:277 sco:0 commands:226 errors:0

で起動。

4. ペアリング

sudo hciconfig hci0 piscanで発見されるモードに。
その後、Mac側でペアリングを行う。

もしくは
bluetoothctlで対話型の操作に切り替える。
pair XX:XX…でペアリング要請。
connect XX:XX…で接続。
trust XX:XX…で次回時、自動で接続する。

※「bluez-util」というパッケージは、jessieのバージョンでは存在しません!!!!

古い記事ではこのbluez-utilをインストールしbluetooth-agentなどのコマンドを使用する方法をよく見かけますが、「bluez-util」というパッケージは、jessieのバージョンでは存在しません!!!!
深追いせず、bluetoothctlを使用することをお勧めします。

5. シリアル通信

SDPにSPPを追加する。

sdptool browse localで登録されているサービスを確認する。

・Failed to connect to SDP server on FF:FF:FF:00:00:00: No such file or directoryとエラーが出た場合。

sudo nano /etc/systemd/system/dbus-org.bluez.serviceで以下を書き換える。

ExecStart=/usr/lib/bluetooth/bluetoothd部分を。
ExecStart=/usr/lib/bluetooth/bluetoothd --compatに書き換える。

書き換え後、
sudo systemctl daemon-reload
sudo systemctl restart bluetoothでBluetooth関連をrestart。
sudo chmod 777 /var/run/sdpパーミッションを変更。

これでSDPの起動が確認できる。
(bluetooth関連をリスタートしているのでもう一度諸々を立ち上げないといけない。)

・サービス内にserialportがない場合。

sdptool add --channel=22 SPで追加する。

・再度確認。

spdtool browse local

                     ~略~
            - Service Name: Serial Port
            - Service Description: COM Port
            - Service Provider: BlueZ
            - Service RecHandle: 0x10008
            - Service Class ID List:
            -   "Serial Port" (0x1101)
            - Protocol Descriptor List:
            -   "L2CAP" (0x0100)
            -   "RFCOMM" (0x0003)
            -     Channel: 22
            - Language Base Attr List:
            -   code_ISO639: 0x656e
            -   encoding:    0x6a
            -   base_offset: 0x100
            - Profile Descriptor List:
            -   "Serial Port" (0x1101)
            -     Version: 0x0100

・RasPi側の待ち受け状態。

下記コマンドで待ち受け状態に。

sudo rfcomm listen /dev/rfcomm0 22

  - Waiting for connection on channel 22

接続後の表示。

sudo rfcomm listen /dev/rfcomm0 22

  - Waiting for connection on channel 22
  - Connection from XX:XX:XX... to /dev/rfcomm0
        - Press CTRL-C for hangup

・Mac側からの接続

ポートの確認。

 ls -l /dev/tty.*

   - crw-rw-rw-  1 root  wheel   17,   0 10 11 18:01 /dev/tty.Bluetooth-Incoming-Port
   - crw-rw-rw-  1 root  wheel   17,   2 10  8 16:01 /dev/tty.Bluetooth-Modem
   - crw-rw-rw-  1 root  wheel   17,  22 10 12 08:17 /dev/tty.pi-SerialPort

screen /dev/tty.pi-SerialPortで接続。

・テスト

・接続のテスト

RasPi側でもう一枚terminalを開き接続の確認を行う。
sudo echo “hello” > /dev/rfcomm0
Mac側のterminalに hello と表示がされていれば成功。

CUI非ログイン状態での通信についても今後書く予定です。

予定は未定です...。

32
34
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
34