BLE.scan() example not working

Hi,

I am trying to get the BLE.scan() example from the docs working. It compiles and I can flash it to my argon but it does not find any bluetooth devices :disappointed_relieved:
I have turned on bluetooth on 2 smartphones, 1 notebook and 1 sensor and made them visable but the Argon does not detect them.

This is my code:

#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();
    Log.info("Scanning...");
    if (scanResults.size()) {
        Log.info("%d devices found", scanResults.size());

        for (int ii = 0; ii < scanResults.size(); ii++) {
            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);
}

The device OS version is 3.1.0-rc.1
I would be very grateful if you could tell me what I am doing wrong :slight_smile:

I found a working solution which looks like this:

/*
 * ble_5_phy_coded_scan_test.ino
 * Description: Test app demonstrates SCANNING for standard (1 MBPS) and long range (Coded Phy) devices
 * Author: Mark Leavitt    mark@markleavitt.com
 * Date: Mar 3 2021
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "Particle.h"
//SYSTEM_MODE(MANUAL);                           // Cellular connectivity not needed

SerialLogHandler logHandler(LOG_LEVEL_INFO);   // Results will be logged over USB Serial
const size_t SCAN_RESULT_MAX = 100;
BleScanResult scanResults[SCAN_RESULT_MAX];
BleScanParams scanParams;

void setup() {
  delay(5000);                                 // Allow time for USB serial to connect
  BLE.on();
  Log.info("BLE 5 PHY CODED SCAN TEST");
  scanParams.version = BLE_API_VERSION;
  scanParams.size = sizeof(BleScanParams);
  BLE.getScanParameters(&scanParams);              // Get the default scan parameters
  scanParams.timeout = 1000;                       // Change timeout to 10 seconds
  // Scanning for both 1 MBPS and CODED PHY simultaneously requires scanning window <= 1/2 the scanning interval.
  // We will widen the window to 2/3 of the interval so automatic override will be tested in simultaneous mode
  scanParams.window = (2 * scanParams.interval) / 3 ;
  BLE.setScanParameters(&scanParams);              // Set the modified scan parameters
}

void loop() {
  int count;
  // First, scan and log with standard PHYS_1MBPS
  scanParams.scan_phys = BLE_PHYS_1MBPS;
  BLE.setScanParameters(scanParams);
  count = BLE.scan(scanResults, SCAN_RESULT_MAX);
  logDevices(count, "PHYS_1MBPS (standard)");
}

void logDevices(int deviceCount, String mode) {
  Log.info("Scanning with %s found %d devices", mode.c_str(), deviceCount);
   
   
  for (int i = 0; i < deviceCount; i++) {
    Log.info("   Device addr: %02X:%02X:%02X:%02X:%02X:%02X, rssi: %d dBm, name: %s, 
    scanResults[i].address()[5], scanResults[i].address()[4],scanResults[i].address()[3],
    scanResults[i].address()[2],scanResults[i].address()[1],scanResults[i].address()[0],
    scanResults[i].rssi(), scanResults[i].advertisingData().deviceName().c_str(),
    scanResults[i].advertisingData().serviceUUID());

  }
  Log.info("--------------------------------------------------------");
}

Now my Argon finds several Bluetooth devices including my sensor (https://github.com/atc1441/ATC_MiThermometer).
My sensor sends every minute an update of advertising data on the UUID 0x181A with the Tempereature, Humidity and Battery data. It is not necessary to connect to the sensor.
With my phone I can read the raw data without connecting.

The format of the advertising data is as follow:
Byte 5-10 MAC in correct order
Byte 11-12 Temperature in int16
Byte 13 Humidity in percent
Byte 14 Battery in percent
Byte 15-16 Battery in mV uint16_t
Byte 17 frame packet counter
Example: 0x0e, 0x16, 0x1a, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xbb, 0xcc, 0xdd, 0xdd, 0x00

I can see the correct MAC Address in the serial monitor but I do not know how to read the full advertasing data. Any ideas? :slight_smile:

0003694802 [app] INFO: Scanning with PHYS_1MBPS (standard) found 7 devices
0003694802 [app] INFO:    Device addr: 4C:E7:BF:16:8F:DD, rssi: -41 dBm, name: 
0003694803 [app] INFO:    Device addr: 64:38:A7:1F:42:98, rssi: -47 dBm, name:
0003694804 [app] INFO:    Device addr: 20:CA:D3:4C:D8:7A, rssi: -51 dBm, name:
0003694804 [app] INFO:    Device addr: 6E:96:DB:AD:31:7A, rssi: -67 dBm, name: 
0003694805 [app] INFO:    Device addr: 47:2D:A7:53:57:39, rssi: -64 dBm, name: 
0003694806 [app] INFO:    Device addr: 5B:B4:FA:51:22:F7, rssi: -81 dBm, name:
0003694807 [app] INFO:    Device addr: A4:C1:38:CA:52:32, rssi: -43 dBm, name:
0003694807 [app] INFO: --------------------------------------------------------