Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE MAC address on MacOS instead of system ID #140

Closed
5frank opened this issue Jan 2, 2020 · 4 comments
Closed

BLE MAC address on MacOS instead of system ID #140

5frank opened this issue Jan 2, 2020 · 4 comments
Assignees
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend Opinions Appreciated Please add an opinion on your desired resolution on this issue!

Comments

@5frank
Copy link

5frank commented Jan 2, 2020

  • bleak: 0.5.1
  • os: Darwin-18.7.0-x86_64-i386-64bit
  • python: 3.7.5

Perhaps it is only me, but it would be nice if the CoreBluetooth backend used BLE MAC address and not the system "device UUID"? It would make my scripts and configurations portable (I am usually on Linux).
I made an attempt to implement this inspired from noble. Initial tests seems to work even if the solution feels somewhat hacky.

Question:

  1. Do anyone see any problems with this method of obtaining the address (i.e. read the "CoreBluetoothCache" from /Library/Preferences/com.apple.Bluetooth.plist)? Is there a better way?

  2. It seems the reason MacOS uses UUID is because some security feature in BLE that hides the MAC address. Does anyone know how this is handled on Windows and Linux/BlueZ?

  3. Is it a bad or good idea to accept both MacOS "device UUID" and BLE MAC address? Would make this change backward compatible, but not sure if anyone cares.

example how to read CoreBluetoothCache:

from plistlib import load as plist_load
async def __get_addr_from_CoreBluetoothCache():
    ''' returns dict {MacOS_device_UUID : BLE_MAC_address } '''

    path = "/Library/Preferences/com.apple.Bluetooth.plist"
    try:
        with open(path, "rb") as f:
            plist = plist_load(f)
    except FileNotFoundError:
        logger.warning("{} do not exist".format(path))
        return {}

    if "CoreBluetoothCache" not in plist:
        logger.debug("No CoreBluetoothCache in {}".format(path))
        return {}

    cbcache = plist["CoreBluetoothCache"]  

    if cbcache is None:
        return {}

    uuid_to_addr = {}

    for devuuid, devinfo in cbcache.items():
        if "DeviceAddress" not in devinfo:
            continue

        addr = devinfo["DeviceAddress"]
        addr = addr.replace('-', ':')

        uuid_to_addr[devuuid] = addr

    return uuid_to_addr 
@5frank
Copy link
Author

5frank commented Jan 4, 2020

  1. It seems that CoreBluetoothCache is only updated after a connect. So you still need the UUID or some other workaround.

  2. Could possible check whatever the "address" passed to connect is a 128-bit UUID or 48-bit MAC address an handle it accordingly. (assuming there is a sane way to obtain the MAC)

@hbldh hbldh self-assigned this Jan 5, 2020
@hbldh hbldh added the Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend label Jan 5, 2020
@hbldh
Copy link
Owner

hbldh commented Jan 5, 2020

From the Bleak documentation:

The most noticible difference between the other backends of bleak and this backend, is that CoreBluetooth doesn’t scan for other devices via MAC address. Instead, UUIDs are utilized that are often unique between the device that is scanning the the device that is being scanned.

It would be nice to be able to use MAC address, but the effort to circumvent the macOS UUID solution is not proportional to the benefits. Reading from a cache file on disk is a reasonable idea, but I do not think it is reliable enough to be used for creating connections.

Bleak strives to have an identical API, but is accepting of OS specific differences. macOS discovers by UUID and connects by UUID, it would be counterproductive to force MAC connecting. This is one of the few "this we just have to accept" differences, in my eyes.

@hbldh hbldh added the Opinions Appreciated Please add an opinion on your desired resolution on this issue! label Jan 5, 2020
hbldh added a commit that referenced this issue Jan 5, 2020
JoeHut pushed a commit to workaroundgmbh/bleak that referenced this issue Feb 4, 2020
JoeHut pushed a commit to workaroundgmbh/bleak that referenced this issue Feb 5, 2020
hbldh added a commit that referenced this issue Mar 9, 2020
@hbldh
Copy link
Owner

hbldh commented Jun 2, 2020

I do not think this is solvable. Will close for now.

@dlech
Copy link
Collaborator

dlech commented Oct 7, 2022

FYI, I have found a hack for this. See #1073. Give it a 👍 if this is still a needed feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend Opinions Appreciated Please add an opinion on your desired resolution on this issue!
Projects
None yet
Development

No branches or pull requests

3 participants