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

Is there a way to connect to a BLE device that requires a pairing code using ble-serial module? #64

Closed KamiK2K closed 1 year ago

KamiK2K commented 1 year ago

Hi, I was able to use this module and connect to a BLE device, and pull the data from the virtual COM port that has been created using com0com application on Windows 10.

However, I am not able to connect to a BLE device that is configured to ask the incoming connection request to enter a pairing code.

I am configuring Ublox's NINA-B221 module as a BLE device and I have to set a pairing code for it to provide security for Bluetooth connection. The ble-serial module is unable to connect to this device. Some times it connects but then gets disconnected and throws the error that says the attribute requires authentication before it can be read or written.

Is there any way to add some lines to set the pairing code if it is required by the BLE device?

I am including the connection log on my command prompt window on both cases here.

Case 1: Connecting to a BLE device that does not require a pairing code:

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-scan Started BLE scan

Finished BLE scan

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-scan Started BLE scan

54:F8:2A:08:29:B8 (RSSI=-67): BLE-SN 29

Finished BLE scan

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-scan -d 54:F8:2A:08:29:B8 Started BLE scan

Started deep scan of 54:F8:2A:08:29:B8

Found no device with matching address

Finished BLE scan

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-scan -d 54:F8:2A:08:29:B8 Started BLE scan

Started deep scan of 54:F8:2A:08:29:B8

Found no device with matching address

Finished BLE scan

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-scan -d 54:F8:2A:08:29:B8 Started BLE scan

54:F8:2A:08:29:B8 (RSSI=-73): BLE-SN 29

Started deep scan of 54:F8:2A:08:29:B8

Found device 54:F8:2A:08:29:B8: BLE-SN 29 (out of 1) SERVICE 00001801-0000-1000-8000-00805f9b34fb (Handle: 1): Generic Attribute Profile CHARACTERISTIC 00002a05-0000-1000-8000-00805f9b34fb (Handle: 2): ['indicate'] DESCRIPTOR 00002902-0000-1000-8000-00805f9b34fb (Handle: 4): Client Characteristic Configuration SERVICE 00001800-0000-1000-8000-00805f9b34fb (Handle: 5): Generic Access Profile CHARACTERISTIC 00002a00-0000-1000-8000-00805f9b34fb (Handle: 6): ['read'] CHARACTERISTIC 00002a01-0000-1000-8000-00805f9b34fb (Handle: 8): ['read'] CHARACTERISTIC 00002a04-0000-1000-8000-00805f9b34fb (Handle: 10): ['read'] SERVICE 0000180a-0000-1000-8000-00805f9b34fb (Handle: 12): Device Information CHARACTERISTIC 00002a29-0000-1000-8000-00805f9b34fb (Handle: 13): ['read'] CHARACTERISTIC 00002a24-0000-1000-8000-00805f9b34fb (Handle: 15): ['read'] CHARACTERISTIC 00002a26-0000-1000-8000-00805f9b34fb (Handle: 17): ['read'] CHARACTERISTIC 00002a28-0000-1000-8000-00805f9b34fb (Handle: 19): ['read'] SERVICE 2456e1b9-26e2-8f83-e744-f34f01e9d701 (Handle: 21): Unknown CHARACTERISTIC 2456e1b9-26e2-8f83-e744-f34f01e9d703 (Handle: 22): ['read', 'write-without-response', 'write', 'notify'] DESCRIPTOR 00002902-0000-1000-8000-00805f9b34fb (Handle: 24): Client Characteristic Configuration CHARACTERISTIC 2456e1b9-26e2-8f83-e744-f34f01e9d704 (Handle: 25): ['write-without-response', 'write', 'notify'] DESCRIPTOR 00002902-0000-1000-8000-00805f9b34fb (Handle: 27): Client Characteristic Configuration

Completed deep scan of 54:F8:2A:08:29:B8

