def centralManager_didDiscoverPeripheral_advertisementData_RSSI_(
        self,
        central: CBCentralManager,
        peripheral: CBPeripheral,
        advertisementData: NSDictionary,
        RSSI: NSNumber,
    ):
        # Note: this function might be called several times for same device.
        # Example a first time with the following keys in advertisementData:
        # ['kCBAdvDataLocalName', 'kCBAdvDataIsConnectable', 'kCBAdvDataChannel']
        # ... and later a second time with other keys (and values) such as:
        # ['kCBAdvDataServiceUUIDs', 'kCBAdvDataIsConnectable', 'kCBAdvDataChannel']
        #
        # i.e it is best not to trust advertisementData for later use and data
        # from it should be copied.
        #
        # This behaviour could be affected by the
        # CBCentralManagerScanOptionAllowDuplicatesKey global setting.

        uuid_string = peripheral.identifier().UUIDString()

        if uuid_string in self.devices:
            device = self.devices[uuid_string]
        else:
            address = uuid_string
            name = peripheral.name() or None
            details = peripheral
            device = BLEDeviceCoreBluetooth(address, name, details)
            self.devices[uuid_string] = device

        device._rssi = float(RSSI)
        device._update(advertisementData)

        logger.debug("Discovered device {}: {} @ RSSI: {} (kCBAdvData {})".format(
                uuid_string, device.name, RSSI, advertisementData.keys()))
    def did_discover_peripheral(
        self,
        central: CBCentralManager,
        peripheral: CBPeripheral,
        advertisementData: NSDictionary,
        RSSI: NSNumber,
    ):
        # Note: this function might be called several times for same device.
        # This can happen for instance when an active scan is done, and the
        # second call with contain the data from the BLE scan response.
        # Example a first time with the following keys in advertisementData:
        # ['kCBAdvDataLocalName', 'kCBAdvDataIsConnectable', 'kCBAdvDataChannel']
        # ... and later a second time with other keys (and values) such as:
        # ['kCBAdvDataServiceUUIDs', 'kCBAdvDataIsConnectable', 'kCBAdvDataChannel']
        #
        # i.e it is best not to trust advertisementData for later use and data
        # from it should be copied.
        #
        # This behaviour could be affected by the
        # CBCentralManagerScanOptionAllowDuplicatesKey global setting.

        uuid_string = peripheral.identifier().UUIDString()

        if uuid_string in self.devices:
            device = self.devices[uuid_string]
            # It could be the device did not have a name previously but now it does.
            if peripheral.name():
                device.name = peripheral.name()
        else:
            address = uuid_string
            name = peripheral.name() or None
            details = peripheral
            device = BLEDeviceCoreBluetooth(address,
                                            name,
                                            details,
                                            delegate=self)
            self.devices[uuid_string] = device

        device.rssi = RSSI
        device._update(advertisementData)

        for callback in self.callbacks.values():
            if callback:
                callback(peripheral, advertisementData, RSSI)

        logger.debug(
            "Discovered device {}: {} @ RSSI: {} (kCBAdvData {}) and Central: {}"
            .format(uuid_string, device.name, RSSI, advertisementData.keys(),
                    central))
Beispiel #3
0
 def centralManager_didDiscoverPeripheral_advertisementData_RSSI_(
     self,
     central: CBCentralManager,
     peripheral: CBPeripheral,
     advertisementData: NSDictionary,
     RSSI: NSNumber,
 ):
     uuid_string = peripheral.identifier().UUIDString()
     if uuid_string not in list(
             map(lambda x: x.identifier().UUIDString(),
                 self.peripheral_list)):
         self.peripheral_list.append(peripheral)
         self.advertisement_data_list.append(advertisementData)
         logger.debug("Discovered device {}: {} @ RSSI: {}".format(
             uuid_string,
             peripheral.name() or "Unknown", RSSI))
 def did_fail_to_connect_peripheral(self, centralManager: CBCentralManager,
                                    peripheral: CBPeripheral,
                                    error: NSError):
     logger.debug("Failed to connect to device uuid {}".format(
         peripheral.identifier().UUIDString()))
     self._connection_state = CMDConnectionState.DISCONNECTED
     self._connection_state_changed.set()
 def centralManager_didFailToConnectPeripheral_error_(
     self, centralManager: CBCentralManager, peripheral: CBPeripheral, error: NSError
 ):
     logger.debug(
         "Failed to connect to device uuid {}".format(
             peripheral.identifier().UUIDString()
         )
     )
     self._connection_state = CMDConnectionState.DISCONNECTED