Skip to content
Permalink
Browse files

ev3: readd bluetooth support

Now that we've got python3-bluez, revive the code using it.
  • Loading branch information...
ensonic committed Oct 4, 2016
1 parent 9671c8e commit 1cb32e3656301963f6550194b8ee43d23e51f0f2
Showing with 33 additions and 55 deletions.
  1. +1 −0 README.md
  2. +2 −2 debian/control
  3. +29 −52 roberta/ev3.py
  4. +1 −1 setup.py
@@ -31,6 +31,7 @@ automatically, but one needs to reconnect to the Open Roberta server again.
# development #
## prerequisites ##
python3-ev3dev
python3-bluez
python3-dbus
python3-gi

@@ -12,8 +12,8 @@ Vcs-Git: git://github.com/OpenRoberta/robertalab-ev3dev.git

Package: openrobertalab
Architecture: all
Depends: ${misc:Depends}, ${python3:Depends}, systemd, python3-dbus,
python3-ev3dev python3-gi
Depends: ${misc:Depends}, ${python3:Depends}, systemd, python3-bluez,
python3-dbus, python3-ev3dev, python3-gi
Enhances: brickman
Description: lab.open-roberta.org connector for ev3dev.org
Open-roberta.org is a web platform for learning how to program robots. This
@@ -1,12 +1,12 @@

from PIL import Image, ImageFont
import bluetooth
from bluetooth import BluetoothSocket
import dbus
import glob
import logging
import math
import os
import re
import socket
import time

from ev3dev import auto as ev3dev
@@ -15,27 +15,6 @@
logger = logging.getLogger('roberta.ev3')


class Bluetooth:

@staticmethod
def getAddr(self, iface):
try:
with open('/sys/class/bluetooth/%s/address' % iface, 'r') as f:
mac = f.read()
except (AttributeError, OSError):
logger.warning('no bluetooth device %s found', iface)
mac = None
return mac

@staticmethod
def isValidAddr(self, mac):
if not mac:
return False
if not re.match(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', mac):
return False
return True


class Hal(object):

# usedSensors is unused, the code-generator for lab.openroberta > 1.4 wont
@@ -575,23 +554,15 @@ def getMotorTachoValue(self, actorPort, mode):
def establishConnectionTo(self, host):
# host can also be a name, resolving it is slow though and requires the
# device to be visible
if not Bluetooth.isValidAddr(host):
# FIXME: there is no python3-bluez (for debian-jessie)
# https://github.com/karulis/pybluez/blob/master/bluetooth/bluez.py#L23
# maybe we can use DBus:
# https://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/
# DiscoverDevices, ResolveHostName
#
# nearby_devices = bluetooth.discover_devices()
# for bdaddr in nearby_devices:
# if host == bluetooth.lookup_name(bdaddr):
# host = bdaddr
# break
logger.warning('no bluetooth discovery available')
return -1
if Bluetooth.isValidAddr(host):
con = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
con.connect((host, 0))
if not bluetooth.is_valid_address(host):
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
if host == bluetooth.lookup_name(bdaddr):
host = bdaddr
break
if bluetooth.is_valid_address(host):
con = BluetoothSocket(bluetooth.RFCOMM)
con.connect((host, 1)) # 0 is channel
self.bt_connections.append(con)
return len(self.bt_connections) - 1
else:
@@ -610,12 +581,9 @@ def waitForConnection(self):
props.Set('org.bluez.Adapter1', 'Discoverable', True)

if not self.bt_server:
mac = Bluetooth.getAddr('hci0')
if Bluetooth.isValidAddr(mac):
con = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
con.bind((mac, 0))
con.listen(1)
self.bt_server = con
self.bt_server = BluetoothSocket(bluetooth.RFCOMM)
self.bt_server.bind(("", bluetooth.PORT_ANY))
self.bt_server.listen(1)

(con, info) = self.bt_server.accept()
self.bt_connections.append(con)
@@ -624,13 +592,22 @@ def waitForConnection(self):
def readMessage(self, con_ix):
message = "NO MESSAGE"
if con_ix < len(self.bt_connections) and self.bt_connections[con_ix]:
logger.debug('reading msg')
message = self.bt_connections[con_ix].recv(1024)
logger.debug('received msg [%s]' % message)
try:
logger.debug('reading msg')
# TODO(ensonic): how much do we actually expect
message = self.bt_connections[con_ix].recv(1024)
logger.debug('received msg [%s]' % message)
except bluetooth.btcommon.BluetoothError:
logger.exception("Bluetooth error")
self.bt_connections[con_ix] = None
return message

def sendMessage(self, con_ix, message):
if con_ix < len(self.bt_connections) and self.bt_connections[con_ix]:
logger.debug('sending msg [%s]' % message)
self.bt_connection[con_ix].send(message)
logger.debug('sent msg')
try:
logger.debug('sending msg [%s]' % message)
self.bt_connections[con_ix].send(message)
logger.debug('sent msg')
except bluetooth.btcommon.BluetoothError:
logger.exception("Bluetooth error")
self.bt_connections[con_ix] = None
@@ -29,6 +29,6 @@
scripts=['openrobertalab'],
packages=['roberta'],
package_data={'roberta': ['ter-*.p??']},
# other deps: apt-get-install python3-dbus python3-ev3dev python3-gi
# other deps: apt-get-install python3-bluez python3-dbus python3-ev3dev python3-gi
# install_requires=['python3-ev3dev']
)

0 comments on commit 1cb32e3

Please sign in to comment.
You can’t perform that action at this time.