Skip to content

Conversation

@AGaylord6
Copy link
Contributor

Features:

  • Extract RSSI/device type
  • Fix crash (add self._device_fut.done() check)
  • Update seen cache to use MAC address instead of Device objects
  • Move device printing and add summary stats

Please let me know what you think! I was thinking having the printing as a class method would be helpful functionality to provide, but happy to move it back it outside if we want to the user to decide. It also looks more wack because of using logger.info.

Copy link
Owner

@malmeloo malmeloo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contributions! The changes look good, but I left some small notes that I think could be improved. I don't think I'll be able to test your changes today, but I'll definitely do so later this weekend.

Comment on lines 26 to 33
APPLE_DEVICE_TYPE = {
0: "Apple Device",
1: "AirTag",
2: "Licensed 3rd Party Find My Device",
3: "AirPods",
}

BATTERY_LEVEL = {0: "Full", 1: "Medium", 2: "Low", 3: "Very Low"}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: maybe swap out the decimals here with their binary representation (0bXX) just for clarity

self._first_adv_key_bytes: bytes = first_adv_key_bytes

@property
def adv_key_bytes(self) -> bytes:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I like this method... I think it could be confusing as its signature is the same as the method on e.g. a KeyPair, but unlike a keypair, the result of this method is incomplete so they aren't directly comparable. I still think it's good to expose this information though, just maybe with a different method name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to standardize the interface but ended up not needing to. I just changed the name so it's still exposed.

Comment on lines 257 to 264
logger.info("Nearby %s - %s", self.device_type, self.mac_address)
logger.info(" Status byte: 0x%x", self.status)
logger.info(" Battery lvl: %s", self.battery_level)
logger.info(" RSSI: %s", self.rssi)
logger.info(" Extra data:")
for k, v in sorted(self.additional_data.items()):
logger.info(" %s: %s", f"{k:20}", v)
logger.info("\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just use print instead (feel free to suppress the linter.) Otherwise the result of this method will depend on whatever logger settings that are currently applied (if any). Also, maybe a file argument to give to print to allow printing to files for example?

Comment on lines 369 to 379
logger.info("Separated %s - %s", self.device_type, self.mac_address)
logger.info(" Public key: %s", self.adv_key_b64)
logger.info(" Lookup key: %s", self.hashed_adv_key_b64)
logger.info(" Status byte: 0x%x", self.status)
logger.info(" Battery lvl: %s", self.battery_level)
logger.info(" Hint byte: 0x%x", self.hint)
logger.info(" RSSI: %s", self.rssi)
logger.info(" Extra data:")
for k, v in sorted(self.additional_data.items()):
logger.info(" %s: %s", f"{k:20}", v)
logger.info("\n")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I think using print is probably better

Comment on lines 488 to 491
# Extract RSSI if it exists
rssi = None
if data.rssi is not None:
rssi = data.rssi
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If data.rssi is already None | int, this if statement is probably redundant


except asyncio.TimeoutError: # timeout reached
self._device_fut = self._loop.create_future()
self.print_scanning_results(devices_seen)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think whether to print results or not should be left up to the caller instead of doing it by default

@malmeloo
Copy link
Owner

Just found some time to test it out and it works great! Definitely a huge improvement over the current scanner. If you could take a look at my comments I'll try to merge it soon.

@AGaylord6
Copy link
Contributor Author

Glad it works, thank you for the suggestions! I'll fix it up sometime this week when I get a chance.

@AGaylord6
Copy link
Contributor Author

Let me know how that updated printing logic looks. I was thinking a JSONL makes it easy to append new devices, but we can do a txt or whatever instead.

@malmeloo
Copy link
Owner

Ah sorry, maybe I wasn't being clear. With printing to a file I meant something more like this:

    def print_device(self, file: SupportsWrite[str] | None = None) -> None:
        print(..., file=file)

Then the user can specify a 'file' that the library will print to, but also sys.stderr and the like. JSON serialization is nice (and the library already has an abc for it: Serializable), but I'm not sure how useful that would be really. I'm also planning to make some changes to these device classes soon that would make it a bit more difficult to deserialize such an object from JSON, so maybe it's better to reconsider such a feature after that is done :-)

I have some time now so I will just go ahead and modify it myself. Thanks again!

@malmeloo malmeloo linked an issue Nov 27, 2025 that may be closed by this pull request
@malmeloo malmeloo merged commit 283a09d into malmeloo:main Nov 27, 2025
8 checks passed
@AGaylord6
Copy link
Contributor Author

Ohh makes sense, that's much cleaner. Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BLE scanning improvements

2 participants