Array initialization

Hello,

I set in my code:

uint8_t macIndex = 0;
const uint8_t  macs[][6] = {
  {0x34, 0xb4, 0x72, 0x4d, 0x63, 0xc6 },
  {0xb0, 0x7e, 0x11, 0xdd, 0xc7, 0x97 }
};
uint8_t mac_addr_Server_Used[6];

Can i set value as

mac_addr_Server_Used[6]=macs[macIndex][6];

After declaration mac_addr_Server_Used ?

Thank you.

Yes, you can do that in the setup() function for example.

You are going out of the array bounds by the way.

No because the maximum index of the arrays is 5.

6 elements, indexed 0 to 5.


No because it only copies one byte... if you want all 6 (probably do!) you need to copy them all.

a7

No, you cannot assign C-style arrays like that.
What you wrote means: assign the (nonexistent) seventh element of the MAC address with index macIndex in macs to the (nonexistent) seventh element of the array mac_addr_Server_Used`.
(The arrays have length 6, so the maximum index you can access is 5.)

In this case, it might make sense to store a pointer to the MAC address instead of storing a copy:

uint8_t macIndex = 2;
const uint8_t  macs[][6] = {
  {0x34, 0xb4, 0x72, 0x4d, 0x63, 0xc6 },
  {0xb0, 0x7e, 0x11, 0xdd, 0xc7, 0x97 },
  {0x01, 0x02, 0x03, 0xdd, 0xc7, 0x97 },
};
const uint8_t (*mac_addr_Server_Used)[6] = nullptr;

void setup() {
    mac_addr_Server_Used = &macs[macIndex];
    // use *mac_addr_Server_Used
}

Unless you're using an AVR board, use a C++ std::array instead of a C-style array: they can be copied without any problem, and resolve most of the issues with C-style arrays.

#include <array>
uint8_t macIndex = 2;
using mac_t = std::array<uint8_t, 6>;
const mac_t macs[] = {
  {0x34, 0xb4, 0x72, 0x4d, 0x63, 0xc6 },
  {0xb0, 0x7e, 0x11, 0xdd, 0xc7, 0x97 },
  {0x01, 0x02, 0x03, 0xdd, 0xc7, 0x97 },
};

void setup() {
  mac_t mac_addr_Server_Used = macs[macIndex];
  // use mac_addr_Server_Used
}

No. Use a 'for' loop or memcpy().

Isn't it a completely legit 8-bit/byte assignment? Out of bounds, though..

The original expression is an assignment of one 8-bit byte. At an invalid index, but that is beside the point.

Ask yourself how, if what the OP had was supposed to copy the whole array, wou,d you express what actually happens, what you might want to do one day, which is copy just one byte over…

a7

Exactly, it assigns a single element, not the array (assuming a valid index).

If i use your code:

#include <array>
uint8_t macIndex = 2;
using mac_t = std::array<uint8_t, 6>;
const mac_t macs[] = {
  {0x34, 0xb4, 0x72, 0x4d, 0x63, 0xc6 },
  {0xb0, 0x7e, 0x11, 0xdd, 0xc7, 0x97 },
  {0x01, 0x02, 0x03, 0xdd, 0xc7, 0x97 },
};

void setup() {
  mac_t mac_addr_Server_Used = macs[macIndex];
  // use mac_addr_Server_Used
}

How can i set on my BLEAdress and display this array with Serial.println in my code?

BLEAddress CamServer(mac_addr_Server_Used);

/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (memcmp(advertisedDevice.getAddress().getNative(),CamServer.getNative(), 6) == 0) {
      Serial.print("BLE Advertised Device checked: ");
      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks

BLEAddress CamServer(mac_addr_Server_Used.data());

You can access the individual components as with any array: mac_addr_Server_Used[i] with 0 <= i < 6.

I get now :slight_smile:

'mac_addr_Server_Used' was not declared in this scope

maybe because is initialize on setup() function?

Yes, you can only use a variable after declaring it.

You need to declare the mac_addr_Server_Used variable in scope with the CamServer variable. You could make a global declaration and put mac_t mac_addr_Server_Used; below the .. macs[] ..; declaration.

I get this error now:

no match for 'operator=' (operand types are 'mac_t' {aka 'std::array<unsigned char, 6>'} and 'const mac_t [6]' {aka 'const std::array<unsigned

in line:

mac_addr_Server_Used = macs[macIndex];

Declaration:

uint8_t macIndex = 2;
using mac_t = std::array<uint8_t, 6>;
const mac_t  macs[][6] = {
  {0x34, 0xb4, 0x72, 0x4d, 0x63, 0xc6 },
  {0xb0, 0x7e, 0x11, 0xdd, 0xc7, 0x97 },
  {0x01, 0x02, 0x03, 0xdd, 0xc7, 0x97 }
};
mac_t mac_addr_Server_Used;
BLEAddress CamServer(mac_addr_Server_Used.data());

/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (memcmp(advertisedDevice.getAddress().getNative(),CamServer.getNative(), 6) == 0) {
      Serial.print("BLE Advertised Device checked: ");
      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks


void scanDevicesBLE(){

 // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
  
}

void setup()

{

mac_addr_Server_Used = macs[macIndex];


}

Instead of having multiple instances of the same address, do it like this:

//Declare constants for mac-address indices
const uint8_t CAM_SERVER = 2;

//Declare mac-addresses
const uint8_t MAC_ADDRESS_LEN = 6;
using mac_t = std::array<uint8_t, MAC_ADDRESS_LEN>;
const mac_t macs[] = {
  {0x34, 0xb4, 0x72, 0x4d, 0x63, 0xc6 },
  {0xb0, 0x7e, 0x11, 0xdd, 0xc7, 0x97 },
  {0x01, 0x02, 0x03, 0xdd, 0xc7, 0x97 },
};

//Compare mac address
if (memcmp(advertisedDevice.getAddress().getNative(), macs[CAM_SERVER], MAC_ADDRESS_LEN) == 0)
{ ... }
    

This is not correct, see my first reply:

Hello,

I thinking i resolve my problem but not:

uint8_t macIndex = 2;
using mac_t = std::array<uint8_t, 6>;
const mac_t  macs[] = {
  {0x34, 0xb4, 0x72, 0x4d, 0x63, 0xc6 },
  {0xb0, 0x7e, 0x11, 0xdd, 0xc7, 0x97 },
  {0x01, 0x02, 0x03, 0xdd, 0xc7, 0x97 }
};
mac_t mac_addr_Server_Used;
BLEAddress CamServer(mac_addr_Server_Used.data());

/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (memcmp(advertisedDevice.getAddress().getNative(),CamServer.getNative(), 6) == 0) {
      Serial.print("BLE Advertised Device checked: ");
      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks


void scanDevicesBLE(){

 // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
  
}

void setup()

{

mac_addr_Server_Used = macs[macIndex];


}

It nevers enter on the if condition :

if (memcmp(advertisedDevice.getAddress().getNative(),CamServer.getNative(), 6) == 0) {
      Serial.print("BLE Advertised Device checked: ");
.......

Do you know what is the problem please?

Thank you.

CamServer is not initialized to any value:

mac_t mac_addr_Server_Used;
BLEAddress CamServer(mac_addr_Server_Used.data());

EDIT: Instead of complicating things, why do you not just use the simplest solution:

#define MAC_ADDRESS_LEN 6
const uint8_t CamServer[MAC_ADDRESS_LEN] = {0xb0, 0x7e, 0x11, 0xdd, 0xc7, 0x97 };
...
//Compare mac address
if (memcmp(advertisedDevice.getAddress().getNative(), CamServer, MAC_ADDRESS_LEN) == 0)
{ ... }

If you want to convert "CamServer" to a BLEAddress elsewhere in the code, just use:

BLEAddress CamServerBLE((uint8_t*)CamServer);
//Do something with CamServerBLE

Yes the simplest solution works but i want to know how with ```
const mac_t macs[ ] i can use?