|
12 | 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 | 13 | # GNU General Public License for more details.
|
14 | 14 |
|
15 |
| -try: |
16 |
| - import bluetooth |
17 |
| -except ImportError: |
18 |
| - import lightblueglue as bluetooth |
| 15 | +import bluetooth |
19 | 16 | import os
|
20 |
| -from .brick import Brick |
| 17 | +from nxt.brick import Brick |
21 | 18 |
|
22 | 19 | class BlueSock(object):
|
23 | 20 |
|
24 |
| - bsize = 118 # Bluetooth socket block size |
25 |
| - PORT = 1 # Standard NXT rfcomm port |
| 21 | + bsize = 118 # Bluetooth socket block size |
| 22 | + PORT = 1 # Standard NXT rfcomm port |
26 | 23 |
|
27 |
| - def __init__(self, host): |
28 |
| - self.host = host |
29 |
| - self.sock = None |
30 |
| - self.debug = False |
| 24 | + def __init__(self, host): |
| 25 | + self.host = host |
| 26 | + self.sock = None |
| 27 | + self.debug = False |
31 | 28 |
|
32 |
| - def __str__(self): |
33 |
| - return 'Bluetooth (%s)' % self.host |
| 29 | + def __str__(self): |
| 30 | + return 'Bluetooth (%s)' % self.host |
34 | 31 |
|
35 |
| - def connect(self): |
36 |
| - if self.debug: |
37 |
| - print 'Connecting via Bluetooth...' |
38 |
| - sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) |
39 |
| - sock.connect((self.host, BlueSock.PORT)) |
40 |
| - self.sock = sock |
41 |
| - if self.debug: |
42 |
| - print 'Connected.' |
43 |
| - return Brick(self) |
| 32 | + def connect(self): |
| 33 | + if self.debug: |
| 34 | + print 'Connecting via Bluetooth...' |
| 35 | + sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) |
| 36 | + sock.connect((self.host, BlueSock.PORT)) |
| 37 | + self.sock = sock |
| 38 | + if self.debug: |
| 39 | + print 'Connected.' |
| 40 | + return Brick(self) |
44 | 41 |
|
45 |
| - def close(self): |
46 |
| - if self.debug: |
47 |
| - print 'Closing Bluetooth connection...' |
48 |
| - self.sock.close() |
49 |
| - if self.debug: |
50 |
| - print 'Bluetooth connection closed.' |
| 42 | + def close(self): |
| 43 | + if self.debug: |
| 44 | + print 'Closing Bluetooth connection...' |
| 45 | + self.sock.close() |
| 46 | + if self.debug: |
| 47 | + print 'Bluetooth connection closed.' |
51 | 48 |
|
52 |
| - def send(self, data): |
53 |
| - if self.debug: |
54 |
| - print 'Send:', |
55 |
| - print ':'.join('%02x' % ord(c) for c in data) |
56 |
| - l0 = len(data) & 0xFF |
57 |
| - l1 = (len(data) >> 8) & 0xFF |
58 |
| - d = chr(l0) + chr(l1) + data |
59 |
| - self.sock.send(d) |
| 49 | + def send(self, data): |
| 50 | + if self.debug: |
| 51 | + print 'Send:', |
| 52 | + print ':'.join('%02x' % ord(c) for c in data) |
| 53 | + l0 = len(data) & 0xFF |
| 54 | + l1 = (len(data) >> 8) & 0xFF |
| 55 | + d = chr(l0) + chr(l1) + data |
| 56 | + self.sock.send(d) |
60 | 57 |
|
61 |
| - def recv(self): |
62 |
| - data = self.sock.recv(2) |
63 |
| - l0 = ord(data[0]) |
64 |
| - l1 = ord(data[1]) |
65 |
| - plen = l0 + (l1 << 8) |
66 |
| - data = self.sock.recv(plen) |
67 |
| - if self.debug: |
68 |
| - print 'Recv:', |
69 |
| - print ':'.join('%02x' % ord(c) for c in data) |
70 |
| - return data |
| 58 | + def recv(self): |
| 59 | + data = self.sock.recv(2) |
| 60 | + l0 = ord(data[0]) |
| 61 | + l1 = ord(data[1]) |
| 62 | + plen = l0 + (l1 << 8) |
| 63 | + data = self.sock.recv(plen) |
| 64 | + if self.debug: |
| 65 | + print 'Recv:', |
| 66 | + print ':'.join('%02x' % ord(c) for c in data) |
| 67 | + return data |
71 | 68 |
|
72 | 69 | def _check_brick(arg, value):
|
73 |
| - return arg is None or arg == value |
| 70 | + return arg is None or arg == value |
74 | 71 |
|
75 | 72 | def find_bricks(host=None, name=None):
|
76 |
| - for h, n in bluetooth.discover_devices(lookup_names=True): |
77 |
| - if _check_brick(host, h) and _check_brick(name, n): |
78 |
| - yield BlueSock(h) |
| 73 | + for h, n in bluetooth.discover_devices(lookup_names=True): |
| 74 | + if _check_brick(host, h) and _check_brick(name, n): |
| 75 | + yield BlueSock(h) |
0 commit comments