From a341e2a74fd71f2d11bfe3101b03bd5e605e7d6d Mon Sep 17 00:00:00 2001 From: marcusw Date: Thu, 14 Jan 2010 19:14:02 +0000 Subject: [PATCH] Added experimental mac support, testing needed. Should not break anything on other platforms. --- nxt/bluesock.py | 5 ++++- nxt/lightblueglue.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 nxt/lightblueglue.py diff --git a/nxt/bluesock.py b/nxt/bluesock.py index d1f1254..2d8f1bf 100644 --- a/nxt/bluesock.py +++ b/nxt/bluesock.py @@ -12,7 +12,10 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -import bluetooth +try: + import bluetooth +except ImportError: + import lightblueglue as bluetooth import os from .brick import Brick diff --git a/nxt/lightblueglue.py b/nxt/lightblueglue.py new file mode 100644 index 0000000..5037d48 --- /dev/null +++ b/nxt/lightblueglue.py @@ -0,0 +1,53 @@ +# bluetooth.py module -- Glue code from NXT_Python to Lightblue, allowing +# NXT_Python to run on Mac without modification. Supports subset of +# PyBluez/bluetooth.py used by NXT_Python. +# +# Copyright (C) 2007 Simon D. Levy +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +import lightblue + +RFCOMM=11 + +def discover_devices(lookup_names=False): # parameter is ignored + pairs = [] + d = lightblue.finddevices() + for p in d: + h = p[0] + n = p[1] + pairs.append((h, n)) + return pairs + +class BluetoothSocket: + + def __init__(self, proto = RFCOMM, _sock=None): + if _sock is None: + _sock = lightblue.socket(proto) + self._sock = _sock + self._proto = proto + + def connect(self, addrport): + addr, port = addrport + self._sock.connect( (addr, port )) + + def send(self, data): + return self._sock.send( data ) + + def recv(self, numbytes): + return self._sock.recv( numbytes ) + + def close(self): + return self._sock.close() + +class BluetoothError(IOError): + pass +