Software/Arduino Core/Connectivity

BLE

BLE on the MAYA-W476 module through the ArduinoBLE library, its peripheral and central roles, pairing, background servicing, and the MayaBLE beacon library.

intermediate3 min read

The MAYA-W476 module on the OV5640-W and TFT-W carriers has a BLE controller reached over USART6 using the HCI H4 protocol. ArduinoBLE, the standard Arduino library with an Ohm Lab HCI transport added, is the BLE API. Its documentation applies without change. MayaBLE is a small beacon library over ArduinoBLE.

Starting BLE

cpp
#include <NeuroN6.h>
#include <ArduinoBLE.h>

void setup() {
  NeuroN6_StreamUsbOnlyStart();       // the USB serial console, with no camera
  Serial.begin(115200);
  if (!BLE.begin()) {
    Serial.println("BLE.begin() failed");
    while (1) delay(1000);
  }
}

BLE.begin() powers the module and loads its firmware. No WiFi network and no MayaW476.h include are needed; WiFi can be started before or after it, and BLE.begin() and WiFi.begin() may be called from two different tasks. The module firmware is written to flash by the upload of any sketch that links ArduinoBLE, and BLE.begin() returns false when it is not there, printing [BLE] the wireless module did not start - no BLE. on the console. NeuroN6_StreamUsbOnlyStart() starts the core's USB task at once in a sketch without a camera; without the call it starts before the first loop(), and the USB serial console is served through setup() either way. USART6 is clocked from PLL1. The PS_CLOCK_ECO power profile turns PLL1 off, which stops BLE, and WiFi too.

Background servicing

A FreeRTOS task named ble, at priority idle + 3 with a 4 KiB stack, is created by BLE.begin() and woken by the USART6 receive interrupt. It services the stack as data arrives, so reads, writes, notifications and pairing are answered while loop() is busy or blocked in delay(). loop() does not need to call BLE.poll(), though the call works; BLE.poll(timeout) yields to lower-priority tasks while it waits for the controller. Characteristic and connection event handlers run in this task and are kept short. See FreeRTOS Under the Hood.

Stopping BLE

BLE.end() drops any connections, stops advertising and scanning, and resets the BLE controller. The module stays powered, so WiFi carries on. BLE.end() also empties the GATT database, so a sketch adds its services again before the next BLE.begin().

Connectable peripheral

The BlePeripheral example advertises a service with two characteristics: one a client can write, one the board increments and notifies.

cpp
BLEService                   svc      ("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEByteCharacteristic        ctrlChar ("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEUnsignedIntCharacteristic countChar("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);

void onCtrlWritten(BLEDevice central, BLECharacteristic characteristic) {
  Serial.println(ctrlChar.value(), HEX);          // runs in the BLE task
}

void setup() {
  NeuroN6_StreamUsbOnlyStart();
  Serial.begin(115200);
  BLE.begin();

  BLE.setLocalName("Neuro-N6");
  BLE.setDeviceName("Neuro-N6");
  BLE.setAdvertisedService(svc);
  svc.addCharacteristic(ctrlChar);
  svc.addCharacteristic(countChar);
  BLE.addService(svc);
  ctrlChar.setEventHandler(BLEWritten, onCtrlWritten);
  BLE.advertise();
}

void loop() {
  static uint32_t counter = 0;
  delay(2000);                                    // BLE carries on meanwhile
  countChar.writeValue(++counter);                // notifies subscribers
}

Beacon

A non-connectable beacon is an ordinary ArduinoBLE peripheral with BLE.setConnectable(false). The BleBeacon example advertises manufacturer data with a counter that changes once a second; setManufacturerData() takes effect on the next advertise().

cpp
uint8_t beacon[6] = { 0xFF, 0xFF, 'N', '6', 0, 0 };   // 0xFFFF is the test company ID

void setup() {
  NeuroN6_StreamUsbOnlyStart();
  BLE.begin();
  BLE.setLocalName("Neuro-N6");
  BLE.setConnectable(false);
  BLE.setManufacturerData(beacon, sizeof(beacon));
  BLE.advertise();
}

Central role

The board scans with BLE.scan(), BLE.scanForName(name), BLE.scanForUuid(uuid) or BLE.scanForAddress(address), reads each device found with BLE.available(), and connects to it with BLEDevice::connect(). discoverAttributes() then makes the peer's services and characteristics available through service() and characteristic(). BLE.available() polls once per call, so it is called from loop().

cpp
void setup() {
  NeuroN6_StreamUsbOnlyStart();
  BLE.begin();
  BLE.scanForUuid("180F");                     // battery service
}

void loop() {
  BLEDevice peripheral = BLE.available();      // one poll per call
  if (!peripheral) return;
  BLE.stopScan();
  if (peripheral.connect() && peripheral.discoverAttributes()) {
    BLECharacteristic level = peripheral.characteristic("2A19");
    // read or subscribe
  }
}

The board sees peripherals that use BLE legacy advertising. A peripheral that uses extended advertising only, which includes the GATT servers Windows hosts itself, is not seen.

Pairing

Pairing uses LE Secure Connections. It uses numeric comparison when both setDisplayCode() and setBinaryConfirmPairing() are set; with neither, it pairs without confirmation (Just Works). Pairing is allowed by default. BLE.setPairable(0) refuses it, and setPairable(2) allows one pairing only (Pairable::NO, YES, ONCE). BLE.setDisplayCode(cb) registers a function that receives the six-digit confirmation code to show, and BLE.setBinaryConfirmPairing(cb) a function that returns whether the user confirmed it. The key-store callbacks setStoreLTK, setGetLTK, setStoreIRK and setGetIRKs let the sketch keep bonding keys in storage of its own; BLE.paired() reports whether a connected peer's link is encrypted.

The pairing callbacks run in the ble task. While a confirmation waits, the BLE stack waits too, and so does any BLE call from loop().

cpp
void showCode(uint32_t code) {
  char s[8];
  snprintf(s, sizeof(s), "%06lu", (unsigned long)code);   // keeps leading zeros
  Serial.println(s);
}
bool confirmCode() { return true; }   // replace with a button or console check

void setup() {
  BLE.begin();
  BLE.setDisplayCode(showCode);
  BLE.setBinaryConfirmPairing(confirmCode);
}

BLE alongside WiFi

Both radios are on the same module and share one 2.4 GHz antenna, and both can run at once. WiFi.end() turns the WiFi radio off and leaves BLE working. See WiFi.

MayaBLE

MayaBLE advertises a named, non-connectable beacon over ArduinoBLE. MayaBLE.begin(name), with "Neuro-N6" as the default name, starts ArduinoBLE and returns true once the beacon is advertising; its second argument, bringUpModule, is ignored. isAdvertising() reports the state and printDiag() prints one console line with the state and the address. It can be included alongside ArduinoBLE. Anything beyond a named beacon uses ArduinoBLE directly, as the two examples filed with the library do.

Examples

BlePeripheral and BleBeacon are under File > Examples > MayaBLE. The stock Peripheral and Central examples under File > Examples > ArduinoBLE also apply.

Licence

ArduinoBLE is LGPL-2.1. Statically linking it into a firmware image carries a relinking obligation; a MayaBLE sketch links ArduinoBLE, so the same applies to it. The NXP WiFi driver source is BSD-3-Clause. The IW610 firmware is distributed under NXP's proprietary licence, LA_OPT_NXP_Software_License.txt, with a distribution grant under its section 2.3, and is written to the board's flash for any sketch that links MayaW476, MayaBLE or ArduinoBLE. See Licensing.