Build BLE string for Particle.publish

argon
Tags: #<Tag:0x00007fe22bdd6a48>

#1

Good day. I have been trying to publish the BLE 31 byte string to the cloud for the past 2 days. I can’t seem to get the correct string together. I either get ASCII, singular bytes, or way too much data.
If someone can assist on how to get the uint8_t buf array to a 31 byte string array so I can publish it.

Thanks so much. One of my attempts to build the string below

void loop() {
    int count = BLE.scan(results, SCAN_RESULT_COUNT); // scan how many beacons are around

    if (count > 0) 
    {
        uint8_t buf[BLE_MAX_ADV_DATA_LEN];
        size_t len;

        Serial.printf("%d devices are found:", count);// how many beacons were found
        for (int i = 0; i < count; i++) {
            BleAddress address = results[i].address;  // mac address
            Serial.printf("MAC: %s\r\n", address.toString().c_str());


            len = results[i].advertisingData(buf, sizeof(buf));
            if (len > 0) 
            {
                Serial.printf("Advertising data length: %02d\r\n", results[i].advertisingData.length());
                for (size_t j = 0; j < len; j++) 
                {
                    Serial.printf("%02x,", buf[j]);  
                    memmove (AdvData,buf,31); // copy the buffer to advdata                 
                }
                  
                    for (int i=0; i<(sizeof(buf)/sizeof(buf[0]));i++)
                        {
                            char s [32];
                            sprintf(s,"%d",buf[i]);
                            strcat (AdvData_publish,s);
                        }

                    Particle.publish(EVENT_NAME, AdvData_publish , PRIVATE);
                                   
                 String Beacon_name = results[i].advertisingData.deviceName(); // check if the results have an name
                if (Beacon_name.length() > 0) {Serial.printlnf("Name: %s", Beacon_name.c_str());}
                    Serial.println("\r\n");
                }

            len = results[i].scanResponse(buf, sizeof(buf));
            if (len > 0) {
                Serial.printf("Scan response data length: %02d\r\n", results[i].scanResponse.length());
                for (size_t j = 0; j < len; j++) {
                    Serial.printf("%02x,", buf[j]);
                }
                Serial.println("\r\n");
            }
        }
    }

    delay(5000);
}


#2

What about printing out the AdvData and AdvData_publish string via Serial.println() and check the validity of the contents?

Where/how have you declared AdvData_publish?
Why have you got the memmove() inside your for() loop?
You are using BLE_MAX_ADV_DATA_LEN concurrently with hardcoded 31 and 32 - why that?

BTW, the indentation in that code snippet doesn’t really help following the code flow :wink:


#3

Thanks Scruff, Yip its a bit messy. thats from trying retrying.

Im using sprintf to build a character string and can now publish BLE AdvData.

char  AdvData[32];

  sprintf(AdvData,"%02x:%02x:%02x:%02x:%02x:%02x", 
                             scanResults[ii].address[0], scanResults[ii].address[1], scanResults[ii].address[2],
                             scanResults[ii].address[3], scanResults[ii].address[4], scanResults[ii].address[5]);
                   
           Particle.publish("Beacon MAC", AdvData , PRIVATE);