XBee BLU Example of Discovery of BLE Devices

XBee BLU Overview

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 following example demonstrates how to discover a remote BLE device and list its attributes. In this example, two XBee BLU modules are used: one set up in MicroPython mode and the other in its default API mode.

In this application, the module in MicroPython mode acts as the device performing the discovery, while the module in API mode acts as the device being discovered.

--------------------------------------------------------
# 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
import gc

gc.enable()
gc.mem_alloc()

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

ble.active(True)
print("Started Bluetooth with address: {}".format(form_mac_address(ble.config("mac"))))
gc.collect()

# Change these three variables to your device's address, password, and address type.
# The address and address type can be discovered using ble.gap_scan().
REMOTE_ADDRESS = b'\xFF\xFF\xFF\x00\xFF\x00'
PASSWORD = "test"
address_type = ble.ADDR_TYPE_PUBLIC

print("Attempting to connect to:", REMOTE_ADDRESS)

if __name__ == '__main__':
    with ble.gap_scan(30000, interval_us=50000, window_us=50000) as scanner:
        # Loop through the available advertisements, blocking if there are none. Exits when the scan stops.
        for adv in scanner:
            print(adv)
    gc.collect()

# Disables the BLE interface.
ble.active(False)
-------------------------------------------------------

Note: At present, the XBee BLU module with firmware version 4001 does not support direct XBee-to-XBee communication over the Bluetooth interface. However, one can use the GATT or Bluetooth Identifier field to facilitate communication. This method is effective as long as the data being transmitted is viewable by other BLE devices.

Last updated: Aug 30, 2024

Recently Viewed

No recently viewed articles

Did you find this article helpful?