Skip to content

Instantly share code, notes, and snippets.

@wwj718
Forked from StefanD986/BluetoothLowEnergy.py
Created September 29, 2018 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwj718/8823e8a35ecdc76e0700cda703e71290 to your computer and use it in GitHub Desktop.
Save wwj718/8823e8a35ecdc76e0700cda703e71290 to your computer and use it in GitHub Desktop.
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