Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
QBluetooth Discovery Methods in pyqt
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