question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible to run notification on two different devices at once.

See original GitHub issue
  • bleak version: 0.6.4
  • Python version: 3.6.5
  • Operating System: OSX Catalina

Description

I’m trying to connect to some heart rate monitors and I can successfully connect to 1 and read the data properly, but I would like to connect and start notification on more than one device at the same time.

Is that possible with bleak?

What I Did

I defined an aync function connect_and_record which connects to a device using the BleakClient by UUID and starts notification with a handler that stores the data in a database.

I tried to run two devices using:

asyncio.gather(connect_and_record(uuid_1), connect_and_record(uuid_2)).

What happens is the first device connects and begins notification, and then the second device connects and it seems to cancel out the first one.

If you have any suggestions I would appreciate it. Thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kswann-imbcommented, Jul 10, 2020

Thanks @hbldh

0reactions
mabhijithncommented, May 25, 2021

I would like to restart this issue again. I am unable to read notifications from two devices simultaneuously. I am running:

  • bleak==0.11.0
  • linux kernel 5.4.0-72-generic
  • Linux Mint (Ubuntu 20.04)

The code I am running is given below based on Issue 345:

import asyncio
from bleak import BleakClient

erc1address = "80:EA:CA:70:00:01"
erc2address = "80:EA:CA:70:00:02"
read_characteristic = '16005991-b131-3396-014c-664c9867b917'

with open('file_one.csv','w') as f:
    f.write("ERC-1\n")

with open('file_two.csv','w') as f:
    f.write("ERC-2\n")

def callback_one(sender, data):
     with open('file_one.csv','a') as f:
         for i in range(len(data)):
             f.write(f'{data[i]}')
         
def callback_two(sender, data):
     with open('file_two.csv','a') as f:
         for i in range(len(data)):
             f.write(f'{data[i]}')

def run(addresses):
    loop = asyncio.get_event_loop()

    tasks = asyncio.gather(connect_one(addresses[0]) ,connect_two(addresses[1]))

    loop.run_until_complete(tasks)


async def connect_one(address):
    print("starting", address, "loop")
    async with BleakClient(address, timeout=20.0) as client:

        print("connect to", address)
        try:
            await client.start_notify(read_characteristic, callback_one)
            while 1:
                await asyncio.sleep(1)
        except Exception as e:
            if e is KeyboardInterrupt:
                await client.stop_notify(read_characteristic)

    print("disconnect from", address)

async def connect_two(address):
    print("starting", address, "loop")
    async with BleakClient(address, timeout=20.0) as client:
        print("connect to", address)
        try:
            await client.start_notify(read_characteristic, callback_two)
            while 1:
                await asyncio.sleep(1)
        except Exception as e:
            if e is KeyboardInterrupt:
                await client.stop_notify(read_characteristic)

    print("disconnect from", address)

if __name__ == "__main__":
    run(
        [erc1address, erc2address]
    )

I have while 1: await sleep(1) because I need to read notifications from both devices. If I replace that with just await sleep(10) for instance, then after 10 seconds, the second connected device notifications are read. However, my devices send notifications continuously, therefore I need to read data from both.

It would be great if any suggestions/help can be provided.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Will push notifications show up on two phones? - Quora
Yes, Push Notifications will show up on both phones as long the App is present on both.
Read more >
Is it possible to simultaneously receive a single push ...
No, it is not possible to simultaneously receive a single push notification (nor any other type of Duo two-factor authentication) on multiple devices...
Read more >
How to Sync and Dismiss Notifications Across Multiple ...
Use this method to mirror your notifications across multiple Android devices. You can also dismiss the synced notifications.
Read more >
iOS Message notifications - multiple dev… - Apple Community
iOS Message notifications - multiple devices. Often, when someone messages me, I will get the notification on my iPad, but not on my...
Read more >
How to Sync Notifications between Android Devices - Hongkiat
You need to have two or more devices running Android 5 or later. They can be phones or tablets or even smartwatches —...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found