How to get BLE beacons

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to get BLE beacons

Post by jimmo » Wed Aug 04, 2021 4:58 am

aofek54 wrote:
Sat Jul 31, 2021 5:09 am
The scanning gives all kind of problems like ble.gap_scan() doesn't work
Could you please inspect this code?
Your code doesn't do anything with the scan IRQ.

Code: Select all

    def _irq(self, event, data):
        if event == _IRQ_SCAN_RESULT:
            addr_type, addr, adv_type, rssi, adv_data = data
            return data
The "return data" here deosn't do anything. At a minimum you could print out the information.

Code: Select all

    def _irq(self, event, data):
        if event == _IRQ_SCAN_RESULT:
           addr_type, addr, adv_type, rssi, adv_data = data
           print("Scan result: {}/{} {} {} {}", addr_type, hexlify(addr, ":"), adv_type, rssi, hexlify(adv_data))
You can use the helpers in ble_advertising.py to process the adv_data. Note you'll need to use "from binascii import hexlify" to use that example above.

Also be aware that the memory used for the address and adv_data is owned by bluetooth, so you need to copy it if you're going to reference it later. See the second paragraph of https://docs.micropython.org/en/latest/ ... th.BLE.irq

I would recommend taking a look at aioble -- it makes using Bluetooth much simpler.

https://github.com/micropython/micropyt ... oth/aioble

Here's a simple scanner with aioble:

Code: Select all

async with aioble.scan() as scanner:
    async for result in scanner:
        if result.name():
            print(result, result.name(), result.rssi, result.services())
            

Post Reply