XBee BLU Extended Advertisement

XBee BLU Extended Advertising

The XBee BLU is a Bluetooth Low Energy (BLE) XBee module. It is designed to allow a device, such as an Arduino or other processor, to communicate via a Bluetooth-enabled device, such as a phone or a PC.

Some potential applications for this product include:

  • Solar panels for home use
  • Solar panel farms
  • Local sensors, such as water sensors or gas sensors

The standard advertisement function allows you to broadcast up to 22 characters. In some cases, you may need to broadcast a larger message, and this is where the Extended Advertisement function comes into play.

To use this function on the XBee3 BLU or other BLE-supported modules, you will need to utilize the embedded MicroPython functionality.

The example code below demonstrates how to set the advertisement name to:
"XBee BLE Extended Advertising Example"

# Copyright (c) 2020, Digi International, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from digi import ble

TYPE_COMPLETE_NAME = 0x09
# The value listed below can be adjusted to accept any ASCII value up to 191 bytes
ADV_NAME_DATA = 'XBee BLU Extended Advertising Example'

TYPE_ADVERTISING_INTERVAL = 0x1A
ADV_INTERVAL_DATA = 100000

TYPE_ADV_URI = 0x24
ADV_URI_DATA = 'https://hub.digi.com/support/products?type=documentation'

def form_adv_field(adv_data_type, data):
    if isinstance(data, str):
        data = data.encode()

    payload = bytearray()
    payload.append(len(data) + 1)
    payload.append(adv_data_type)
    payload.extend(data)
    return payload

def form_mac_address(addr: bytes) -> str:
    return ":".join('{:02x}'.format(b) for b in addr)

# Turn on Bluetooth
ble.active(True)
print("Started Bluetooth with address: {}".format(form_mac_address(ble.config("mac"))))

# Advertising interval is displayed in units of 0.625ms
adv_interval = int((ADV_INTERVAL_DATA // 1000) / 0.625)
adv_interval_bytes = adv_interval.to_bytes(2, 'little')
# {Advertising field value: advertising data}
adv_field_dict = {TYPE_COMPLETE_NAME: ADV_NAME_DATA,
                  TYPE_ADVERTISING_INTERVAL: adv_interval_bytes,
                  TYPE_ADV_URI: ADV_URI_DATA}

adv_payload = bytearray()
for adv_type, data in adv_field_dict.items():
    adv_payload.extend(form_adv_field(adv_type, data))

print(adv_payload)

ble.gap_advertise(ADV_INTERVAL_DATA, adv_payload, ble.BLE_ADV_EXTENDED_MODE)

Last updated: Aug 30, 2024

Recently Viewed

No recently viewed articles

Did you find this article helpful?