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

Standalone example somewhere? #63

Closed
ThomasAtBBTF opened this issue Jul 17, 2022 · 3 comments
Closed

Standalone example somewhere? #63

ThomasAtBBTF opened this issue Jul 17, 2022 · 3 comments

Comments

@ThomasAtBBTF
Copy link

I would like to use your library without the com0com component.
Just directly from a python application on windows.

Do you have any sample code which you could share?

I am working with an nRF52840 board with CircuitPython installed and would like to communicate over the Nordic UART characteristic to send and receive data.
Looking at my device with an Bluetooth tool I see the characteristic UUID' which you just added.

Thanks
Thomas

@Jakeler
Copy link
Owner

Jakeler commented Jul 17, 2022

Yes, this is what I came up with:

import asyncio, logging
from ble_serial.bluetooth.ble_interface import BLE_interface

def receive_callback(value: bytes):
    print("Received:", value)


async def hello_sender(ble: BLE_interface):
    while True:
        await asyncio.sleep(3.0)
        print("Sending...")
        ble.queue_send(b"Hello world\n")


async def main():
    # None uses default/autodetection, insert values if needed
    ADAPTER = "hci0"
    SERVICE_UUID = None
    WRITE_UUID = None
    READ_UUID = None
    DEVICE = "20:91:48:4C:4C:54"

    ble = BLE_interface(ADAPTER, SERVICE_UUID)
    ble.set_receiver(receive_callback)

    try:
        await ble.connect(DEVICE, "public", 10.0)
        await ble.setup_chars(WRITE_UUID, READ_UUID, "rw")

        await asyncio.gather(ble.send_loop(), hello_sender(ble))
    finally:
        await ble.disconnect()


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    asyncio.run(main())

Does that help?

Jakeler added a commit that referenced this issue Jul 17, 2022
@ThomasAtBBTF
Copy link
Author

ThomasAtBBTF commented Jul 17, 2022

Thank you for answering so quickly.
I will give it a try tomorrow morning.
But from what I see from the example, it looks understandable.
The only thing that is kind of "obscure" to me is identifying the "partner" to connect with.
Currently, I think the MAC-Address or Device-Address is problematic as it seems to me that the devices are changing this address "all the time".
There seems to be something like the device name which the tools on Android I have can "figure out" somehow.
I will let you know and will be happy to publish my results here.

@ThomasAtBBTF
Copy link
Author

After a while I got your example working today.
The problems, I had were more related to the device / service / characteristic identification than to the actual transfer of data.
A part of my problem was/ is the advertisement of the services / characteristics as my BLE device is actually mainly a keyboard with a Nordic UART as a "side" feature.
I am realizing that I have to modify my advertisement strategy to make the connection more seamless.
But anyway, thank you again for providing the sample code.

@Jakeler Jakeler closed this as completed Jul 22, 2022
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

No branches or pull requests

2 participants