Transmitting 8 digit ID #

I have a 16 digit ID number that I am trying to transmit through the Nano 33 BLE module. Currently I am using the Light Blue app to read the data but I can't get it to read more than one character at a time. Eventually I plan to connect to a custom app on the android that is being built right now as well so BlueLight is a substitute till that app is done.
I am using Device Information "180A" for my service UUID and Object ID "2AC3" for my characteristic UUID. I have a test ID that I am trying to send in one go but have hit a wall in my code as it will only read one character of the ID. I'm sure I missed an important piece to include so please ask any other questions and all help is appreciated!

#include <ArduinoBLE.h>

BLEService IDService("180A"); // BLE Generic Attribute Service

BLECharCharacteristic IDCharacteristic("2AC3", BLERead);

char ID[8] = {1, 2, 3, 4, 5, 6, 7, 8};

void setup() {
  Serial.begin(9600);
  while (!Serial);

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

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("Here Fishy Fishy");
  BLE.setAdvertisedService(IDService);

  // add the characteristic to the service
  IDService.addCharacteristic(IDCharacteristic);

  // add service
  BLE.addService(IDService);

  // set the initial value for the characteristic:
  IDCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());
  
    // while the central is still connected to peripheral:
    while (central.connected()) {

      IDCharacteristic.writeValue(*ID);

    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

char characteristic is for a single char. You could step through an array of chars, but I would think that the ID would be better sent using the String characteristic or perhaps the text characteristic with a specified length.

Thank you for the response. I was looking at characteristics on the page below:

On this page I don't see a string or text characteristic. Do you mind explaining what the syntax would look like?

BLEWordCharacteristic() seems like it may be the same idea but I'm not sure how I would initialize that as a variable.

I'm not clear when the page you cited was written, but the String Characteristic was added to the library 6 years ago.

BLEStringCharacteristic(const char* uuid, unsigned char properties, int valueSize);

The reason that it needs a length parameter is that the String is converted to a c-string by the library behind the scenes.

An example sketch is in this thread.

https://forum.arduino.cc/t/ble-code-to-take-string-input-from-another-bluetooth-enabled-deice/643877

This worked, but only once. I was able to adapt the code to fit my application and it worked the first time I tested it. However, since the first test the BT module won't connect to the android at all. Using BLE Scanner or Light Blue the device no longer appears to connect with.

Here is my code, I mustve changed something small that I didn't notice. Code compiles just fine.

#include <ArduinoBLE.h> //Bluetooth Library

BLEService IDService("180A"); // BLE Generic Attribute Service UUID

BLECharCharacteristic IDChartest("2AC3", BLERead);  //Object ID UUID, read only
BLEStringCharacteristic IDString("2AF8", BLERead, 8);  //Fixed String 8 UUID, read only

char ID = 7;
String ID2 = "1A2B3C4D";
String ID3 = "";

void setup() {
  Serial.begin(9600);
  while (!Serial);

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

  // set advertised local name and service UUID:
  BLE.setLocalName("Here Fishy Fishy");
  BLE.setAdvertisedService(IDService);

  // add the characteristic to the service
  IDService.addCharacteristic(IDString);
  IDService.addCharacteristic(IDChartest);
  
  // add service
  BLE.addService(IDService);

  // set the initial value for the characteristic:
  IDString.writeValue(ID3);
  IDChartest.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());  // print the central's MAC address:
  
    // while the central is still connected to peripheral:
    while (central.connected()) {
      IDString.writeValue(ID2);
      IDChartest.writeValue(ID);
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());  // print the central's MAC address:
  }
}

I can't confirm this behavior. I can connect, disconnect, and then reconnect.

You may be running into an issue with this which needs the monitor to be open.
while (!Serial);

In this block of code I would add a millis() timer or some delay() so that the values are not sent over and over again in a tight loop. There is probably some message spacing built into the BLE core, but it's best not to spam the central.

while (central.connected()) {
      IDString.writeValue(ID2);
      IDChartest.writeValue(ID);
    }

Thank you! Everything seems to be working how I hoped up to this point. Your help was crucial in that!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.