Пример #1
0
 def populate_items(self, scan_list):
     # scan list --> items
     # for item in scan list:
     for dev in scan_list:
         try:
             services = [
                 uuidstr_to_str(serv.lower())
                 for serv in dev.metadata['uuids']
             ]
         except Exception as e:
             services = []
         icon = os.path.join(
             self.SRC_PATH,
             self._RSSI_icon.format(self.map_rssi_level_icon(dev.rssi)))
         myQCustomQWidget = ScanDeviceItem()
         myQCustomQWidget.setTextUp("{}\nUUID: {}".format(
             dev.name, dev.address))
         myQCustomQWidget.setTextDown("RSSI: {} dBm  Services: {}".format(
             dev.rssi, ','.join(services)))
         myQCustomQWidget.setIcon(icon)
         # Create QListWidgetItem
         myQListWidgetItem = QtWidgets.QListWidgetItem(self.myQListWidget)
         # Set size hint
         myQListWidgetItem.setSizeHint(myQCustomQWidget.sizeHint())
         # Add QListWidgetItem into QListWidget
         self.myQListWidget.addItem(myQListWidgetItem)
         self.myQListWidget.setItemWidget(myQListWidgetItem,
                                          myQCustomQWidget)
Пример #2
0
def format_GATT_object(object_path, interfaces):
    if defs.GATT_SERVICE_INTERFACE in interfaces:
        props = interfaces.get(defs.GATT_SERVICE_INTERFACE)
        _type = "{0} Service".format(
            "Primary" if props.get("Primary") else "Secondary")
    elif defs.GATT_CHARACTERISTIC_INTERFACE in interfaces:
        props = interfaces.get(defs.GATT_CHARACTERISTIC_INTERFACE)
        _type = "Characteristic"
    elif defs.GATT_DESCRIPTOR_INTERFACE in interfaces:
        props = interfaces.get(defs.GATT_DESCRIPTOR_INTERFACE)
        _type = "Descriptor"
    else:
        return None

    _uuid = props.get("UUID")
    return "\n{0}\n\t{1}\n\t{2}\n\t{3}".format(_type, object_path, _uuid,
                                               uuidstr_to_str(_uuid))
Пример #3
0
async def run(address, loop, debug=False):
    log = logging.getLogger(__name__)
    if debug:
        import sys

        # loop.set_debug(True)
        log.setLevel(logging.DEBUG)
        h = logging.StreamHandler(sys.stdout)
        h.setLevel(logging.DEBUG)
        log.addHandler(h)

    async with BleakClient(address, loop=loop) as client:
        x = await client.is_connected()
        log.info("Connected: {0}".format(x))

        for service in client.services:
            # service is instance of 'Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceService'
            log.info("[Service] {0}: {1}".format(service.uuid,
                                                 uuidstr_to_str(service.uuid)))
            # Ugly way to filter out characteristics for this service... I use this since Bleak has
            # already fetched all characteristics and stored them in `client.characteristics`,
            # albeit not grouped by service...
            # Could e.g. be fetched as `chars = await client._get_chars(service)`
            for char in service.characteristics:
                # char is instance of 'Windows.Devices.Bluetooth.GenericAttributeProfile.GattCharacteristic'
                if "read" in char.properties:
                    try:
                        char_value = await client.read_gatt_char(char.uuid)
                    except BleakError as e:
                        char_value = "ERROR: {0}".format(e)
                else:
                    char_value = None

                char_name = (char.description if char.description else "None")
                log.info(
                    "\t[Characteristic] {0}: ({1}) | Name: {2}, Value: {3} ".
                    format(char.uuid, ",".join(char.properties), char_name,
                           char_value))
                # Descriptor handling for Windows will be added in Bleak 0.4.0...
                for descriptor in char.descriptors:
                    log.info("\t\t[Descriptor] {0}: (Handle: {1})".format(
                        descriptor.description, descriptor.handle))
Пример #4
0
    def do_scan(self):
        self.devices = []
        n = 0
        self.log.info('Scanning...')
        while n < 3:
            try:
                self.devices = ble_scan()
                n += 1
                if len(self.devices) > 0:
                    break
                else:
                    self.log.info('Scanning...')
            except KeyboardInterrupt:
                break
            except Exception as e:
                print(e)
                time.sleep(1)
        if len(self.devices) == 0:
            self.log.info('No BLE device found')
        else:
            self.devices = sorted(self.devices, key=lambda dev: dev.rssi)
            self.devices.reverse()
            self.log.info('BLE device/s found: {}'.format(len(self.devices)))
            for dev in self.devices:
                services = []
                if hasattr(dev, 'metadata'):
                    if isinstance(dev.metadata, dict):
                        if 'uuids' in dev.metadata.keys():
                            try:
                                services = [
                                    uuidstr_to_str(serv.lower())
                                    for serv in dev.metadata['uuids']
                                ]
                            except Exception as e:
                                services = []

                self.log.info("NAME: {}, UUID: {}, RSSI: {} dBm".format(
                    dev.name, dev.address, dev.rssi))
Пример #5
0
 def description(self) -> str:
     """Description for this characteristic"""
     return uuidstr_to_str(self.uuid)
Пример #6
0
 def description(self) -> str:
     """String description for this service"""
     return uuidstr_to_str(self.uuid)
Пример #7
0
 def description(self) -> str:
     """Description for this descriptor"""
     return uuidstr_to_str(self.uuid)
Пример #8
0
 def description(self) -> str:
     """Description for this characteristic"""
     # No description available in Core Bluetooth backend.
     return uuidstr_to_str(self._uuid)