MarkusPiotrowski / bleekWare

A limited Bleak complement for accessing Bluetooth LE on Android in Python apps made with BeeWare.
MIT License
5 stars 2 forks source link

"write_gatt_char" can't work communicating with esp32 #4

Open operator000 opened 3 weeks ago

operator000 commented 3 weeks ago

promlem:

I met another problem. I'm writing an app on android to commuincate with esp32. I made changes on the example code. But "write_gatt_char" can't work and there's no errors presented.

details:

part of the code:

    # seatch and connect
    async def start_discover(self, widget):
        """Use class method Scanner.discover().

        'return_adv=True' returns a dic.
        'return_adv=False' would just return a list of discovered devices.
        """
        self.print_device('Start BLE scan...', clear=True)
        self.print_data(clear=True)

        scanner = Scanner()
        await scanner.discover()
        self.print_device('...scanning stopped.')
        result=scanner.discovered_devices_and_advertisement_data

        for key in result.keys():
            self.print_device("result")
            try:
                name=result[key][0]
                self.print_device(str(name))
            except Exception as e:
                self.print_data(str(e))

        try:
            self.print_device("connecting...")
            self.client = Client(result["08:D1:F9:DC:B7:46"][0]) # target device
            #client = Client(result["E8:FD:F8:6B:B1:80"][0]) # unknown device
            #client = Client(result["EC:FA:5C:64:E5:CB"][0]) # a speaker

            self.print_device("connecting...")
            res=await self.client.connect()
            self.print_device(str(res))
            self.print_device("connecting...")
            self.print_device(str(self.client.is_connected))

            self.write_something()
        except Exception as e:
            self.print_data(str(e))

    # write
    def write_something(self):
        try:
            self.client.write_gatt_char('beb5483e-36e1-4688-b7f5-ea07361b26a8', b'hello')
        except Exception as e:
            self.print_data(str(e))

    def disconnect(self):
        self.client.disconnect()

and the output on phone :

Start BLE scan...
...scanning stopped.
result
08:D1:F9:DC:B7:46:MyESP32
result
17:82:BF:84:D1:F6: None
result
C0:00:00:04:1D:E3:YD_1DE3
result
03:49:45:5F:9C:D8: Smart Guard 2LL0
result
4C:C6:4C:41:C6:B1:DL01
connecting...
connecting...
True
connecting...
True

(there's no errors collected) esp32 is expected to return a value by serial, like

*********
New value: hello
*********
*********
New value: hello
*********

and I used Bluetooth LE Explorer to test it, and it worked: image esp32 also got themessage. Then I used a WeChat applet to test it again. it also worked. What's going wrong? I sincerely hope to get your help.

MarkusPiotrowski commented 3 weeks ago

write_gatt_char() is also an async method that must be awaited, which means that also your write_something method must be declared as async:

# write
async def write_something(self):
    try:
        await self.client.write_gatt_char('beb5483e-36e1-4688-b7f5-ea07361b26a8', b'hello')
    except Exception as e:
        self.print_data(str(e))

Edit: not awaiting an async method just issues a Warning (RuntimeWarning: coroutine '... ' was never awaited) but not an Exception. This is probably why your code doesn't report an error.

operator000 commented 2 weeks ago

That's really the problem. Thank you very much!