Skip to content

Instantly share code, notes, and snippets.

@N3MIS15
Last active July 10, 2024 13:54
Show Gist options
  • Save N3MIS15/589062360a658a36b9c810fec8bb0c91 to your computer and use it in GitHub Desktop.
Save N3MIS15/589062360a658a36b9c810fec8bb0c91 to your computer and use it in GitHub Desktop.
micropython iBeacon example
import struct
import ubluetooth as bt
from micropython import const
MANUFACTURER_ID = const(0x004C)
DEVICE_TYPE = const(0x02)
DATA_LENGTH = const(0x15)
BR_EDR_NOT_SUPPORTED = const(0x04)
FLAG_BROADCAST = const(0x01)
MANUFACTURER_DATA = const(0xFF)
def convert_tx_power(dbm):
return dbm + 0xFF + 1
class iBeacon():
def __init__(self, ble, uuid, major, minor, tx_power):
# Setup BLE
self.ble = ble
self.ble.active(False)
self.ble.active(True)
print("BLE Activated")
self.uuid = uuid
self.major = major
self.minor = minor
self.tx_power = convert_tx_power(tx_power)
self.adv_payload = self.create_payload()
def create_payload(self):
payload = bytearray()
#Set advertising flag
value = struct.pack('B', BR_EDR_NOT_SUPPORTED)
payload += struct.pack('BB', len(value) + 1, FLAG_BROADCAST) + value
# Set advertising data
value = struct.pack('<H2B', MANUFACTURER_ID, DEVICE_TYPE, DATA_LENGTH)
value += self.uuid
value += struct.pack(">2HB", self.major, self.minor, self.tx_power)
payload += struct.pack('BB', len(value) + 1, MANUFACTURER_DATA) + value
return payload
def advertise(self, interval_us=100000):
print("Advertising: " + str(self.adv_payload))
self.ble.gap_advertise(None)
self.ble.gap_advertise(interval_us, adv_data=self.adv_payload, connectable=False)
def update(self, major, minor, advertise_interval):
self.ble.active(False)
self.major = major
self.minor = minor
self.adv_payload = self.create_payload()
self.ble.active(True)
self.advertise(advertise_interval)
def demo():
beacon = iBeacon(
ble = bt.BLE(),
uuid = bytearray((
0xa4, 0x95, 0xbb, 0x10, 0xc5, 0xb1, 0x4b, 0x44,
0xb5, 0x12, 0x13, 0x70, 0xf0, 0x2d, 0x74, 0xde
)),
major = 62,
minor = 1050,
tx_power = -50,
)
beacon.advertise()
if __name__ == "__main__":
demo()
while True:
pass
@dinosaurclover
Copy link

Nice piece of work.
Do you know where I can find the full documentation of the structure that needs to be passed gap_advertise.
The information int the micropython docs is sparse to say the least.
It lists the flag names but doesn't explain what they mean.

@N3MIS15
Copy link
Author

N3MIS15 commented Aug 20, 2022

The advertising data comes from the ble spec. I would suggest looking up "ibeacon packet structure" to understand what needs to be used to advertise.

@dinosaurclover
Copy link

Thanks for the contribution, N3MIS15.

It doesn't seem to have any relevance to controlling the advertisement tx power using the currently available micropython library. Am I missing something here/

I was hoping that someone might be able to point me towards full documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment