|
| 1 | +# bluetooth.py module -- Glue code from NXT_Python to Lightblue, allowing |
| 2 | +# NXT_Python to run on Mac without modification. Supports subset of |
| 3 | +# PyBluez/bluetooth.py used by NXT_Python. |
| 4 | +# |
| 5 | +# Copyright (C) 2007 Simon D. Levy |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation; either version 2 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | + |
| 17 | +import lightblue |
| 18 | + |
| 19 | +RFCOMM=11 |
| 20 | + |
| 21 | +def discover_devices(lookup_names=False): # parameter is ignored |
| 22 | + pairs = [] |
| 23 | + d = lightblue.finddevices() |
| 24 | + for p in d: |
| 25 | + h = p[0] |
| 26 | + n = p[1] |
| 27 | + pairs.append((h, n)) |
| 28 | + return pairs |
| 29 | + |
| 30 | +class BluetoothSocket: |
| 31 | + |
| 32 | + def __init__(self, proto = RFCOMM, _sock=None): |
| 33 | + if _sock is None: |
| 34 | + _sock = lightblue.socket(proto) |
| 35 | + self._sock = _sock |
| 36 | + self._proto = proto |
| 37 | + |
| 38 | + def connect(self, addrport): |
| 39 | + addr, port = addrport |
| 40 | + self._sock.connect( (addr, port )) |
| 41 | + |
| 42 | + def send(self, data): |
| 43 | + return self._sock.send( data ) |
| 44 | + |
| 45 | + def recv(self, numbytes): |
| 46 | + return self._sock.recv( numbytes ) |
| 47 | + |
| 48 | + def close(self): |
| 49 | + return self._sock.close() |
| 50 | + |
| 51 | +class BluetoothError(IOError): |
| 52 | + pass |
| 53 | + |
0 commit comments