BLE with Nicla Voice

Hi Community,

Recently I am trying to establish a stable connection between my computer (Mac) and my Nicla Voice via BLE.

The connections seems to be unstable. And I realised I am not the only one who faces such issues. I read on stackoverflow someone shared the same:

stackoverflow

Has anyone managed to get BLE working on your nicla voice successfully?

Here's my code on both the python and arduino:

/*
  Callback LED

  This example creates a Bluetooth® Low Energy peripheral with service that contains a
  characteristic to control an LED. The callback features of the
  library are used.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>
#include <Nicla_System.h>

BLEService ledService("00001101-0000-1000-8000-00805f9b34fb"); // create service

// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic switchCharacteristic("00001143-0000-1000-8000-00805f9b34fb", BLERead | BLEWrite);

// const int ledPin = LED_BUILTIN; // pin to use for the LED

void setup() {
  Serial.begin(9600);
  while (!Serial);
  
  pinMode(LED_BUILTIN, OUTPUT); // use the LED pin as an output

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1);
  }

  // set the local name peripheral advertises
  BLE.setLocalName("LEDCallback");
  // set the UUID for the service this peripheral advertises
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // assign event handlers for connected, disconnected to peripheral
  BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);

  // assign event handlers for characteristic
  switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
  // set an initial value for the characteristic
  switchCharacteristic.setValue(0);

  // start advertising
  BLE.advertise();

  Serial.println(("Bluetooth® device active, waiting for connections..."));
}

void loop() {
  // poll for Bluetooth® Low Energy events
  BLE.poll();
}

void blePeripheralConnectHandler(BLEDevice central) {
  // central connected event handler
  Serial.print("Connected event, central: ");
  Serial.println(central.address());
}

void blePeripheralDisconnectHandler(BLEDevice central) {
  // central disconnected event handler
  Serial.print("Disconnected event, central: ");
  Serial.println(central.address());
}

void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
  // central wrote new value to characteristic, update LED
  Serial.print("Characteristic event, written: ");

  if (switchCharacteristic.value()) {
    Serial.println("LED on");
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    Serial.println("LED off");
    digitalWrite(LED_BUILTIN, LOW);
  }
}

import asyncio
from bleak import BleakScanner, BleakClient
import logging
logging.basicConfig()
log = logging.getLogger(__name__)
# Discover Device, Get UUID + Name


async def discover_devices():
    devices = await BleakScanner.discover()
    for device in devices:
        print(device)


async def connect_to_device(address, loop):
    log.info(f"Connecting to {address}")
    async with BleakClient(address, loop=loop) as client:
        log.info("Connected. Reading data...")
        while True:
            data = await client.read_gatt_char("2A06")
            log.info(f"Received data: {data}")
# Fill this up
UUID = "861A4982-F830-BD55-C091-D5A7E3CBC0AE"

loop = asyncio.get_event_loop()
asyncio.run(discover_devices())
loop.run_until_complete(connect_to_device(UUID, loop))

Output Logs:

10:54:46.143 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
10:54:46.208 -> Connected event, central: 6c:7e:67:cb:7c:8f
10:54:47.362 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
10:54:48.351 -> Connected event, central: 6c:7e:67:cb:7c:8f
10:54:49.503 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
10:59:38.761 -> Bluetooth® device active, waiting for connections...
10:59:53.289 -> Connected event, central: 6c:7e:67:cb:7c:8f
10:59:54.408 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
10:59:54.671 -> Connected event, central: 6c:7e:67:cb:7c:8f
10:59:56.121 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
10:59:56.186 -> Connected event, central: 6c:7e:67:cb:7c:8f
10:59:56.548 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
11:10:32.914 -> Connected event, central: 6c:7e:67:cb:7c:8f
11:10:34.138 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
11:10:34.138 -> Connected event, central: 6c:7e:67:cb:7c:8f
11:10:35.354 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
11:10:35.816 -> Connected event, central: 6c:7e:67:cb:7c:8f
11:10:37.004 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
11:10:37.297 -> Connected event, central: 6c:7e:67:cb:7c:8f
11:10:38.846 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
11:10:39.342 -> Connected event, central: 6c:7e:67:cb:7c:8f
11:10:40.490 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
11:10:40.490 -> Connected event, central: 6c:7e:67:cb:7c:8f
11:10:41.713 -> Disconnected event, central: 6c:7e:67:cb:7c:8f
11:10:41.713 -> Connected event, central: 6c:7e:67:cb:7c:8f
11:10:42.834 -> Disconnected event, central: 6c:7e:67:cb:7c:8f

Thank you for your help

1 Like

This may help but it depends on what you are trying to do.

If you just want to be notified the a match has happened you can just update the BLE advertising.

Here is the code, ei_model.ino, I am using to notify me of matches via BLE adverts.
This has the advantages that

  1. it is broadcase so multiple receivers and use it
  2. it is in plain text so you can just look at the BLE connection name on your phone and see the current result.

For a receiver I am using an ESP32C3. See this project
https://www.forward.com.au/pfod/HomeAutomation/Wifi2BLE/ESP32C3_BLE_WIFI/index.html

ei_model.ino (5.4 KB)

Hey any resolution here, I'm facing the exact same issue. Thank you!