0

Bluefruit nrf52 - Disconnecting on command
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Bluefruit nrf52 - Disconnecting on command

by David_BA on Wed Feb 13, 2019 5:36 pm

Hello everybody,

I did establish an connection with my phone an Bluefruit nrf52.
Now I do wont to disconnect from the phone, as soon as i get a specific message.
Does somebody of you knows a function of the Bluefruit library with which I can do that.

Thanks a lot for your help in advance.

Cheers,
David

David_BA
 
Posts: 11
Joined: Tue Nov 20, 2018 12:58 pm

Re: Bluefruit nrf52 - Disconnecting on command

by jps2000 on Thu Feb 14, 2019 7:05 am

Hi David,
have you tried bluefruit.disconnect(); ?
This should drop a connection.

jps2000
 
Posts: 334
Joined: Fri Jun 02, 2017 4:12 pm

Re: Bluefruit nrf52 - Disconnecting on command

by David_BA on Thu Feb 14, 2019 8:25 am

Yes i did try Bluefruit.disconnect() but it didn't work .:(

Code: Select all | TOGGLE FULL SIZE
          Bluefruit.disconnect();
          uint8_t locatioAdv[] = {0xFF, 0xFF, 0xFF, 0xFF};
          sendtoApp(locatioAdv, 4, false);


It will still send this message to the App and on the App side no ConnectionStateChange() is recognized.

David_BA
 
Posts: 11
Joined: Tue Nov 20, 2018 12:58 pm

Re: Bluefruit nrf52 - Disconnecting on command

by jps2000 on Thu Feb 14, 2019 8:36 am

I tried it in the bleuart example. It worked in the following way.
I send a message "e" (or whatever) from the BLE connect app to the peripheral.
Then the peripheral (feather nrf52) detects this and calls bluefruit.disconnect();
Then the peripheral starts blinking blue and the app says connect (ready for next connect).
So the peripheral drops the connection.

jps2000
 
Posts: 334
Joined: Fri Jun 02, 2017 4:12 pm

Re: Bluefruit nrf52 - Disconnecting on command

by David_BA on Thu Feb 14, 2019 9:32 am

Well, thats strange.
I did try to rebuild the issue as well.
Therefore i used the Dual Roles BLEUART example code and added the disconnect function if the bluefruit receives "a".
And that didn't work as well.


The Serial output is the following one:

Bluefruit52 Dual Role BLEUART Example
-------------------------------------

[Prph] Connected to (N18) Samsung Galax
[Prph] RX: odfo

[Prph] RX: 0

Code: Select all | TOGGLE FULL SIZE
/*********************************************************************
 This is an example for our nRF52 based Bluefruit LE modules

 Pick one up today in the adafruit shop!

 Adafruit invests time and resources providing this open source code,
 please support Adafruit and open-source hardware by purchasing
 products from Adafruit!

 MIT license, check LICENSE for more information
 All text above, and the splash screen below must be included in
 any redistribution
*********************************************************************/

/*
 * This sketch demonstrate how to run both Central and Peripheral roles
 * at the same time. It will act as a relay between an central (mobile)
 * to another peripheral using bleuart service.
 *
 * Mobile <--> DualRole <--> peripheral Ble Uart
 */
#include <bluefruit.h>

// Peripheral uart service
BLEUart bleuart;

// Central uart client
BLEClientUart clientUart;

void setup()
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // for nrf52840 with native usb

  Serial.println("Bluefruit52 Dual Role BLEUART Example");
  Serial.println("-------------------------------------\n");
 
  // Initialize Bluefruit with max concurrent connections as Peripheral = 1, Central = 1
  // SRAM usage required by SoftDevice will increase with number of connections
  Bluefruit.begin(1, 1);
  // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  Bluefruit.setTxPower(4);
  Bluefruit.setName("Bluefruit52 duo");

  // Callbacks for Peripheral
  Bluefruit.setConnectCallback(prph_connect_callback);
  Bluefruit.setDisconnectCallback(prph_disconnect_callback);

  // Callbacks for Central
  Bluefruit.Central.setConnectCallback(cent_connect_callback);
  Bluefruit.Central.setDisconnectCallback(cent_disconnect_callback);

  // Configure and Start BLE Uart Service
  bleuart.begin();
  bleuart.setRxCallback(prph_bleuart_rx_callback);

  // Init BLE Central Uart Serivce
  clientUart.begin();
  clientUart.setRxCallback(cent_bleuart_rx_callback);


  // Set up and start advertising
  startAdv();
}

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  // Include bleuart 128-bit uuid
  Bluefruit.Advertising.addService(bleuart);

  // Secondary Scan Response packet (optional)
  // Since there is no room for 'Name' in Advertising packet
  Bluefruit.ScanResponse.addName();

  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   *
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html
   */
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
  Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds
}

