日本ANDROIDの会茨城支部
第11回勉強会
Raspberry Pi+AndroidでBluetooth通信
@morinezumiiii
手順
Raspberry Pi側 デバイス設定
# pacman -S bluez bluez-utils
# gpasswd -a morinezumiiii lp
# systemctl start bluetooth
# systemctl enable bluetooth
# nano /usr/lib/systemd/system/bluetooth.service
-- ExecStart=/usr/lib/bluetooth/bluetoothd
++ ExecStart=/usr/lib/bluetooth/bluetoothd -C
$ hciconfig --all
# hciconfig hci1 up
# hciconfig hci1 piscan
# sdptool browse local
$ sdptool add --channel=22 SP
$ sudo rfcomm listen /dev/rfcomm0 22
SPPサーバ
Pythonで書きます
→ と思ったらPython3.3以上ならPython Sockets(標準ライブラリ)でいけたの
でPyBluezいらなかった
参考記事
# pacman -S python python-pip
# pip install pybluez
http://blog.kevindoran.co/bluetooth-programming-with-python-3/
SPPサーバ(コード)
import socket
hostMACAddress = '00:1f:e1:dd:08:3d' # BluetoothアダプタのMACアドレス.
port = 22
backlog = 1
size = 1024
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((hostMACAddress,port))
s.listen(backlog)
try:
client, address = s.accept()
while 1:
data = client.recv(size)
if data:
print(data)
client.send(data)
except:
print("Closing socket")
client.close()
s.close()
ANDROID側
適当にBluetoothなアプリ落として使いました
(シリアル接続可能なものがいい)
実演
今回はRaspberry Pi Model A+にプラネックスのBluetoothアダプタ挿して使用
SPPプロファイルに対応していればOK
Logitecおすすめ
結果
Raspberry PiならUSB接続可能なので、Bluetoothシリアルモジュール(基板)買
わなくても通信できた。
最近のLinuxと最近のPythonのおかげで思った以上にさっくり出来てしまっ
た。
課題
今回のhciconfigとかその辺りは、王道的な手法なので、BLEまわりもう少し勉
強したい。

Morinezumiiii 20150425-bluetooth