4

I want to communicate with a python application on my raspberry pi via bluetooth from my android phone (currently using BlueTerm).

I'm trying to get the following 'Hello World' script working on my raspberry pi:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pty
import uuid    
import bluetooth

def main():
    server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    server_sock.bind(('', bluetooth.PORT_ANY))
    server_sock.listen(1)

    port = server_sock.getsockname()[1]
    random_uuid = str(uuid.uuid4())

    bluetooth.advertise_service(server_sock,
        'ShellServer',
        service_id=random_uuid,
        service_classes=[random_uuid, bluetooth.SERIAL_PORT_CLASS],
        profiles=[bluetooth.SERIAL_PORT_PROFILE]
    )

    print("Waiting for connection on RFCOMM channel {0}".format(port))

    while True:
        try:
            client_sock, (client_addr, client_ch) = server_sock.accept()
        except KeyboardInterrupt:
            break
        print("Accepted connection from {0}".format(client_addr))
        client_sock.send("Hello World!")
        client_sock.close()

    print('Shutting down the server socket...')
    server_sock.close()

if __name__ == '__main__':
    main()

(code based on https://warroom.securestate.com/index.php/spawning-shells-over-bluetooth/)

I pair the device following tutorials on the web:

pi@raspberrypi ~ $ sudo hciconfig hci0 piscan pi@raspberrypi ~ $ sudo bluetooth-agent 1234 Pincode request for device /org/bluez/2152/hci0/dev_[...]

Pairing seems to work (device shows up on 'sudo bluez-test-device list'). On an other terminal i start the script:

pi@raspberrypi /usr/local/bin $ sudo python bluetest.py
Waiting for connection on RFCOMM channel 1

and i try to connect using BlueTerm, but this just gives me the message "Unable to connect device". The script itself is silent; server_sock.accept() never returns.

The irritating part is: the exact same setup (same script, same phone, same paring procedure, same bluetooth-adapter) works as expected on my desktop machine (Ubuntu 14.04).

/etc/bluetooth/main.conf and /etc/bluetooth/rfcomm.conf are identical on both machines.

More info on the pi:

pi@raspberrypi ~ $ cat /etc/*release
PRETTY_NAME="Raspbian GNU/Linux 7 (wheezy)"
NAME="Raspbian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=raspbian
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
pi@raspberrypi ~ $ uname -a
Linux raspberrypi 3.18.11+ #781 PREEMPT Tue Apr 21 18:02:18 BST 2015 armv6l GNU/
Linux
pi@raspberrypi ~ $ hcitool --version
hcitool: unrecognized option '--version'
hcitool - HCI Tool ver 4.99
Usage:
 [...]

pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 04d9:1602 Holtek Semiconductor, Inc.
Bus 001 Device 005: ID 05e3:0606 Genesys Logic, Inc. USB 2.0 Hub / D-Link DUB-H4 USB 2.0 Hub
Bus 001 Device 006: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

pi@raspberrypi /usr/local/bin $ aptitude versions bluetooth bluez-utils bluez-compat
Package bluetooth:
i   4.99-2                                                                                oldstable                                                         500

Package bluez-compat:
i   4.99-2                                                                                oldstable                                                         500

Package bluez-utils:
p   4.99-2                                                                                oldstable                                                         500

[...]

The desktop machine is a pretty standard Ubuntu 14.04.

What could be possible reasons why the script works exactly as expected on the desktop system and not on the pi?

This is the first time i work with bluetooth and rfcomm, and i have run out of ideas how to debug this. I would be very grateful for help.

0 Answers0