finish dual role bleuart
This commit is contained in:
1 parent
56446aab63
commit
c07f85a32e
1 file changed
+115
-85
@@ -14,7 +14,8 @@
|
||||
|
||||
/*
|
||||
* This sketch demonstrate how to run both Central and Peripheral roles
|
||||
* at the same tiem
|
||||
* at the same time. It will act as a relay between an central (mobile)
|
||||
* to another peripheral using bleuart service.
|
||||
*/
|
||||
#include <bluefruit.h>
|
||||
|
||||
@@ -35,149 +36,178 @@ void setup()
|
||||
Bluefruit.begin(true, true);
|
||||
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
Bluefruit.setTxPower(4);
|
||||
Bluefruit.setName("Bluefruit52");
|
||||
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(bleuart_rx_callback);
|
||||
clientUart.setRxCallback(cent_bleuart_rx_callback);
|
||||
|
||||
|
||||
/* Start Central Scanning
|
||||
* - Enable auto scan if disconnected
|
||||
* - Interval = 100 ms, window = 80 ms
|
||||
* - Filter only accept bleuart service
|
||||
* - 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.filterUuid(bleuart.uuid);
|
||||
Bluefruit.Scanner.useActiveScan(false);
|
||||
Bluefruit.Scanner.start(0); // // 0 = Don't stop scanning after n seconds
|
||||
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
|
||||
|
||||
// Set up and start advertising
|
||||
startAdv();
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when scanner pick up an advertising data
|
||||
* @param report Structural advertising data
|
||||
*/
|
||||
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 ( clientUart.discovered() )
|
||||
{
|
||||
clientUart.print(str);
|
||||
}else
|
||||
{
|
||||
bleuart.println("[Prph] Central role not connected");
|
||||
}
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Central
|
||||
*------------------------------------------------------------------*/
|
||||
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 ... ");
|
||||
Serial.println("BLE UART service detected. Connecting ... ");
|
||||
|
||||
// Connect to device with bleuart service in advertising
|
||||
Bluefruit.Central.connect(report);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when an connection is established
|
||||
* @param conn_handle
|
||||
*/
|
||||
void cent_connect_callback(uint16_t conn_handle)
|
||||
{
|
||||
Serial.println("Connected");
|
||||
char peer_name[32] = { 0 };
|
||||
Bluefruit.Gap.getPeerName(conn_handle, peer_name, sizeof(peer_name));
|
||||
|
||||
Serial.print("Dicovering DIS ... ");
|
||||
if ( clientDis.discover(conn_handle) )
|
||||
{
|
||||
Serial.println("Found it");
|
||||
char buffer[32+1];
|
||||
|
||||
// read and print out Manufacturer
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
if ( clientDis.getManufacturer(buffer, sizeof(buffer)) )
|
||||
{
|
||||
Serial.print("Manufacturer: ");
|
||||
Serial.println(buffer);
|
||||
}
|
||||
|
||||
// read and print out Model Number
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
if ( clientDis.getModel(buffer, sizeof(buffer)) )
|
||||
{
|
||||
Serial.print("Model: ");
|
||||
Serial.println(buffer);
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
Serial.print("Discovering BLE Uart Service ... ");
|
||||
Serial.print("[Cent] Connected to ");
|
||||
Serial.println(peer_name);;
|
||||
|
||||
if ( clientUart.discover(conn_handle) )
|
||||
{
|
||||
Serial.println("Found it");
|
||||
|
||||
Serial.println("Enable TXD's notify");
|
||||
// Enable TXD's notify
|
||||
clientUart.enableTXD();
|
||||
|
||||
Serial.println("Ready to receive from peripheral");
|
||||
}else
|
||||
{
|
||||
Serial.println("Found NONE");
|
||||
|
||||
// disconect since we couldn't find bleuart service
|
||||
Bluefruit.Central.disconnect(conn_handle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle
|
||||
* @param reason
|
||||
*/
|
||||
void cent_disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) conn_handle;
|
||||
(void) reason;
|
||||
|
||||
Serial.println("Disconnected");
|
||||
Serial.println("[Cent] Disconnected");
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when uart received data
|
||||
* @param uart_svc Reference object to the service where the data
|
||||
* @param cent_uart Reference object to the service where the data
|
||||
* arrived. In this example it is clientUart
|
||||
*/
|
||||
void bleuart_rx_callback(BLEClientUart& uart_svc)
|
||||
void cent_bleuart_rx_callback(BLEClientUart& cent_uart)
|
||||
{
|
||||
Serial.print("[RX]: ");
|
||||
|
||||
while ( uart_svc.available() )
|
||||
{
|
||||
Serial.print( (char) uart_svc.read() );
|
||||
}
|
||||
char str[20+1] = { 0 };
|
||||
cent_uart.read(str, 20);
|
||||
|
||||
Serial.print("[Cent] RX: ");
|
||||
Serial.println(str);
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if ( Bluefruit.Central.connected() )
|
||||
{
|
||||
// Not discovered yet
|
||||
if ( clientUart.discovered() )
|
||||
{
|
||||
// Discovered means in working state
|
||||
// Get Serial input and send to Peripheral
|
||||
if ( Serial.available() )
|
||||
{
|
||||
delay(2); // delay a bit for all characters to arrive
|
||||
|
||||
char str[20+1] = { 0 };
|
||||
Serial.readBytes(str, 20);
|
||||
|
||||
clientUart.print( 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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user