Trying to understand ArduinoBLE device discovery

I bought a little Bluetooth Low Energy temperature and humidity sensor. I was able to find it using the LightBlue app on my iPhone, it appears with the name "LYWSD03MMC", and good strong signal strength. So I'm trying to find it and talk to it, starting with this example as my basis:

My code is at the end of this posting.

I found that I needed to move most of the code into my loop() instead of my setup() function or things would go by too quickly and nothing would be found. That got me closer. But things are really flaky. I usually find two devices. I strongly suspect that they are my laptop and my phone. Most often the connect() call fails. If it succeeds, the discoverAttributes call invariably fails. And my efforts to print the device name show an empty string.

I really wish I had more descriptive text to go with the calling sequences of the arduinoBLE library. After BLE.available(), what is the state of things? Does my BLEDevice record know the name and the UU ID of the thing it got yet? Or do I need to discover attributes first? And what does it mean for attribute discovery to fail? How could that even happen? How might I pursue this to debug it?

If I replace BLE.scan() with BLE.scanForName("LYWSD03MMC"), I get no devices at all. The thing is a foot away from the Arduino board, and my cell phone was showing strong signal strength, so signal can't be the problem. Is there just some weakness of this library that can fail to reach certain devices?

Thanks for any help.

#include <ArduinoBLE.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) ;
  
  Serial.println("I live!");
  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  Serial.println("BLE Central scan");

  // start scanning for peripheral, collect a list
  BLE.scan();
  // BLE.scanForName("LYWSD03MMC");
}

void loop() {
  BLEDevice peripheral;
  do {
    peripheral = BLE.available(); // pick one off the list
        Serial.print("Device name: ");
        Serial.println(peripheral.deviceName());    // can this work?
    if (peripheral) {
      Serial.println("Connecting ...");
  
      if (peripheral.connect()) {
        Serial.println("Connected");
        Serial.print("Device name: ");
        Serial.println(peripheral.deviceName());    
      } else {
        Serial.println("Failed to connect!");
        return;
      }
  
      // discover peripheral attributes
      Serial.println("Discovering attributes ...");
      if (peripheral.discoverAttributes()) {
        Serial.println("Attributes discovered");
      } else {
        Serial.println("Attribute discovery failed!");
        peripheral.disconnect();
        return;
      }
  
      // read and print device name of peripheral
      Serial.println();        
      Serial.print("Device name: ");
      Serial.println(peripheral.deviceName());    


    } else {
      Serial.println("No device");
    }
    delay(300);
  } while (peripheral);

  BLE.scan();
}

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