BlueZ勉強中――続・シリアルポートマニアになりたい

先日の日記で、ザウルス同士のBluetoothシリアル接続が出来ないという問題に突き当たっていました。が、ここ数日検索で色々と海外サイトの英文ドキュメントを読み進めていくうちに、少しずつ全容が把握できて来ました。


まず、SDPに反応してくれない件ですが、これは単純にSDPを司るsdpdに対して、シリアルポートプロファイルを登録して無いというだけの話でした。sdpdに対して、プロファイルを登録してサービスを公開するには、


sdptool add --channel=1 SP


という感じで記述すると、Bluetoothチャンネル1でシリアルポートプロファイルのサービスが公開されます。しかし、ここで公開されているサービスにcu/minicomやWindows上のシリアルコンソールで接続しようとしても、やはりタイムアウトが発生してしまいます。この問題に関しては、BlueZ公式サイトFAQにしっかり記載されていました。灯台下暗しと言うか、説明書くらい読めよ自分と言う感じですね_| ̄|○


18. RFCOMM connection using minicom or other tool fails?
In case you have created the RFCOMM link using the "rfcomm bind" function this can be caused by application timeouts. This is not a problem of BlueZ or the RFCOMM implementation but rather a problem with the different nature of a RFCOMM virtual TTY and a physical serial TTY device which is normally used by those applications. Some applications implement a timeout protection function while opening the port. If opening the port takes longer than a certain amount of time then the application fails. A serial TTY will usually only take several microseconds to open while the RFCOMM virtual TTY can take several seconds. Try to create the connection before starting your application. This can be done by the "rfcomm connect" function. The Bluetooth connection will then remain until the rfcomm utility is stopped.


という感じで、つまりは「ケーブル接続(ttySx)による接続を想定したプログラムでは、相手デバイス応答時間を数ミリ秒に設定しているため、Bluetoothによる接続確立まで待ってくれずにタイムアウトを起こすことがある」という事のようです。rfcomm bindコマンドでは、rfcommXに対してあらかじめBTアドレスを割り振る事しかしてくれないので、明示的にrfcomm connectにより接続してから通信を開く必要があるようです。他にも設定が必要な部分もあると思うので、随時テストして項目を追加してみます。