Last active
April 26, 2024 09:03
-
-
Save StefanD986/1b00e6c078516a60e5e3e691903b9051 to your computer and use it in GitHub Desktop.
QBluetooth Discovery Methods in pyqt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PyQt5 | |
from PyQt5 import QtCore | |
from PyQt5 import QtBluetooth | |
class DeviceFinder(QtCore.QObject): | |
def __init__(self): | |
super().__init__() | |
self.m_devices = [] | |
self.deviceDiscoveryAgent = QtBluetooth.QBluetoothDeviceDiscoveryAgent(self) | |
self.deviceDiscoveryAgent.setLowEnergyDiscoveryTimeout(500) | |
self.deviceDiscoveryAgent.deviceDiscovered.connect(self.add_device) | |
self.deviceDiscoveryAgent.error.connect(self.scan_error) | |
self.deviceDiscoveryAgent.finished.connect(self.scan_finished) | |
self.deviceDiscoveryAgent.canceled.connect(self.scan_finished) | |
self.deviceDiscoveryAgent.start(QtBluetooth.QBluetoothDeviceDiscoveryAgent.DiscoveryMethod(2)) | |
def add_device(self, device): | |
# If device is LowEnergy-device, add it to the list | |
if device.coreConfigurations() and QtBluetooth.QBluetoothDeviceInfo.LowEnergyCoreConfiguration: | |
self.m_devices.append( QtBluetooth.QBluetoothDeviceInfo(device) ) | |
print("Low Energy device found. Scanning more...") | |
def scan_finished(self): | |
print("scan finished") | |
for i in self.m_devices: | |
#QtBluetooth.QBluetoothDeviceInfo. | |
print('UUID: {UUID}, Name: {name}, rssi: {rssi}'.format(UUID=i.deviceUuid().toString(), | |
name=i.name(), | |
rssi=i.rssi())) | |
self.quit() | |
def scan_error(self): | |
print("scan error") | |
def quit(self): | |
print("Bye!") | |
QtCore.QCoreApplication.instance().quit() | |
if __name__ == "__main__": | |
import sys | |
app = QtCore.QCoreApplication(sys.argv) | |
hello = DeviceFinder() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment