nRF52832-Bluefruit52学习之Arduino开发(3)-- 蓝牙组网一拖8主机模式(central_bleuart)

本文主要是介绍nRF52832-Bluefruit52学习之Arduino开发(3)-- 蓝牙组网一拖8主机模式(central_bleuart),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       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 msBluefruit.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 serviceif ( Bluefruit.Scanner.checkReportForService(report, clientUart) ){Serial.print("BLE UART service detected. Connecting ... ");// Connect to device with bleuart service in advertisingBluefruit.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 Manufacturermemset(buffer, 0, sizeof(buffer));if ( clientDis.getManufacturer(buffer, sizeof(buffer)) ){Serial.print("Manufacturer: ");Serial.println(buffer);}// read and print out Model Numbermemset(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 serviceBluefruit.Central.disconnect(conn_handle);}  
}

Full Sample Code完整的例程代码:

/*********************************************************************This is an example for our nRF52 based Bluefruit LE modulesPick 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 purchasingproducts from Adafruit!MIT license, check LICENSE for more informationAll text above, and the splash screen below must be included inany 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 clientvoid setup()
{Serial.begin(115200);while ( !Serial ) delay(10);   // for nrf52840 with native usbSerial.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 connectionsBluefruit.begin(0, 1);Bluefruit.setName("Bluefruit52 Central");// Configure Battyer clientclientBas.begin();  // Configure DIS clientclientDis.begin();// Init BLE Central Uart SerivceclientUart.begin();clientUart.setRxCallback(bleuart_rx_callback);// Increase Blink rate to different from PrPh advertising modeBluefruit.setConnLedInterval(250);// Callbacks for CentralBluefruit.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 msBluefruit.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 serviceif ( Bluefruit.Scanner.checkReportForService(report, clientUart) ){Serial.print("BLE UART service detected. Connecting ... ");// Connect to device with bleuart service in advertisingBluefruit.Central.connect(report);}else{      // For Softdevice v6: after received a report, scanner will be paused// We need to call Scanner resume() to continue scanningBluefruit.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 Manufacturermemset(buffer, 0, sizeof(buffer));if ( clientDis.getManufacturer(buffer, sizeof(buffer)) ){Serial.print("Manufacturer: ");Serial.println(buffer);}// read and print out Model Numbermemset(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 serviceBluefruit.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 yetif ( clientUart.discovered() ){// Discovered means in working state// Get Serial input and send to Peripheralif ( Serial.available() ){delay(2); // delay a bit for all characters to arrivechar str[20+1] = { 0 };Serial.readBytes(str, 20);clientUart.print( str );}}}
}

从上面分析的代码流程,我们很容易理解蓝牙组网连接的流程。本例程代码是主机端的,从机端将在下一篇博文。从最后的实验现象我们可以看出,蓝牙组网中主机只能存在一个设备,从机可以是多个设备,从机是主从模式存在的,所以此蓝牙网络是一种级联的形式,中间连接的从设备既充当主机又是中继站。

这篇关于nRF52832-Bluefruit52学习之Arduino开发(3)-- 蓝牙组网一拖8主机模式(central_bleuart)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/621467

相关文章

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis