adafruit / Adafruit_CircuitPython_BLE

Bluetooth Low Energy (BLE) library for CircuitPython
MIT License
124 stars 57 forks source link

uuid has no attribute pack_into #77

Closed mak3rm1k3 closed 4 years ago

mak3rm1k3 commented 4 years ago

I'm trying this out for the first time on a rpi zero w, and after installing the library I keep getting an error:

AttributeError: 'UUID' object has no attribute 'pack_into'

Running with Python 3 here is a sample of my code:

import adafruit_ble
from adafruit_ble.advertising import standard
import adafruit_ble_apple_notification_center as ancs

radio = adafruit_ble.BLERadio()
a = standard.SolicitServicesAdvertisement()
a.solicited_services.append(ancs.AppleNotificationCenterService)

Looking more closely the error is in line 63 of uuid/init.py:

def pack_into(self, buffer, offset=0):
        """Packs the UUID into the buffer at the given offset."""
        self.bleio_uuid.pack_into(buffer, offset=offset) #<-- this is where the error is coming from

any advice?

Edit: Seems like the issue is actually coming from declaring the advertisement with SolicitServicesAdvertisement() because the advertisement is a Nonetype and the radio advertising fails.

Traceback (most recent call last):
  File "ancs.py", line 16, in <module>
    radio.start_advertising(a)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_ble/__init__.py", line 174, in start_advertising
    advertisement_bytes = bytes(advertisement)
TypeError: __bytes__ returned non-bytes (type bytearray)
tannewt commented 4 years ago

Hi! Good find. I think the issue is that I didn't add pack_into into the Blinka bleio library here: https://github.com/adafruit/Adafruit_Blinka_bleio/blob/master/_bleio.py#L318

Would you mind adding it?

The second error looks like another issue. CPython sanity checks that bytes does as advertised. CircuitPython doesn't check. It should be an easy fix though, makes sure bytes methods return a bytes() of the bytearray.

Mind looking at that too? Sounds like you are setup to test it. I can help if you have any Git/GitHub questions.

Thanks!

mak3rm1k3 commented 4 years ago

Thanks so much for getting back to me on this. I'm pretty green on this level of python (hardware bytes level), so I'd love help adding in pack_into to the Blinka library. What exactly do you need me to add?

def pack_into(self, buffer, offset=0):
        """Packs the UUID into the buffer at the given offset."""
        self.bleio_uuid.pack_into(buffer, offset=offset)

Something like this into the UUID class?

I think I understand your second suggestion, to just inspect the type that is returned from a bytes() when assigned to advertisement_bytes? If it doesn't return bytes, though, not sure what exception to use. Any advice?

I really appreciate your help and am very excited to get this going.

tannewt commented 4 years ago

Thanks so much for getting back to me on this. I'm pretty green on this level of python (hardware bytes level), so I'd love help adding in pack_into to the Blinka library. What exactly do you need me to add?

Awesome! Thank you! I'm available on our Discord too. Just @tannewt there.

def pack_into(self, buffer, offset=0):
        """Packs the UUID into the buffer at the given offset."""
        self.bleio_uuid.pack_into(buffer, offset=offset)

Something like this into the UUID class?

Yup! Same signature (the def line) but with code inside that sets the values in the buffer. This is the C version: https://github.com/adafruit/circuitpython/blob/master/ports/nrf/common-hal/_bleio/UUID.c#L70

I think I understand your second suggestion, to just inspect the type that is returned from a bytes() when assigned to advertisement_bytes? If it doesn't return bytes, though, not sure what exception to use. Any advice?

Not quite. You'll want to find the advertisement class and fix it so that it returns bytes. I think you'll want to change the line here to:

return bytes(encode_data(self.data_dict))

I really appreciate your help and am very excited to get this going.

Great! I'm happy to help you fix it and appreciate your willingness to jump in.

Kimpeh commented 4 years ago

I also encountered this issue "AttributeError: 'UUID' object has no attribute 'pack_into'". Can you please advise as to what code needs to be added to the Blinka bleio library?

Thanks!

tannewt commented 4 years ago

@makermelissa Any chance you could add pack_into into Blinka_BLE?

makermelissa commented 4 years ago

I missed the tag. I just created a new issue at https://github.com/adafruit/Adafruit_Blinka_bleio/issues/10

tannewt commented 4 years ago

Ok, closing this in favor of https://github.com/adafruit/Adafruit_Blinka_bleio/issues/10