BLE Central Gateway Example Code

Can I get a simple code example that reads advertising data from BLE devices and push it to particle cloud ?

The samples in the docs about BLE.scan() illustrate how to scan for devices and catch their advertised data in the process.

And pushing the received data would go via Particle.publish()

If you give it a try and show us what you came up with we can help you on your journey.

1 Like

I have flashed the code from Web IDE. Where Can I look at the printing Logs ?

#include "Particle.h"

SYSTEM_MODE(MANUAL);

SerialLogHandler logHandler(LOG_LEVEL_TRACE);

void setup() {
    (void)logHandler; // Does nothing, just to eliminate warning for unused variable
}

void loop() {
    Vector<BleScanResult> scanResults = BLE.scan();

    if (scanResults.size()) {
        Log.info("%d devices found", scanResults.size());

        for (int ii = 0; ii < scanResults.size(); ii++) {
            // For Device OS 2.x and earlier, use scanResults[ii].address[0], etc. without the ()
            Log.info("MAC: %02X:%02X:%02X:%02X:%02X:%02X | RSSI: %dBm",
                    scanResults[ii].address[0], scanResults[ii].address[1], scanResults[ii].address[2],
                    scanResults[ii].address[3], scanResults[ii].address[4], scanResults[ii].address[5], scanResults[ii].rssi);

            String name = scanResults[ii].advertisingData.deviceName();
            if (name.length() > 0) {
                Log.info("Advertising name: %s", name.c_str());
            }
        }
    }

    delay(3000);
}

e.g. particle serial monitor --follow or any other terminal program that can connect to the USB Serial port of the device.

1 Like

Thanks. It’s working now

I am able to scan and get MAC address and RSSI values now. If I want to get the entire raw data how can I parse. Attached the raw data I got on NRF connect app from my advertising device for reference

These are two mutually exclusive demands in on sentence.
Either you want the raw data or the parsed data. Which is it?

Raw is simple: scanResults[ii].advertisingData.get(buf, scanResults[ii].advertisingData.length) (where buf is a uint8_t* with enough space to take length bytes).

Parsing is a bit more complicated as it depends on the data type how the respective field needs to be interpreted.

If you want to know how, you need to get yourself acquainted with the BLE Advertising Data Packet format.

1 Like

I just to want that RAW hex data. let me try as you suggested

I tried your suggestion but the advertising data I am getting is different from one I am getting in the NRF connect app.

Raw Data Received on Particle : 20016FA4
Raw Data Received on Raspberry or NRF connect : 000027000000000100bb

#include "Particle.h"

SYSTEM_MODE(MANUAL);

SerialLogHandler logHandler(LOG_LEVEL_TRACE);

void setup() {
    (void)logHandler; // Does nothing, just to eliminate warning for unused variable
}

void loop() {
    Vector<BleScanResult> scanResults = BLE.scan();

    if (scanResults.size()) {
        Log.info("%d devices found", scanResults.size());

        for (int ii = 0; ii < scanResults.size(); ii++) {
            uint8_t buf[100];
            size_t len;
            // For Device OS 2.x and earlier, use scanResults[ii].address[0], etc. without the ()
            Log.info("MAC: %02X:%02X:%02X:%02X:%02X:%02X | RSSI: %dBm",
                    scanResults[ii].address[0], scanResults[ii].address[1], scanResults[ii].address[2],
                    scanResults[ii].address[3], scanResults[ii].address[4], scanResults[ii].address[5], scanResults[ii].rssi);

            String name = scanResults[ii].advertisingData.deviceName();
            
            len = scanResults[ii].advertisingData.get(buf,len);
            
            if (name.length() > 0) {
                Log.info("Advertising name: %s", name.c_str());
                Log.info("Length: %d", len);
                Log.info("Raw Data: %02X", buf);
            }
        }
    }

    delay(3000);
}

This is the code I am using on Particle

Just a question: What is the initial value of len before you call

size_t len;
I Just declared type only before calling the line

0000044936 [app] INFO: Advertising name: BB-FF6235
0000044937 [app] INFO: Length: 24
0000044937 [app] INFO: Raw Data: 20016FA4

This is the log I am getting from my device

len = results[i].scanResponse(buf, sizeof(buf));

This one finally worked !

Or this should have :wink:

3 Likes