Finished BLE scan

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-serial -d 54:F8:2A:08:29:B8 -t 30 -w 2456e1b9-26e2-8f83-e744-f34f01e9d701 -r 2456e1b9-26e2-8f83-e744-f34f01e9d701 07:21:57.266 | INFO | ble_interface.py: Receiver set up 07:22:00.968 | INFO | ble_interface.py: Trying to connect with 54:F8:2A:08:29:B8: Unknown 07:22:31.006 | ERROR | main.py: Unexpected Error: TimeoutError() 07:22:31.006 | WARNING | main.py: Shutdown initiated 07:22:31.006 | INFO | windows_com0com.py: Stopping RX+TX loop 07:22:31.006 | INFO | main.py: Shutdown complete.

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-serial -d 54:F8:2A:08:29:B8 -t 60 -w 2456e1b9-26e2-8f83-e744-f34f01e9d701 -r 2456e1b9-26e2-8f83-e744-f34f01e9d701 07:22:44.531 | INFO | ble_interface.py: Receiver set up 07:22:48.653 | INFO | ble_interface.py: Trying to connect with 54:F8:2A:08:29:B8: Unknown 07:23:25.667 | INFO | client.py: Services resolved for BleakClientWinRT (54:F8:2A:08:29:B8) 07:23:25.667 | INFO | ble_interface.py: Device 54:F8:2A:08:29:B8 connected 07:23:25.682 | ERROR | main.py: Unexpected Error: AssertionError('No characteristic with specified write UUID 2456e1b9-26e2-8f83-e744-f34f01e9d701 found!') 07:23:25.682 | WARNING | main.py: Shutdown initiated 07:23:25.682 | INFO | windows_com0com.py: Stopping RX+TX loop 07:23:28.789 | WARNING | ble_interface.py: Device 54:F8:2A:08:29:B8 disconnected 07:23:28.789 | INFO | ble_interface.py: Stopping Bluetooth event loop 07:23:28.805 | INFO | ble_interface.py: Bluetooth disconnected 07:23:28.805 | INFO | main.py: Shutdown complete.

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-serial -d 54:F8:2A:08:29:B8 -t 60 -w 2456e1b9-26e2-8f83-e744-f34f01e9d704 -r 2456e1b9-26e2-8f83-e744-f34f01e9d703 07:33:05.422 | INFO | ble_interface.py: Receiver set up 07:33:09.858 | INFO | ble_interface.py: Trying to connect with 54:F8:2A:08:29:B8: Unknown 07:33:25.471 | INFO | client.py: Services resolved for BleakClientWinRT (54:F8:2A:08:29:B8) 07:33:25.471 | INFO | ble_interface.py: Device 54:F8:2A:08:29:B8 connected 07:33:25.471 | INFO | ble_interface.py: Found write characteristic 2456e1b9-26e2-8f83-e744-f34f01e9d704 (H. 25) 07:33:25.471 | INFO | ble_interface.py: Found notify characteristic 2456e1b9-26e2-8f83-e744-f34f01e9d703 (H. 22) 07:33:25.573 | INFO | main.py: Running main loop!

Case 2: Connecting to a BLE device that requires a pairing code:

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-scan Started BLE scan

54:F8:2A:08:29:B0 (RSSI=80): Secure_BLE_SN:19 54:F8:2A:08:29:B8 (RSSI=-70): BLE-SN 29

Finished BLE scan

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-scan -d 54:F8:2A:08:29:B0 Started BLE scan

54:F8:2A:08:29:B0 (RSSI=79): Secure_BLE_SN:19 54:F8:2A:08:29:B8 (RSSI=-66): BLE-SN 29

Started deep scan of 54:F8:2A:08:29:B0

Found device 54:F8:2A:08:29:B0: Secure_BLE_SN:19 (out of 1) SERVICE 00001801-0000-1000-8000-00805f9b34fb (Handle: 1): Generic Attribute Profile CHARACTERISTIC 00002a05-0000-1000-8000-00805f9b34fb (Handle: 2): ['indicate'] DESCRIPTOR 00002902-0000-1000-8000-00805f9b34fb (Handle: 4): Client Characteristic Configuration SERVICE 00001800-0000-1000-8000-00805f9b34fb (Handle: 5): Generic Access Profile CHARACTERISTIC 00002a00-0000-1000-8000-00805f9b34fb (Handle: 6): ['read'] CHARACTERISTIC 00002a01-0000-1000-8000-00805f9b34fb (Handle: 8): ['read'] CHARACTERISTIC 00002a04-0000-1000-8000-00805f9b34fb (Handle: 10): ['read'] SERVICE 0000180a-0000-1000-8000-00805f9b34fb (Handle: 12): Device Information CHARACTERISTIC 00002a29-0000-1000-8000-00805f9b34fb (Handle: 13): ['read'] CHARACTERISTIC 00002a24-0000-1000-8000-00805f9b34fb (Handle: 15): ['read'] CHARACTERISTIC 00002a26-0000-1000-8000-00805f9b34fb (Handle: 17): ['read'] CHARACTERISTIC 00002a28-0000-1000-8000-00805f9b34fb (Handle: 19): ['read'] CHARACTERISTIC 00002a25-0000-1000-8000-00805f9b34fb (Handle: 21): ['read'] SERVICE 2456e1b9-26e2-8f83-e744-f34f01e9d701 (Handle: 23): Unknown CHARACTERISTIC 2456e1b9-26e2-8f83-e744-f34f01e9d703 (Handle: 24): ['read', 'write-without-response', 'write', 'notify'] DESCRIPTOR 00002902-0000-1000-8000-00805f9b34fb (Handle: 26): Client Characteristic Configuration CHARACTERISTIC 2456e1b9-26e2-8f83-e744-f34f01e9d704 (Handle: 27): ['write-without-response', 'write', 'notify'] DESCRIPTOR 00002902-0000-1000-8000-00805f9b34fb (Handle: 29): Client Characteristic Configuration

