Datasheet
Central Role
You normally need to setup the Central mode device's connect callback, which fires when a connection is
established/disconnected with a peripheral device. Alternatively you could poll the connection status with connected(),
but callbacks help to simplify the code significantly:
In the connect callback, we will try to discover the bleuart service by browsing the GATT table of the peripheral. This
will help to determine the handle values for characteristics (e.g TXD, RXD, etc.). This is all done by BLEClientUart's
.discover() . Once the service is found, enable the TXD characteristic's CCCD to allow the peripheral to send data, and
we are ready to send data back and forth between the devices:
void setup()
{
// Other set up .....
/* Start Central Scanning
* - Enable auto scan if disconnected
* - Interval = 100 ms, window = 80 ms
* - Don't use active scan
* - Start(timeout) with timeout = 0 will scan forever (until connected)
*/
Bluefruit.Scanner.setRxCallback(scan_callback);
Bluefruit.Scanner.restartOnDisconnect(true);
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
Bluefruit.Scanner.useActiveScan(false);
Bluefruit.Scanner.start(0); // // 0 = Don't stop scanning after n seconds
}
/**
* Callback invoked when scanner pick up an advertising data
* @param report Structural advertising data
*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{
// Check if advertising contain BleUart service
if ( Bluefruit.Scanner.checkReportForService(report, clientUart) )
{
Serial.print("BLE UART service detected. Connecting ... ");
// Connect to device with bleuart service in advertising
Bluefruit.Central.connect(report);
}
}
// Callbacks for Central
Bluefruit.Central.setConnectCallback(connect_callback);
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
© Adafruit Industries https://learn.adafruit.com/bluefruit-nrf52-feather-learning-guide Page 71 of 175