Skip to content

BLE won't reconnect unless serial port opened up #218

Open
@sup4rl33thax0r

Description

@sup4rl33thax0r
  1. Hardware is a Sparkfun Artemis Nano. If this isn't the right place I apologize.
  2. Code connects to peripheral, reads characteristics, etc.
  3. If I disconnect peripheral (say by disconnecting power), code refuses to reconnect until the serial port in the IDE is opened. It doesn't need it open to connect the first time, but it seems to hang the second go around until the serial port window is opened back up. Importantly, there isn't a single call to Serial in the code. Not even Serial.begin()
  4. I have run the board from a battery and still will not reconnect.

Code follows:


#include "strings.h"

#include <Wire.h>
#include <ArduinoBLE.h>
#include <SparkFun_Alphanumeric_Display.h>

HT16K33 display;

uint16_t value1;
char SevenSeg1String[6]; 

bool ble_connected;

void setup() {
  ble_connected = false;

  Wire.begin();
  BLE.begin(); 
  BLE.scan();
  display.begin(0x70);

}


void loop() {
    BLEDevice peripheral = BLE.available();
    
    if (peripheral) {
      if (peripheral.advertisedServiceUuid() == "59839c23-8109-4ac1-bde9-62269a6550a4") {
        BLE.stopScan();
        manage_ble_connection(peripheral);
      }
    }    
    ble_connected = false;
    BLE.scan();
}

void manage_ble_connection(BLEDevice peripheral) {
  // connect to the peripheral
  if (peripheral.connect()) {
    ble_connected = true;
  } else {
    return;
  }

  // discover peripheral attributes
  if (peripheral.discoverService("59839c23-8109-4ac1-bde9-62269a6550a4")) {
  } else {
    peripheral.disconnect();
    
    return;
  }

  // retrieve the simple key characteristic
  BLECharacteristic simpleKeyCharacteristic = peripheral.characteristic("1d1a6dc8-3870-448d-bd1a-3e34f63d397f");

  // subscribe to the simple key characteristic
  if (!simpleKeyCharacteristic) {
    peripheral.disconnect();
    return;
  } else if (!simpleKeyCharacteristic.canSubscribe()) {
    peripheral.disconnect();
    return;
  } else if (!simpleKeyCharacteristic.subscribe()) {
    peripheral.disconnect();
    return;
  } else {
  }

  while (peripheral.connected()) {

    if(ble_connected == true){
      String dist = String(value1);   
      sprintf(SevenSeg1String, "% 4s", dist.c_str());   
      display.print(SevenSeg1String);
    }else{   
      display.print("----");
    } 
    
    // check if the value of the simple key characteristic has been updated
    if (simpleKeyCharacteristic.valueUpdated()) {
      // yes, get the value, characteristic is 1 byte so use byte value
      byte value = 0;    
      simpleKeyCharacteristic.readValue(value1);
    }
  }
  return;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions