Page 1 of 1

BLE advertising packet

Posted: Sat Feb 01, 2020 1:01 am
by Beezy
I want to create a custom advertising packet to pass to gap_advertise(). Does anyone have any information how to do this? I tried to use the example in ble_advertise.py but I get OSError: [Errno 5] EIO when I pass it to the function. When I just pass a string value to the function such as "Micropython" I see nothing in the packet that is sent. My raw data is 0x4553503332 and the rest of the packet is 0's.
I have tried both using the latest build esp32-idf3-20200131-v1.12-107-g31ba06ce8.bin and have built myself with ESP-IDF 3.3.1 and micropython 1.12.
Any help or suggestions would be greatly appreciated.
Thanks in advance!

Re: BLE advertising packet

Posted: Sun Feb 02, 2020 11:24 pm
by jimmo
It might be that the payload is too long... I think that triggers the EIO error.

Can you share the code you used with ble_advertise.py and the call to gap_advertise().

Re: BLE advertising packet

Posted: Mon Feb 03, 2020 12:05 pm
by bart.cerneels
I've had success copying the advertising init code from here:
https://github.com/jamestiotio/deLIGHT/ ... ipheral.py

Code: Select all

def adv_encode(adv_type, value):
    return bytes((len(value) + 1, adv_type,)) + value

def adv_encode_name(name):
    return adv_encode(const(0x09), name.encode())

bt.gap_advertise(100, adv_encode_name('HelloFri3d'))
Proof that my BLE hello world works:
Image

My application is a conference badge for https://www.fri3d.be/

Re: BLE advertising packet

Posted: Tue Feb 04, 2020 10:52 pm
by Beezy
Thanks bart.cerneels that worked perfectly.

Jimmo, I was trying to take the return from advertise_payload() and pass it to gap_advertise. That was my problem, I misunderstood the comment above and thought I could just pass it as is, without using the other two functions.

Re: BLE advertising packet

Posted: Wed Feb 05, 2020 1:26 am
by jimmo
Beezy wrote:
Tue Feb 04, 2020 10:52 pm
Jimmo, I was trying to take the return from advertise_payload() and pass it to gap_advertise. That was my problem, I misunderstood the comment above and thought I could just pass it as is, without using the other two functions.
I'm not quite sure what you mean, you should be able to pass the result of advertising_payload directly to gap_advertise:

Code: Select all

from ble_advertising import advertising_payload
payload = advertising_payload(name='my-device')  #also, services=[..], appearance=N, etc
ble.gap_advertise(interval_us, adv_data=payload)
See https://github.com/micropython/micropyt ... erature.py

Re: BLE advertising packet

Posted: Thu Feb 06, 2020 8:11 pm
by Beezy
I was doing:

Code: Select all

payload =  advertising_payload(name='MicroPython', services=[ubluetooth.UUID(0x181A), ubluetooth.UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')])
bt.gap_advertise(100, payload)

Re: BLE advertising packet

Posted: Thu Feb 06, 2020 9:23 pm
by jimmo
Yeah, in that case what I said earlier -- the payload is too long. Try leaving out the services.

Re: BLE advertising packet

Posted: Tue Aug 18, 2020 6:59 am
by jha929
jimmo wrote:
Thu Feb 06, 2020 9:23 pm
Yeah, in that case what I said earlier -- the payload is too long. Try leaving out the services.
What is the maximum length of the payload?

Re: BLE advertising packet

Posted: Thu Sep 03, 2020 6:21 am
by jimmo
jha929 wrote:
Tue Aug 18, 2020 6:59 am
What is the maximum length of the payload?
31 bytes.

You can also use the scan response field (set resp_data in the gap_advertise call) which will allow active scanners to see an additional 31 bytes worth of data.