Skip to content

Commit a341e2a

Browse files
author
marcusw
committed
Added experimental mac support, testing needed. Should not break anything on other platforms.
1 parent 5036aa3 commit a341e2a

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

nxt/bluesock.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313
# GNU General Public License for more details.
1414

15-
import bluetooth
15+
try:
16+
import bluetooth
17+
except ImportError:
18+
import lightblueglue as bluetooth
1619
import os
1720
from .brick import Brick
1821

nxt/lightblueglue.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)