Bluetooth Low Energy

Bluetooth Low Energy (BLE) is an independent form of wireless communication that is used with low-energy technology, some of the most notable being smart watches, heart rate sensors and wireless earphones.

The VBOX Touch also supports communication on this protocol.

Callbacks

The callback for BLE processes is set using set_BLE_cb. An example is shown below.

from net_new import network

def BLE_cb(event_code, data):
    print("EVENT_CODE: {}, DATA: {}".format(event_code, data))

network.set_BLE_cb(BLE_cb)

As you can see above the function passed will be given 2 parameters on each callback. An event code (the reason for the callback) and any relevant data to this code. The event code is a flag that can currently take seven different values:

  • EVENT_BLE_SUBSCRIPTION

  • EVENT_BLE_READ

  • EVENT_BLE_TIMEOUT

  • EVENT_BLE_FINISHED_ROLE

  • EVENT_BLE_FINISHED_SUBSCRIBE

  • EVENT_BLE_FINISHED_WRITE

  • EVENT_BLE_FINISHED_READ

These constants can be accessed from the net_new module, for example net_new.EVENT_BLE_READ.

Scan

The scan function can be used similarly to other connection types to find nearby BLE devices. The first parameter is used to set the type of scan required (BLE) and the second is used to specify the length of the scan. The devices found are stored in a dictionary with the device name as the key and its address as the value.

network.scan('BLE', time=10000)
print(network.BLE_devices_found)

Setting a role

The BLE protocol operates differently to Bluetooth Classic. They both operate on point-to-point connections however Bluetooth LE also supports Broadcast and Mesh communications. Because of this the way it connects to a device needs to be different.

A Bluetooth Low Energy (LE) device needs to be given a “role” in a connection, this is either: Central, Peripheral, or Simultaneous. Central takes the role of a client device and makes connection to the Peripheral, which is a server device. The Peripheral is typically a battery powered device like a sensor, and the Central, is often a smartphone or a computer.

The set_BLE_role function can be used to set the BLE role of the VBOX touch. It takes one parameter which can be “disabled”, “central”, “peripheral” or “simultaneous”.

network.set_BLE_role("central")

Subscribing to a characteristic

BLE peripheral devices share data via characteristics. These are data containers that can be read from, written to, subscribed to and more. Related characteristics are grouped together in services. Subscribing to a characteristic means that the central device will be sent a message every time the characteristic is updated on the peripheral device. subscribe_to_characteristic takes three arguments, the BLE address of the peripheral device, the UUID of the service that contains the characteristic that is to be subscribed to and the UUID of the characteristic itself. It will check that the VBOX is set to central role and then subscribe to the characteristic. The data from the subscription will be passed to the BLE callback function.

network.subscribe_to_characteristic(device_uuid, service_uuid, characteristic_uuid)

Writing to a characteristic

A Peripheral device is used to write to this characteristic and let the Central subscribed devices know that this information has been updated. write_to_characteristic takes a BLE address, service UUID, characteristic UUID and the data to be written as arguments.

network.write_to_characteristic(device_uuid, service_uuid, characteristic_uuid, hex(1024))

Reading from a characteristic

Getting the data from the characteristic is done by reading it in. Passing the read_from_characteristic function a BLE address, service UUID and a list of characteristics to be read from as arguments will allow this to be done. The data received will be passed to the BLE callback function.

network.read_from_characteristics(device_uuid, "180A", network.BLE_DIS_list)