Skip to content

PC and bleak work, Android and bleekware don't. #6

@martan3d

Description

@martan3d

I'm running windows 11 and have two Android devices, a phone and a tablet, both running Android 12. The scan works fine on both, both see the devices. The target device is an Xbee 3 which requires I first send an 'unlock' message and handle the response.

After the scan I can ID the device and open a client. On the PC, this works as expected. I send the message, wait for the notify and I get the Xbee response packet.

Android, nothing. Perhaps it's my devices? Perhaps Android 12 is marginal? I don't know. If anyone can try this on their device, I'd appreciate it. Thanks.

import asyncio

import toga
from toga import Button, MultilineTextInput, Label, TextInput
from toga.style import Pack
from toga.style.pack import COLUMN, CENTER

if toga.platform.current_platform == 'android':
    from .bleekWare import bleekWareError as BLEError
    from .bleekWare.Scanner import Scanner as Scanner
    from .bleekWare.Client import Client as Client
else:
    from bleak import BleakError as BLEError
    from bleak import BleakScanner as Scanner
    from bleak import BleakClient as Client

UID1 = "00002a00-0000-1000-8000-00805f9b34fb"
UID2 = "00002a29-0000-1000-8000-00805f9b34fb"
UID3 = "00002a24-0000-1000-8000-00805f9b34fb"
UID4 = "00002a26-0000-1000-8000-00805f9b34fb"
UID5 = "7dddca00-3e05-4651-9254-44074792c590"
UID6 = "f9279ee9-2cd0-410c-81cc-adf11e4e5aea"

UNLOCK0 = [126, 0, 130, 44, 1, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 234]

class BleScannerApp(toga.App):
    """A small App to demonstrate Bluetooth LE functionality with bleekWare.

    bleekWare replaces Bleak on the Android platform when working with
    Toga and BeeWare (see the conditional import above).

    This app demonstrates several possibilities to perform a scan and
    read the advertised data.
    """

    def startup(self):

        self.discover_button = Button(
            'Scan',
            on_press=self.start_discover,
            style=Pack(width=120, height=60, margin_top=6, background_color="#cccccc", color="#000000", font_size=12)
        )

        self.working_text = Label("", style=Pack(font_size=12, color="#000000"))

        scan_content = toga.Box(style=Pack(direction=COLUMN, alignment=CENTER, margin_top=5))
        scan_content.add(self.discover_button)
        scan_content.add(self.working_text)

        self.scroller = toga.ScrollContainer(content=scan_content)

        self.main_window = toga.MainWindow(resizable=True)
        self.main_window.content = self.scroller
        self.main_window.show()


    async def start_discover(self, widget):
        self.working_text.text = "Scanning for Receivers..."
        data = await Scanner.discover(return_adv=False)
        print (data)

        self.working_text.text = ""
        scan_content = toga.Box(style=Pack(direction=COLUMN, alignment=CENTER, margin_top=5))
        self.button_list = []

        for k in data:
            s = str(k).split(' ')
            if s[1] == 'None': continue
            addr = s[0][:-1]
            fmstring = "{} {}".format(s[1], addr)
            self.button_list.append([s[1], addr])
            scan_content.add(
                toga.Button(id=addr, text=fmstring,
                    on_press = self.connectToClient,
                    style=Pack(width=220, height=120, margin_top=6, background_color="#aaaaaa", color="#000000", font_size=16),
                )
            )

        self.scroller = toga.ScrollContainer(content=scan_content)
        self.main_window.content = self.scroller
        self.main_window.show()


    async def connectToClient(self, button):
        print ("connectToClient")
        async with Client(button.id) as client:
            #status = await client.connect()
            
            print ("Write")

            wrdata = bytearray(UNLOCK0)
            result = await client.write_gatt_char(UID5, wrdata)
            print (result)

            print ("Wait notification")
            await client.start_notify(UID6, notification_handler)

            await asyncio.sleep(3.0)
            await client.stop_notify(UID6)

    
def notification_handler(sender, data):
    print (sender, data)


def main():
    return BleScannerApp()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions