nRF52832技术交流群:680723714
nRF52832-Bluefruit52核心板详细介绍:
https://blog.csdn.net/solar_Lan/article/details/88688451
github仓库地址:https://github.com/Afantor/Bluefruit52_Arduino.git
Arduino例程目前分为6大部分:Central、DualRoles、Display、Hardware、Peripheral、Project。
一、Central:主设备与其他设备通信例程
二、DualRoles:从设备与主设备通信例程
本篇讲解蓝牙组网一拖8实验,本实验展示了以一个nRF52382为中心,其他nRF52832模块为外围设备,进行级联通信控制的过程。示例中演示串口打印连接状态和接收到数据,通过数据收发控制板载LED灯。主机中心为一个主模式设备,检测按键发送控制LED的字符。
下面直接讲解代码:
Client Services客户端服务
由于Central角色访问外围设备上的GATT服务器,我们首先需要使用BLEClientUart帮助程序类声明客户端bleuart实例。 如果还使用BLEClientDis,我们也可以方便地读取设备信息。
BLEClientDis clientDis;
BLEClientUart clientUart;
在我们配置客户端服务之前,必须至少调用Bluefruit.begin(),以获得中央模式支持的并发连接数。 由于在这种情况下我们不会将nRF52作为外设运行,因此我们将外设计数设置为0:
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
Bluefruit.begin(0, 1);
之后,必须通过调用begin()函数初始化客户端服务,并且可以从helper类设置任何要使用的回调:
// Configure DIS client
clientDis.begin();
// Init BLE Central Uart Serivce
clientUart.begin();
clientUart.setRxCallback(bleuart_rx_callback);
Scanner扫描器
让我们启动广告扫描仪找到一个外围设备。
我们将使用setRxCallback()连接扫描结果回调。
每当扫描仪发现广告数据时,它将被传递给该回调处理程序,我们可以检查那里的广告数据,并且只连接到广播bleuart服务的外围设备。
注意:如果外围设备有多个服务且广告包中的UUID列表中未包含bleuart,您可以选择使用其他检查,例如匹配MAC地址,名称检查,使用“其他服务”等。
找到我们希望与之通信的外围设备后,请调用Bluefruit.Central.connect()与其建立连接:
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);
}
}
Central Role中心角色
通常需要设置中央模式设备的连接回调,该连接回调在与外围设备建立/断开连接时触发。 或者,可以使用connected()轮询连接状态,但回调有助于显着简化代码:
// Callbacks for Central
Bluefruit.Central.setConnectCallback(connect_callback);
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
在连接回调中,我们将尝试通过浏览外围设备的GATT表来发现bleuart服务。 这将有助于确定特征的句柄值(例如TXD,RXD等)。 这完全由BLEClientUart的.discover()完成。 找到服务后,启用TXD特性的CCCD以允许外设发送数据,我们准备在设备之间来回发送数据:
void connect_callback(uint16_t conn_handle)
{
Serial.println("Connected");
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 ... ");
if ( clientUart.discover(conn_handle) )
{
Serial.println("Found it");
Serial.println("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);
}
}
Full Sample Code完整的例程代码:
/*********************************************************************
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 the central API(). A additional bluefruit
* that has bleuart as peripheral is required for the demo.
*/
#include <bluefruit.h>
BLEClientBas clientBas; // battery client
BLEClientDis clientDis; // device information client
BLEClientUart clientUart; // bleuart client
void setup()
{
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
Serial.println("Bluefruit52 Central BLEUART Example");
Serial.println("-----------------------------------\n");
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
// SRAM usage required by SoftDevice will increase dramatically with number of connections
Bluefruit.begin(0, 1);
Bluefruit.setName("Bluefruit52 Central");
// Configure Battyer client
clientBas.begin();
// Configure DIS client
clientDis.begin();
// Init BLE Central Uart Serivce
clientUart.begin();
clientUart.setRxCallback(bleuart_rx_callback);
// Increase Blink rate to different from PrPh advertising mode
Bluefruit.setConnLedInterval(250);
// Callbacks for Central
Bluefruit.Central.setConnectCallback(connect_callback);
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
/* 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);
}else
{
// For Softdevice v6: after received a report, scanner will be paused
// We need to call Scanner resume() to continue scanning
Bluefruit.Scanner.resume();
}
}
/**
* Callback invoked when an connection is established
* @param conn_handle
*/
void connect_callback(uint16_t conn_handle)
{
Serial.println("Connected");
Serial.print("Dicovering Device Information ... ");
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();
}else
{
Serial.println("Found NONE");
}
Serial.print("Dicovering Battery ... ");
if ( clientBas.discover(conn_handle) )
{
Serial.println("Found it");
Serial.print("Battery level: ");
Serial.print(clientBas.read());
Serial.println("%");
}else
{
Serial.println("Found NONE");
}
Serial.print("Discovering BLE Uart Service ... ");
if ( clientUart.discover(conn_handle) )
{
Serial.println("Found it");
Serial.println("Enable TXD's notify");
clientUart.enableTXD();
Serial.println("Ready to receive from peripheral");
}else
{
Serial.println("Found NONE");
// disconnect since we couldn't find bleuart service
Bluefruit.disconnect(conn_handle);
}
}
/**
* Callback invoked when a connection is dropped
* @param conn_handle
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
}
/**
* Callback invoked when uart received data
* @param uart_svc Reference object to the service where the data
* arrived. In this example it is clientUart
*/
void bleuart_rx_callback(BLEClientUart& uart_svc)
{
Serial.print("[RX]: ");
while ( uart_svc.available() )
{
Serial.print( (char) uart_svc.read() );
}
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 );
}
}
}
}
从上面分析的代码流程,我们很容易理解蓝牙组网连接的流程。本例程代码是主机端的,从机端将在下一篇博文。从最后的实验现象我们可以看出,蓝牙组网中主机只能存在一个设备,从机可以是多个设备,从机是主从模式存在的,所以此蓝牙网络是一种级联的形式,中间连接的从设备既充当主机又是中继站。