Skip to content

Commit f124823

Browse files
committed
now discovering address of mindwave mobile
1 parent e5ed26b commit f124823

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ Requirements:
66
Usage in python:
77

88
```python
9+
from mindwavemobile.MindwaveDataPointReader import MindwaveDataPointReader
910
mindwaveDataPointReader = MindwaveDataPointReader()
1011
# connect to the mindwave mobile headset...
1112
mindwaveDataPointReader.start()
1213
# read one data point, data point types are specified in MindwaveDataPoints.py'
1314
dataPoint = mindwaveDataPointReader.readNextDataPoint()
15+
print dataPoint
1416
```

example/read_mindwave_mobile.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
import bluetooth
33
from mindwavemobile.MindwaveDataPoints import RawDataPoint
44
from mindwavemobile.MindwaveDataPointReader import MindwaveDataPointReader
5-
5+
import textwrap
66

77
if __name__ == '__main__':
88
mindwaveDataPointReader = MindwaveDataPointReader()
99
mindwaveDataPointReader.start()
10-
11-
while(True):
12-
dataPoint = mindwaveDataPointReader.readNextDataPoint()
13-
if (not dataPoint.__class__ is RawDataPoint):
14-
print dataPoint
15-
16-
10+
if (mindwaveDataPointReader.isConnected()):
11+
while(True):
12+
dataPoint = mindwaveDataPointReader.readNextDataPoint()
13+
if (not dataPoint.__class__ is RawDataPoint):
14+
print dataPoint
15+
else:
16+
print(textwrap.dedent("""\
17+
Exiting because the program could not connect
18+
to the Mindwave Mobile device.""").replace("\n", " "))
19+

mindwavemobile/MindwaveDataPointReader.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ def __init__(self):
1111

1212
def start(self):
1313
self._mindwaveMobileRawReader.connectToMindWaveMobile()
14-
14+
15+
def isConnected(self):
16+
return self._mindwaveMobileRawReader.isConnected()
17+
1518
def readNextDataPoint(self):
1619
if (not self._moreDataPointsInQueue()):
1720
self._putNextDataPointsInQueue()
@@ -76,4 +79,4 @@ def _readDataPointsFromPayload(self, payloadBytes):
7679

7780

7881

79-
82+

mindwavemobile/MindwaveMobileRawReader.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
import bluetooth
22
import time
3+
import textwrap
34

45

56
class MindwaveMobileRawReader:
67
START_OF_PACKET_BYTE = 0xaa;
78
def __init__(self):
89
self._buffer = [];
910
self._bufferPosition = 0;
11+
self._isConnected = False;
1012

1113
def connectToMindWaveMobile(self):
12-
# connecting via bluetooth RFCOMM
14+
# First discover mindwave mobile address, then connect
15+
# headset address of me was'9C:B7:0D:72:CD:02';
16+
# not sure if it really can be different?
17+
# now discovering address because of https://github.com/robintibor/python-mindwave-mobile/issues/4
18+
mindwaveMobileAddress = self._findMindwaveMobileAddress()
19+
if (mindwaveMobileAddress is not None):
20+
print ("Discovered Mindwave Mobile...")
21+
self._connectToAddress(mindwaveMobileAddress)
22+
else:
23+
self._printErrorDiscoveryMessage()
24+
25+
def _findMindwaveMobileAddress(self):
26+
nearby_devices = bluetooth.discover_devices(lookup_names = True)
27+
for address, name in nearby_devices:
28+
if (name == "MindWave Mobile"):
29+
return address
30+
return None
31+
32+
def _connectToAddress(self, mindwaveMobileAddress):
1333
self.mindwaveMobileSocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
14-
mindwaveMobileAddress = '9C:B7:0D:72:CD:02';
15-
while(True):
34+
while (not self._isConnected):
1635
try:
17-
self.mindwaveMobileSocket.connect((mindwaveMobileAddress, 1))
18-
return;
36+
self.mindwaveMobileSocket.connect(
37+
(mindwaveMobileAddress, 1))
38+
self._isConnected = True
1939
except bluetooth.btcommon.BluetoothError as error:
2040
print "Could not connect: ", error, "; Retrying in 5s..."
2141
time.sleep(5)
22-
42+
43+
44+
def isConnected(self):
45+
return self._isConnected
46+
47+
def _printErrorDiscoveryMessage(self):
48+
print(textwrap.dedent("""\
49+
Could not discover Mindwave Mobile. Please make sure the
50+
Mindwave Mobile device is in pairing mode and your computer
51+
has bluetooth enabled.""").replace("\n", " "))
52+
2353
def _readMoreBytesIntoBuffer(self, amountOfBytes):
2454
newBytes = self._readBytesFromMindwaveMobile(amountOfBytes)
2555
self._buffer += newBytes
@@ -67,4 +97,4 @@ def clearAlreadyReadBuffer(self):
6797
def _bufferSize(self):
6898
return len(self._buffer);
6999

70-
#------------------------------------------------------------------------------
100+
#------------------------------------------------------------------------------

0 commit comments

Comments
 (0)