pyqt5 QTbluetooth 发送和接收的代码
时间: 2024-05-10 17:15:46 浏览: 21
以下是使用 Pyqt5 中的 QBluetooth 模块进行蓝牙发送和接收的示例代码:
```python
from PyQt5.QtBluetooth import QBluetoothDeviceDiscoveryAgent, QBluetoothSocket
from PyQt5.QtCore import QIODevice, QBluetoothUuid, QByteArray
from PyQt5.QtWidgets import QApplication, QDialog, QListWidget, QVBoxLayout, QPushButton, QHBoxLayout, QLineEdit
import sys
class BluetoothDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.devices = QListWidget()
self.discovery = QBluetoothDeviceDiscoveryAgent()
self.discovery.deviceDiscovered.connect(self.add_device)
self.devices.itemDoubleClicked.connect(self.connect_to_device)
self.connect_button = QPushButton('Connect')
self.connect_button.clicked.connect(self.connect_to_device)
self.send_button = QPushButton('Send')
self.send_button.clicked.connect(self.send_data)
self.input_box = QLineEdit()
layout = QVBoxLayout()
layout.addWidget(self.devices)
layout.addWidget(self.connect_button)
hbox = QHBoxLayout()
hbox.addWidget(self.input_box)
hbox.addWidget(self.send_button)
layout.addLayout(hbox)
self.setLayout(layout)
def add_device(self, info):
name = info.name()
address = info.address().toString()
self.devices.addItem('{} ({})'.format(name, address))
def connect_to_device(self):
device = self.devices.currentItem().text()
device_name = device.split(' ')[0]
device_address = device.split(' ')[1][1:-1]
socket = QBluetoothSocket(QBluetoothServiceInfo.RfcommProtocol)
socket.connectToService(QBluetoothAddress(device_address), QBluetoothUuid.SerialPort)
self.socket = socket
self.socket.readyRead.connect(self.receive_data)
def send_data(self):
text = self.input_box.text()
if not text:
return
data = QByteArray()
data.append(text)
self.socket.write(data)
self.input_box.setText('')
def receive_data(self):
data = self.socket.readAll()
print(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = BluetoothDialog()
dialog.discovery.start()
dialog.show()
sys.exit(app.exec_())
```
该示例代码实现了蓝牙设备的搜索、连接、发送和接收功能。在搜索到蓝牙设备后,用户可以通过双击列表中的设备名称来连接设备。连接成功后,用户可以通过文本框输入需要发送的数据,点击 Send 按钮发送数据,并通过 receive_data 方法接收数据。注意,该示例代码只是一个简单的示例,实际应用中需要根据具体需求进行修改。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)