def __init__(self, service_uuid, char_uuid, processData, sendHeartbeat, sendStop, reset): super().__init__() self.SERVICE_UUID = QtBluetooth.QBluetoothUuid(service_uuid) self.CHAR_UUID = QtBluetooth.QBluetoothUuid(char_uuid) self.processData = processData self.sendHeartbeat = sendHeartbeat self.sendStop = sendStop self.reset = reset self.QBluetoothDeviceDiscoveryAgent_LowEnergyMethod = 2 self.ENABLE_NOTIFICATION_VALUE = QtCore.QByteArray.fromHex(b"0100") self.DISABLE_NOTIFICATION_VALUE = QtCore.QByteArray.fromHex(b"0000") self.m_deviceDiscoveryAgent = QtBluetooth.QBluetoothDeviceDiscoveryAgent( ) self.m_deviceDiscoveryAgent.setLowEnergyDiscoveryTimeout(500) self.m_deviceDiscoveryAgent.deviceDiscovered.connect(self.addDevice) self.m_deviceDiscoveryAgent.finished.connect(self.onScanFinished) self.m_deviceDiscoveryAgent.canceled.connect(self.onScanFinished) self.m_device = None self.m_control = None self.m_service = None self.m_notificationDesc = QtBluetooth.QLowEnergyDescriptor() self.m_readCharacteristic = QtBluetooth.QLowEnergyCharacteristic() self.m_writeCharacteristic = QtBluetooth.QLowEnergyCharacteristic() self.m_writemode = QtBluetooth.QLowEnergyService.WriteMode() self.m_connected = False self.m_readTimer = None self.dataReceived.connect(self.printDataReceived)
def __init__(self, service_uuid, char_uuid, processData, sendHeartbeat, reset): super().__init__() self.SERVICE_UUID = QtBluetooth.QBluetoothUuid(service_uuid) self.CHAR_UUID = QtBluetooth.QBluetoothUuid(char_uuid) self.processData = processData self.sendHeartbeat = sendHeartbeat self.reset = reset self.m_deviceDiscoveryAgent = QtBluetooth.QBluetoothDeviceDiscoveryAgent( ) self.m_notificationDesc = QtBluetooth.QLowEnergyDescriptor() self.m_control = None #self.m_servicesUuid = QtBluetooth.QBluetoothUuid() self.m_servicesUuid = [] self.m_service = QtBluetooth.QLowEnergyService self.m_readCharacteristic = QtBluetooth.QLowEnergyCharacteristic() self.m_writeCharacteristic = QtBluetooth.QLowEnergyCharacteristic() self.m_writemode = QtBluetooth.QLowEnergyService.WriteMode() self.m_device = None # bound to the device if discovered self.m_services = [] self.m_currentService = None self.m_connected = False self.m_readTimer = None self.m_deviceDiscoveryAgent.setLowEnergyDiscoveryTimeout(500) self.m_deviceDiscoveryAgent.deviceDiscovered.connect(self.addDevice) self.m_deviceDiscoveryAgent.error.connect(self.onDeviceScanError) self.m_deviceDiscoveryAgent.finished.connect(self.onScanFinished) self.m_deviceDiscoveryAgent.canceled.connect(self.onScanFinished) self.dataReceived.connect(self.printDataReceived)
def detailsDiscovered(self, state): if state == QtBluetooth.QLowEnergyService.ServiceDiscovered: self.positionChrc = self.service.characteristic(QtBluetooth.QBluetoothUuid('{00000125-0000-1000-8000-00805f9b34fb}')) self.buttonsPressChrc = self.service.characteristic(QtBluetooth.QBluetoothUuid('{00000225-0000-1000-8000-00805f9b34fb}')) des = self.positionChrc.descriptor( QtBluetooth.QBluetoothUuid(QtBluetooth.QBluetoothUuid.ClientCharacteristicConfiguration) ) self.service.writeDescriptor(des, bytes.fromhex('0100')) des = self.buttonsPressChrc.descriptor( QtBluetooth.QBluetoothUuid(QtBluetooth.QBluetoothUuid.ClientCharacteristicConfiguration) ) self.service.writeDescriptor(des, bytes.fromhex('0100')) pos = self.positionChrc.value() self.x, self.y = struct.unpack("<ff", pos)
def searchCharacteristic(self): # print("searchCharacteristic") if self.m_service: for c in self.m_service.characteristics(): if c.isValid(): if c.properties( ) & QtBluetooth.QLowEnergyCharacteristic.WriteNoResponse or c.properties( ) & QtBluetooth.QLowEnergyCharacteristic.Write: self.m_writeCharacteristic = c self.update_connected(True) if c.properties( ) & QtBluetooth.QLowEnergyCharacteristic.WriteNoResponse: self.m_writemode = QtBluetooth.QLowEnergyService.WriteWithoutResponse else: self.m_writemode = QtBluetooth.QLowEnergyService.WriteWithResponse if c.properties( ) & QtBluetooth.QLowEnergyCharacteristic.Read: self.m_readCharacteristic = c if not self.m_readTimer: self.m_readTimer = QtCore.QTimer() self.m_readTimer.timeout.connect(self.read) self.m_readTimer.start(3000) self.m_notificationDesc = c.descriptor( QtBluetooth.QBluetoothUuid(0x2902)) #print(QtBluetooth.QBluetoothUuid(0x2902).toString()) if self.m_notificationDesc.isValid(): #print("notificationCharacteristic") #print(bytearray([1,0])) self.m_service.writeDescriptor(self.m_notificationDesc, bytearray([1, 0])) self.heartbeat()
def searchCharacteristic(self): if self.m_service: for c in self.m_service.characteristics(): if c.isValid(): if c.properties() & QtBluetooth.QLowEnergyCharacteristic.WriteNoResponse or c.properties() & QtBluetooth.QLowEnergyCharacteristic.Write: self.m_writeCharacteristic = c self.update_connected(True) if c.properties() & QtBluetooth.QLowEnergyCharacteristic.WriteNoResponse: self.m_writemode=QtBluetooth.QLowEnergyService.WriteWithoutResponse else: self.m_writemode=QtBluetooth.QLowEnergyService.WriteWithResponse self.m_notificationDesc = c.descriptor(QtBluetooth.QBluetoothUuid(QtBluetooth.QBluetoothUuid.ClientCharacteristicConfiguration)) if self.m_notificationDesc.isValid(): self.m_service.descriptorWritten.connect(self.descriptorWrittenSlot) self.m_service.writeDescriptor(self.m_notificationDesc,self.ENABLE_NOTIFICATION_VALUE)
def onServiceDiscovered(self, _=QtBluetooth.QBluetoothUuid()): self.statusInfoChanged.emit( "Service discovered. Waiting for service scan to be done...", True)
def onServiceDiscovered(self, gatt=QtBluetooth.QBluetoothUuid()): self.statusInfoChanged.emit( "Service discovered. Waiting for service scan to be done...", True) print( "Service discovered. Waiting for service scan to be done... GATT: {gatt} " .format(gatt=gatt))
import signal import sys import os from PyQt5 import QtBluetooth as QtBt from PyQt5 import QtCore, QtWidgets service_uuid = '7b183224-9168-443e-a927-7aeea07e8105' count_uuid = '292bd3d2-14ff-45ed-9343-55d125edb721' rw_uuid = '56cd7757-5f47-4dcd-a787-07d648956068' data_uuid = 'fec26ec4-6d71-4442-9f81-55bc21d658d6' print(QtBt.QBluetoothUuid(rw_uuid)) data_uuid = QtBt.QBluetoothUuid(data_uuid) service_uuid = QtBt.QBluetoothUuid(service_uuid) if sys.platform == 'darwin': os.environ['QT_EVENT_DISPATCHER_CORE_FOUNDATION'] = '1' class Application(QtWidgets.QApplication): # class Application(QtCore.QCoreApplication): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # self.le_controller = QLowEnergyController self.scan_for_devices() print("before exec") self.exec() print("after exec") def display_status(self): print(self.agent.isActive(), self.agent.discoveredDevices())
def servicesScanned(self): self.service = self.central.createServiceObject(QtBluetooth.QBluetoothUuid('{00003125-0000-1000-8000-00805f9b34fb}'), self.central) self.service.stateChanged.connect(self.detailsDiscovered) self.service.descriptorWritten.connect(self.countDescript) self.service.discoverDetails()