Jakeler / ble-serial

"RFCOMM for BLE" a UART over Bluetooth low energy (4+) bridge for Linux, Mac and Windows
https://blog.ja-ke.tech/tags/#bluetooth
MIT License
251 stars 37 forks source link

macOS Monterey breaks the package entirely #53

Closed manielli closed 2 years ago

manielli commented 2 years ago

Describe the bug A clear and concise description of what the bug is. Installation using pip or pip3 happens successfully. ble-scan doesn't return anything. As soon as downgraded to macOS Big Sur, bee-serial works again. Tried on multiple systems and laptops.

ble-scan doesn't return anything. Only says Started scan and finished scan.

Setup (please complete the following information):

pip3 list
Package                        Version
------------------------------ -------
ble-serial                     2.5.0
bleak                          0.13.0
coloredlogs                    15.0.1
humanfriendly                  10.0
pip                            20.2.3
pyobjc-core                    8.1
pyobjc-framework-Cocoa         8.1
pyobjc-framework-CoreBluetooth 8.1
pyobjc-framework-libdispatch   8.1
setuptools                     49.2.1
six                            1.15.0
wheel                          0.33.1
brunorodd commented 2 years ago

Me too help! Thanks in advance!

Jakeler commented 2 years ago

This was already discussed on bleak: https://github.com/hbldh/bleak/issues/635 https://github.com/hbldh/bleak/pull/692 Apparently Apple now restricts what you can do with Bluetooth, like on iOS. That means scanning is only possible if you already know the service UUID, which is kind of crazy, especially for this application, cause there is no standard serial profile/UUID. Also it won't work at all if the advertisement packet does not contain the service uuid... I am sure it does not affect official Apple peripherals and they don't give a shit about 3rd party stuff as always. I will check if typical BLE serial modules send data how Apple wants it, otherwise I have to say that macOS 12+ is just unsupported.

mani-jafari commented 2 years ago

We have tried using the ble-serial --dev <service_uuid> --write-uuid <write-uuid> --read-uuid <read-uuid> --mtu 187 --port /tmp/ttyBLE as well and it doesn't connect either. We suspect the encrypted uuids we discovered using ble-scan with macOS Big Sur may have changed from Big Sur to Monterey and that's why the above mentioned command doesn't work maybe? regardless thanks for your help, we'll keep an eye on those github issues referenced

Jakeler commented 2 years ago

@mani-jafari BLE UUIDs are not encrypted and are the same, no matter the OS. Actually someone suggested to use the very powerful nRF Connect app on Android to find the IDs out. What you are trying to do does not work, because scanning based on service discovery is not implemented currently. You need to manually install the bleak develop branch and also change connection code in ble-serial, to make explicit use of BleakScanner, add an aditional service parameter and pass it through. At runtime you have to specify both the service UUIDs and device address then, because the service is not unambiguous, there could be many devices with same services.

mani-jafari commented 2 years ago

What is presented as BLE UUIDs as a result of scans on iOS and macOS are not the same for a specific device. The same device scanned with ble-serial's ble-scan command gives a different BLE UUID than what is scanned on iOS with libraries like react-native-ble-plx. Android for instance presents true MAC Addresses when a scan is performed with react-native-ble-plx library, but macOS/iOS does not show the true MAC Address (public one) and only show the (random) MAC addresses. ble-serial's ble-serial command on Linux can use Mac Addresses to connect directly to the device but macOS requires the BLE UUID that is return as part of the ble-scan command. I wrongly used the word encrypt but I meant the random resolvable addresses:

https://www.novelbits.io/bluetooth-address-privacy-ble/

Thanks for the instructions, will give it a try.

dejanovicnenad commented 2 years ago

@Jakeler Is there any fix coming for this? I'm on macOS Monterey and I'm unable to scan or connect to the BLE device.

image

image

Jakeler commented 2 years ago

Yes, you can expect at least some solution, since bleak added the service uuid workaround with 0.14. I just did not have the time to implement it so far, might be able to do it next week, will give you guys an update here then.

SpudGunMan commented 2 years ago

looks like osx12.3 has fixes reference https://github.com/hbldh/bleak/issues/720#issuecomment-1025017418

Jakeler commented 2 years ago

I have implemented the service based scanning now, you can install the branch with:

pip install https://github.com/Jakeler/ble-serial/archive/refs/heads/mac-scan-workaround.zip

It includes a new parameter:

  -s SERVICE_UUID, --service-uuid SERVICE_UUID
                        The service used for scanning of potential devices (default: None)

So if you specify a existing service like this:

ble-scan -s 0000ffe0-0000-1000-8000-00805f9b34fbe

It should find devices. Same for the main:

ble-serial -s 0000ffe0-0000-1000-8000-00805f9b34fb -v -t 30 ...

Note: It is still recommended to add the -d/--dev ID, because there could be multiple devices providing the same services.

Let me know if it works for you. Also thanks @SpudGunMan for the hint. Would be nice if someone could already try the macOS 12.3 (beta) and verify that it indeed works without service UUID again and solves the issue.