<div dir="ltr"><div class="gmail_quote"><div>David,</div><div><br></div><div>Thanks for taking the time to answer.</div><div><br></div><div>I found the problem - I was mixing "regular" Bluetooth and QtBluetooth.</div><div><br></div><div>In particular, this line:</div><div>        self.sock = QtBluetooth.QBluetoothSocket(bluetooth.RFCOMM)<br></div><div>should have been:</div>        self.sock = QtBluetooth.QBluetoothSocket(QtBluetooth.QBluetoothServiceInfo.RfcommProtocol)<br><div><br></div><div>> You are already creating and starting a QApplication instance in your main</div>
code. Does removing this help at all?<br><div><br></div><div>Now the program works. And after I removed the duplicate calls to QApplication, it stopped crashing on exit also :)</div><div><br></div><div>> Perhaps it would be useful to see the regular Python code.<br></div><div><br></div><div>For reference, this was the original python code (no PyQt):</div><div><br></div><div>***</div><div>import bluetooth<br><br>bt_addr = '98:D3:C1:FD:2C:46'<br>port = 1<br>sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)<br>sock.connect((bt_addr,port))<br>sock.send('A'.encode())<br>data = sock.recv(1024)<br>if data:<br>    print(data.decode())<br></div><div><br></div><div>****</div><div><br></div><div>and the fixed code using PyQt5:</div><div><br></div><div>****</div><div><br></div><div>import sys<br>import os<br><br>from PyQt5.QtWidgets import QApplication, QWidget<br>from PyQt5.QtCore import Qt<br>from PyQt5 import QtBluetooth<br><br>class bluetoothTest(QWidget):<br><br>    def __init__(self, parent = None):<br>        super(bluetoothTest, self).__init__(parent)<br>        self.connectToRobot()<br>        self.win = QWidget()<br>        self.win.show()<br><br>    def connectToRobot(self):<br>        self.sock = QtBluetooth.QBluetoothSocket(QtBluetooth.QBluetoothServiceInfo.RfcommProtocol)<br><br>        self.sock.connected.connect(self.connectedToBluetooth)<br>        self.sock.readyRead.connect(self.receivedBluetoothMessage)<br>        self.sock.disconnected.connect(self.disconnectedFromBluetooth)<br>        self.sock.error.connect(self.socketError)<br>        port = 1<br>        self.sock.connectToService(QtBluetooth.QBluetoothAddress("98:D3:C1:FD:2C:46"),port)<br><br>    def socketError(self,error):<br>        print(self.sock.errorString())<br><br>    def connectedToBluetooth(self):<br>        self.sock.write('A'.encode())<br>    <br>    def disconnectedFromBluetooth(self):<br>        self.print('Disconnected from bluetooth')<br>        <br>    def receivedBluetoothMessage(self):<br>        while self.sock.canReadLine():<br>            line = self.sock.readLine()<br>            print(line)<br><br>def main():<br>    # deal with a bluetooth bug on mac<br>    if sys.platform == 'darwin':<br>        os.environ['QT_EVENT_DISPATCHER_CORE_FOUNDATION'] = '1'<br><br>    app = QApplication(sys.argv)<br>    ex = bluetoothTest()<br>    sys.exit(app.exec_())<br>            <br>if __name__ == '__main__':<br>        main()<br></div><div><br></div><div>******</div><div><br></div><div>Thanks,</div><div><br></div><div>Jason</div></div><div dir="ltr" class="gmail_signature"><div dir="ltr"></div></div></div>