Completed deep scan of 54:F8:2A:08:29:B0

Finished BLE scan

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-serial -d 54:F8:2A:08:29:B0 -t 60 -r 2456e1b9-26e2-8f83-e744-f34f01e9d703 -w 2456e1b9-26e2-8f83-e744-f34f01e9d704 08:03:17.138 | INFO | ble_interface.py: Receiver set up 08:03:19.512 | INFO | ble_interface.py: Trying to connect with 54:F8:2A:08:29:B0: Unknown 08:04:19.556 | ERROR | main.py: Unexpected Error: TimeoutError() 08:04:19.557 | WARNING | main.py: Shutdown initiated 08:04:19.559 | INFO | windows_com0com.py: Stopping RX+TX loop 08:04:19.561 | INFO | main.py: Shutdown complete.

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>ble-serial -d 54:F8:2A:08:29:B0 -t 60 -r 2456e1b9-26e2-8f83-e744-f34f01e9d703 -w 2456e1b9-26e2-8f83-e744-f34f01e9d704 08:05:59.039 | INFO | ble_interface.py: Receiver set up 08:06:06.400 | INFO | ble_interface.py: Trying to connect with 54:F8:2A:08:29:B0: Unknown 08:06:38.290 | INFO | client.py: Services resolved for BleakClientWinRT (54:F8:2A:08:29:B0) 08:06:38.291 | INFO | ble_interface.py: Device 54:F8:2A:08:29:B0 connected 08:06:38.293 | INFO | ble_interface.py: Found write characteristic 2456e1b9-26e2-8f83-e744-f34f01e9d704 (H. 27) 08:06:38.293 | INFO | ble_interface.py: Found notify characteristic 2456e1b9-26e2-8f83-e744-f34f01e9d703 (H. 24) 08:06:38.428 | ERROR | main.py: Unexpected Error: OSError(22, 'The attribute requires authentication before it can be read or written', None, -2140864507, None) 08:06:38.429 | WARNING | main.py: Shutdown initiated 08:06:38.430 | INFO | windows_com0com.py: Stopping RX+TX loop Traceback (most recent call last): File "C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts\ble-serial-script.py", line 33, in sys.exit(load_entry_point('ble-serial==2.6.0', 'console_scripts', 'ble-serial')()) File "C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ble_serial\main.py", line 78, in launch Main(args).start() File "C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ble_serial\main.py", line 16, in start asyncio.run(self._run()) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64qbz5n2kfra8p0\lib\asyncio\runners.py", line 44, in run return loop.run_until_complete(main) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1520.0_x64qbz5n2kfra8p0\lib\asyncio\base_events.py", line 646, in run_until_complete return future.result() File "C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ble_serial\main.py", line 62, in _run await self.bt.disconnect() File "C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\ble_serial\bluetooth\ble_interface.py", line 113, in disconnect await self.dev.stop_notify(self.read_char) File "C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\bleak\backends\winrt\client.py", line 756, in stop_notify await characteristic.obj.write_client_characteristic_configuration_descriptor_async( OSError: [WinError -2140864507] The attribute requires authentication before it can be read or written

C:\Users\kamal.zarei\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts>

image

KamiK2K commented 1 year ago

I wanted to let everybody know that I was able to solve this and the pairing code can be entered on the windows Bluetooth application manager, in other words you just need the Bluetooth device address and the UUIDs of the read (notify/indicate) and write characteristics to send the ble-serial connection command establish the establish the BLE<->COM9 link. You can connect to your BLE device using the pairing code on windows before making the COM0<->BLE stream connection. Hope this helps, Kami :)