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))
async def retrieveConnected_(self, scan_options) -> List[CBPeripheral]: self.devices = {} service_uuids = [] if "service_uuids" in scan_options: service_uuids_str = scan_options["service_uuids"] service_uuids = NSArray.alloc().initWithArray_( list(map(string2uuid, service_uuids_str))) peripherals = self.central_manager.retrieveConnectedPeripheralsWithServices_( service_uuids) # Add all those peripherals: for peripheral in peripherals: 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 logger.debug("Connected device {}: {}".format( uuid_string, device.name)) return []