void loop()
{
  // do nothing, all the work is done in callback
}

/*------------------------------------------------------------------*/
/* Peripheral
 *------------------------------------------------------------------*/
void prph_connect_callback(uint16_t conn_handle)
{
  char peer_name[32] = { 0 };
  Bluefruit.Gap.getPeerName(conn_handle, peer_name, sizeof(peer_name));

  Serial.print("[Prph] Connected to ");
  Serial.println(peer_name);
}

void prph_disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

  Serial.println();
  Serial.println("[Prph] Disconnected");
}

void prph_bleuart_rx_callback(void)
{
  // Forward data from Mobile to our peripheral
  char str[20+1] = { 0 };
  bleuart.read(str, 20);

  Serial.print("[Prph] RX: ");
  Serial.println(str); 
  if(str[0]==0){
    Bluefruit.disconnect(); 
  }

  if ( clientUart.discovered() )
  {
    clientUart.print(str);
  }else
  {
    bleuart.println("[Prph] Central role not connected");
  }
}

/*------------------------------------------------------------------*/
/* Central
 *------------------------------------------------------------------*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{
  // Since we configure the scanner with filterUuid()
  // Scan callback only invoked for device with bleuart service advertised 
  // Connect to the device with bleuart service in advertising packet 
  Bluefruit.Central.connect(report);
}

void cent_connect_callback(uint16_t conn_handle)
{
  char peer_name[32] = { 0 };
  Bluefruit.Gap.getPeerName(conn_handle, peer_name, sizeof(peer_name));

  Serial.print("[Cent] Connected to ");
  Serial.println(peer_name);;

  if ( clientUart.discover(conn_handle) )
  {
    // Enable TXD's notify
    clientUart.enableTXD();
  }else
  {
    // disconect since we couldn't find bleuart service
    Bluefruit.Central.disconnect(conn_handle);
  } 
}

void cent_disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;
 
  Serial.println("[Cent] Disconnected");
}

/**
 * Callback invoked when uart received data
 * @param cent_uart Reference object to the service where the data
 * arrived. In this example it is clientUart
 */
void cent_bleuart_rx_callback(BLEClientUart& cent_uart)
{
  char str[20+1] = { 0 };
  cent_uart.read(str, 20);
     
  Serial.print("[Cent] RX: ");
  Serial.println(str);

  if ( bleuart.notifyEnabled() )
  {
    // Forward data from our peripheral to Mobile
    bleuart.print( str );
  }else
  {
    // response with no prph message
    clientUart.println("[Cent] Peripheral role not connected");
  } 
}


David_BA
 
Posts: 11
Joined: Tue Nov 20, 2018 12:58 pm

Re: Bluefruit nrf52 - Disconnecting on command

by jps2000 on Thu Feb 14, 2019 10:26 am

Are you sure that you receive the command?
Try switch on or blink Led on pin 17 to verify that
I used the bleuart peripheral example
here the rx is handled in the loop
I never tried the dual role stuff

jps2000
 
Posts: 334
Joined: Fri Jun 02, 2017 4:12 pm

Re: Bluefruit nrf52 - Disconnecting on command

by David_BA on Thu Feb 14, 2019 4:06 pm

In the end thatdid work:
Bluefruit.Central.disconnect(conn_handler)

But for that I did need to same the conn_handler as global variable. :/

But still thanks a lot for your help!!

David_BA
 
Posts: 11
Joined: Tue Nov 20, 2018 12:58 pm

Please be positive and constructive with your questions and comments.